From 09e8ed05037913dae5d8032363b3a2330d529608 Mon Sep 17 00:00:00 2001 From: pgrondek Date: Fri, 24 Jul 2020 02:02:00 +0200 Subject: [PATCH] Mapstruct --- adapter-incoming-rest/build.gradle.kts | 9 +++++++ .../grondek/fitapp/adapter/rest/RestMapper.kt | 10 +++++++ .../fitapp/adapter/rest/WeightApiModel.kt | 9 +++++++ .../fitapp/adapter/rest/WeightController.kt | 6 +++-- adapter-repository-mongodb/build.gradle.kts | 12 +++++++-- .../fitapp/adapter/mongo/MongoMapper.kt | 16 ++++++++++++ .../fitapp/adapter/mongo/WeightMapper.kt | 24 ----------------- .../adapter/mongo/WeightMongoRepository.kt | 2 +- build.gradle.kts | 26 ++++++++++++++----- gradle.properties | 1 + 10 files changed, 80 insertions(+), 35 deletions(-) create mode 100644 adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/RestMapper.kt create mode 100644 adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightApiModel.kt create mode 100644 adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/MongoMapper.kt delete mode 100644 adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMapper.kt create mode 100644 gradle.properties diff --git a/adapter-incoming-rest/build.gradle.kts b/adapter-incoming-rest/build.gradle.kts index 5879445..17c7848 100644 --- a/adapter-incoming-rest/build.gradle.kts +++ b/adapter-incoming-rest/build.gradle.kts @@ -1,7 +1,16 @@ +val mapstructVersion: String by project + +plugins { + kotlin("kapt") +} + dependencies { implementation("org.springframework.boot:spring-boot-starter-webflux") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation(project(":model")) implementation(project(":ports-incoming")) + + implementation("org.mapstruct:mapstruct:$mapstructVersion") + kapt("org.mapstruct:mapstruct-processor:$mapstructVersion") } \ No newline at end of file diff --git a/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/RestMapper.kt b/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/RestMapper.kt new file mode 100644 index 0000000..014da08 --- /dev/null +++ b/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/RestMapper.kt @@ -0,0 +1,10 @@ +package pl.grondek.fitapp.adapter.rest + +import org.mapstruct.Mapper +import pl.grondek.fitapp.model.Weight + +@Mapper(componentModel = "spring") +interface RestMapper { + + fun map(domain: Weight): WeightApiModel +} \ No newline at end of file diff --git a/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightApiModel.kt b/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightApiModel.kt new file mode 100644 index 0000000..d6e7ad3 --- /dev/null +++ b/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightApiModel.kt @@ -0,0 +1,9 @@ +package pl.grondek.fitapp.adapter.rest + +import java.math.BigDecimal +import java.time.LocalDateTime + +data class WeightApiModel( + val weight: BigDecimal, + val date: LocalDateTime +) \ No newline at end of file diff --git a/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightController.kt b/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightController.kt index 55c9cd4..cb1e017 100644 --- a/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightController.kt +++ b/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightController.kt @@ -14,12 +14,14 @@ import reactor.core.publisher.Mono @RestController @RequestMapping("/weight") class WeightController( - private val weightService: WeightService + private val weightService: WeightService, + private val mapper: RestMapper ) { @GetMapping - fun listAll(): Flux{ + fun listAll(): Flux{ return weightService.listAll() + .map(mapper::map) } @PostMapping diff --git a/adapter-repository-mongodb/build.gradle.kts b/adapter-repository-mongodb/build.gradle.kts index 0b10f1b..7fb5d33 100644 --- a/adapter-repository-mongodb/build.gradle.kts +++ b/adapter-repository-mongodb/build.gradle.kts @@ -1,8 +1,16 @@ -dependencies { - implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") +val mapstructVersion: String by project +plugins { + kotlin("kapt") +} + +dependencies { implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive") + implementation("org.mapstruct:mapstruct:$mapstructVersion") + kapt("org.mapstruct:mapstruct-processor:$mapstructVersion") + implementation(project(":model")) implementation(project(":ports-repository")) + } \ No newline at end of file diff --git a/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/MongoMapper.kt b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/MongoMapper.kt new file mode 100644 index 0000000..795601f --- /dev/null +++ b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/MongoMapper.kt @@ -0,0 +1,16 @@ +package pl.grondek.fitapp.adapter.mongo + +import org.mapstruct.Mapper +import org.mapstruct.Mapping +import org.springframework.stereotype.Component +import pl.grondek.fitapp.model.CreateWeightDTO +import pl.grondek.fitapp.model.Weight +import reactor.core.publisher.Mono + +@Mapper(componentModel = "spring") +interface MongoMapper { + @Mapping(target = "id", ignore = true) + fun map(createWeightDTO: CreateWeightDTO): WeightRO + + fun map(weightRO: WeightRO): Weight +} \ No newline at end of file diff --git a/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMapper.kt b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMapper.kt deleted file mode 100644 index f193571..0000000 --- a/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMapper.kt +++ /dev/null @@ -1,24 +0,0 @@ -package pl.grondek.fitapp.adapter.mongo - -import org.springframework.stereotype.Component -import pl.grondek.fitapp.model.CreateWeightDTO -import pl.grondek.fitapp.model.Weight -import reactor.core.publisher.Mono - -@Component -class WeightMapper { - fun map(createWeightDTO: CreateWeightDTO): WeightRO { - return WeightRO( - id = null, - weight = createWeightDTO.weight, - date = createWeightDTO.date - ) - } - - fun map(weightRO: WeightRO): Weight { - return Weight( - weight = weightRO.weight, - date = weightRO.date - ) - } -} \ No newline at end of file diff --git a/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMongoRepository.kt b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMongoRepository.kt index bc944d6..3fbb576 100644 --- a/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMongoRepository.kt +++ b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMongoRepository.kt @@ -10,7 +10,7 @@ import reactor.core.publisher.Mono @Component class WeightMongoRepository( private val weightCrudRepository: WeightCrudRepository, - private val mapper: WeightMapper + private val mapper: MongoMapper ) : WeightRepository { override fun add(createWeightDTO: CreateWeightDTO): Mono { diff --git a/build.gradle.kts b/build.gradle.kts index 13d5753..aed737e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,6 +4,7 @@ plugins { id("org.springframework.boot") version "2.3.1.RELEASE" id("io.spring.dependency-management") version "1.0.9.RELEASE" kotlin("jvm") version "1.3.72" + kotlin("kapt") version "1.3.72" kotlin("plugin.spring") version "1.3.72" } @@ -32,6 +33,16 @@ allprojects { dependencies{ implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") } + + tasks.withType { + kotlinOptions { + freeCompilerArgs = listOf( + "-Xjsr305=strict" + ) + jvmTarget = "1.8" + javaParameters = true + } + } } dependencies { @@ -54,9 +65,12 @@ tasks.withType { useJUnitPlatform() } -tasks.withType { - kotlinOptions { - freeCompilerArgs = listOf("-Xjsr305=strict") - jvmTarget = "1.8" - } -} +//tasks.withType { +// kotlinOptions { +// freeCompilerArgs = listOf( +// "-Xjsr305=strict", +// ) +// jvmTarget = "1.8" +// javaParameters = true +// } +//} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..0074e46 --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +mapstructVersion=1.4.0.Beta3 \ No newline at end of file