Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature backend kotlin mappingy andrew #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Backend/mapping-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@ plugins {
kotlin("plugin.spring") version kotlinVersion
id("org.springframework.boot") version "2.4.0"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
kotlin("kapt") version kotlinVersion
}

group = "com.staffinghub.coding.challenges"
version = "0.0.1-SNAPSHOT"

repositories {
mavenCentral()
maven {
url = uri("https://maven.pkg.github.com/liodali/KotlinMapster")
credentials {
username = "{USER_NAME}"
password = "{TOKEN}"
}
}
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-test")
implementation ("com.hamza.dali:mapster-ktx:0.4.0")
implementation("org.mapstruct:mapstruct:1.5.3.Final")
testImplementation ("io.kotest:kotest-property:4.0.3")
testImplementation ("org.mockito:mockito-core:3.+")
testImplementation ("org.mockito:mockito-inline:3.11.2")
kapt("org.mapstruct:mapstruct-processor:1.5.3.Final")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.staffinghub.coding.challenges.mapping.controllers

import com.staffinghub.coding.challenges.mapping.models.dto.ArticleDto
import com.staffinghub.coding.challenges.mapping.services.ArticleService
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*

@RestController
Expand All @@ -12,6 +13,7 @@ class ArticleController(
@GetMapping
fun list(): List<ArticleDto> = articleService.list()

@ResponseStatus(HttpStatus.NOT_FOUND)
@GetMapping("/{id}")
fun details(@PathVariable id: Long): ArticleDto = articleService.articleForId(id)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.staffinghub.coding.challenges.mapping.models.dto.blocks

import com.staffinghub.coding.challenges.mapping.models.db.Image
import com.staffinghub.coding.challenges.mapping.models.db.ImageSize
import com.staffinghub.coding.challenges.mapping.models.db.blocks.ArticleBlock
import org.mapstruct.ObjectFactory
import org.springframework.stereotype.Component
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.TextBlock as TextBlockDto
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.VideoBlock as VideoBlockDto
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.ImageBlock as ImageBlockDto
import com.staffinghub.coding.challenges.mapping.models.db.blocks.*
import com.staffinghub.coding.challenges.mapping.models.dto.ImageDto

@Component
class ArticleBlockDtoFactory {
@ObjectFactory
fun createArticleBlockDto(articleBlock: ArticleBlock): ArticleBlockDto {
return when (articleBlock) {
is TextBlock -> TextBlockDto(
text = articleBlock.text,
sortIndex = articleBlock.sortIndex
)

is ImageBlock -> {
ImageBlockDto(
image = articleBlock.image?.let { mapToImageDto(it) } ?: ImageDto(-1, "", ImageSize.SMALL),
sortIndex = articleBlock.sortIndex
)
}

is VideoBlock -> VideoBlockDto(
url = articleBlock.url,
type = articleBlock.type,
sortIndex = articleBlock.sortIndex
)

is GalleryBlock -> GalleryBlockDto(
images = articleBlock.images.mapNotNull { mapToImageDto(it) } ?: emptyList(),
sortIndex = articleBlock.sortIndex
)

else -> throw IllegalArgumentException("Unknown ArticleBlock type")
}
}

private fun mapToImageDto(image: Image?): ImageDto? {
return image?.let {
ImageDto(
id = it.id,
url = it.url,
imageSize = it.imageSize
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ package com.staffinghub.coding.challenges.mapping.mappers

import com.staffinghub.coding.challenges.mapping.models.db.Article
import com.staffinghub.coding.challenges.mapping.models.dto.ArticleDto
import mapper.adaptTo
import org.mapstruct.factory.Mappers
import org.springframework.stereotype.Component
import java.util.*

@Component
class ArticleMapper {
fun map(article: Article?): ArticleDto {
//TODO
return ArticleDto(0, "", "", "", emptyList())
var mapperStruct: ArticleMapperMapstruct = Mappers.getMapper(ArticleMapperMapstruct::class.java)
fun map(article: Article): ArticleDto {
return article.adaptTo(ArticleDto::class)
}

/**
* Object mapper using Mapstruct as alternative to Mapster Dependency
*/
fun mapStructMap(article: Article): ArticleDto {
return mapperStruct.mapToArticleDto(article)
}

// Not part of the challenge / Nicht Teil dieser Challenge.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.staffinghub.coding.challenges.mapping.mappers

import com.staffinghub.coding.challenges.mapping.models.db.Article
import com.staffinghub.coding.challenges.mapping.models.db.blocks.*
import com.staffinghub.coding.challenges.mapping.models.dto.ArticleDto
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.ArticleBlockDto
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.ArticleBlockDtoFactory
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.GalleryBlockDto
import org.mapstruct.*
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.TextBlock as TextBlockDto
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.VideoBlock as VideoBlockDto
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.ImageBlock as ImageBlockDto
import org.springframework.stereotype.Component

@Component
@Mapper(uses = [ArticleBlockDtoFactory::class])
interface ArticleMapperMapstruct {

fun mapToGalleryBlockDto(gallery: GalleryBlock): GalleryBlockDto

fun mapToTextBlockDto(textBlock: TextBlock): TextBlockDto

fun mapToImageBlockDto(imageBlock: ImageBlock): ImageBlockDto

fun mapToVideoBlockDto(videoBlock: VideoBlock): VideoBlockDto

fun mapToArticleDto(article: Article): ArticleDto {
return ArticleDto(
id = article.id,
title = article.title,
description = article.description.toString(),
author = article.author.toString(),
blocks = mapBlocksToDto(article.blocks)
)
}

@IterableMapping(elementTargetType = ArticleBlockDto::class)
fun mapBlocksToDto(blocks: Collection<ArticleBlock>): Collection<ArticleBlockDto> {
return blocks.map { mapToArticleBlockDto(it) }
}

fun mapToArticleBlockDto(articleBlock: ArticleBlock): ArticleBlockDto {
return when (articleBlock) {
is TextBlock -> mapToTextBlockDto(articleBlock)
is ImageBlock -> mapToImageBlockDto(articleBlock)
is VideoBlock -> mapToVideoBlockDto(articleBlock)
is GalleryBlock -> mapToGalleryBlockDto(articleBlock)
else -> throw IllegalArgumentException("Unknown ArticleBlock type")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.staffinghub.coding.challenges.mapping.models.dto.blocks

interface ArticleBlockDto {
interface ArticleBlockDto : Comparable<ArticleBlockDto> {
val sortIndex: Int
override fun compareTo(other: ArticleBlockDto): Int {
return compareValuesBy(this, other) { it.sortIndex }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ import com.staffinghub.coding.challenges.mapping.models.dto.ImageDto
data class GalleryBlockDto(
var images: List<ImageDto>,
override val sortIndex: Int,
) : ArticleBlockDto
) : ArticleBlockDto, Comparable<ArticleBlockDto> {
override fun compareTo(other: ArticleBlockDto): Int {
return compareValuesBy(this, other) { it.sortIndex }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ import com.staffinghub.coding.challenges.mapping.models.dto.ImageDto
data class ImageBlock(
var image: ImageDto,
override val sortIndex: Int,
) : ArticleBlockDto
) : ArticleBlockDto, Comparable<ArticleBlockDto> {
override fun compareTo(other: ArticleBlockDto): Int {
return compareValuesBy(this, other) { it.sortIndex }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ package com.staffinghub.coding.challenges.mapping.models.dto.blocks
data class TextBlock(
var text: String,
override val sortIndex: Int,
) : ArticleBlockDto
) : ArticleBlockDto, Comparable<ArticleBlockDto> {
override fun compareTo(other: ArticleBlockDto): Int {
return compareValuesBy(this, other) { it.sortIndex }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ data class VideoBlock(
var url: String,
var type: VideoBlockType,
override val sortIndex: Int,
) : ArticleBlockDto
) : ArticleBlockDto, Comparable<ArticleBlockDto> {
override fun compareTo(other: ArticleBlockDto): Int {
return compareValuesBy(this, other) { it.sortIndex }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object ArticleRepository {
title = "Article Nr.: $this",
description = "Article Description $this",
author = "Max Mustermann",
blocks = dummyArticleBlocks,
blocks = dummyArticleBlocks.sortedWith(compareBy { it.sortIndex }).toSet(),
)

private val Long.dummyArticleBlocks: Set<ArticleBlock> by lazy {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package com.staffinghub.coding.challenges.mapping.services

import com.staffinghub.coding.challenges.mapping.repositories.ArticleRepository
import com.staffinghub.coding.challenges.mapping.mappers.ArticleMapper
import com.staffinghub.coding.challenges.mapping.models.dto.ArticleDto
import com.staffinghub.coding.challenges.mapping.repositories.ArticleRepository
import mapper.adaptListTo
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException

@Service
class ArticleService(
private val mapper: ArticleMapper,
) {
/**
*@return List of AriticleDto that have been mapped to the data opject with internal Blocks sorted by sortIndex
*/
fun list(): List<ArticleDto> {
val articles = ArticleRepository.all()
//TODO
return emptyList()
return articles.adaptListTo(ArticleDto::class).toList()
}

/**
*@return Single Instance of AriticleDto that have been mapped to the data opject with internal Blocks sorted by sortIndex
*/
fun articleForId(id: Long): ArticleDto {
val article = ArticleRepository.findBy(id)
//TODO
return ArticleDto(0, "", "", "", emptyList())

if (article == null)
throw ResponseStatusException(HttpStatus.NOT_FOUND, "No Article with suplied ID found")

return mapper.mapStructMap(article)
}

fun create(articleDto: ArticleDto): ArticleDto {
Expand Down
Loading