From 6c567ffa593a54892f0b7695ca467facdc3e6de2 Mon Sep 17 00:00:00 2001 From: pgrondek Date: Fri, 24 Jul 2020 01:25:41 +0200 Subject: [PATCH] Hexagon + some implementation --- adapter-incoming-rest/build.gradle.kts | 7 ++++ .../fitapp/adapter/rest/WeightController.kt | 29 +++++++++++++++ adapter-repository-mongodb/build.gradle.kts | 8 +++++ .../adapter/mongo/WeightCrudRepository.kt | 6 ++++ .../fitapp/adapter/mongo/WeightMapper.kt | 24 +++++++++++++ .../adapter/mongo/WeightMongoRepository.kt | 35 +++++++++++++++++++ .../grondek/fitapp/adapter/mongo/WeightRO.kt | 14 ++++++++ build.gradle.kts | 35 +++++++++++++++---- core/build.gradle.kts | 7 ++++ .../grondek/fitapp/core/WeightsServiceImpl.kt | 22 ++++++++++++ model/build.gradle.kts | 0 .../grondek/fitapp/model/CreateWeightDTO.kt | 9 +++++ .../kotlin/pl/grondek/fitapp/model/Weight.kt | 9 +++++ ports-incoming/build.gradle.kts | 5 +++ .../fitapp/port/incoming/WeightService.kt | 13 +++++++ ports-repository/build.gradle.kts | 5 +++ .../fitapp/repository/WeightRepository.kt | 17 +++++++++ settings.gradle.kts | 7 ++++ 18 files changed, 245 insertions(+), 7 deletions(-) create mode 100644 adapter-incoming-rest/build.gradle.kts create mode 100644 adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightController.kt create mode 100644 adapter-repository-mongodb/build.gradle.kts create mode 100644 adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightCrudRepository.kt create mode 100644 adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMapper.kt create mode 100644 adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMongoRepository.kt create mode 100644 adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightRO.kt create mode 100644 core/build.gradle.kts create mode 100644 core/src/main/kotlin/pl/grondek/fitapp/core/WeightsServiceImpl.kt create mode 100644 model/build.gradle.kts create mode 100644 model/src/main/kotlin/pl/grondek/fitapp/model/CreateWeightDTO.kt create mode 100644 model/src/main/kotlin/pl/grondek/fitapp/model/Weight.kt create mode 100644 ports-incoming/build.gradle.kts create mode 100644 ports-incoming/src/main/kotlin/pl/grondek/fitapp/port/incoming/WeightService.kt create mode 100644 ports-repository/build.gradle.kts create mode 100644 ports-repository/src/main/kotlin/pl/grondek/fitapp/repository/WeightRepository.kt diff --git a/adapter-incoming-rest/build.gradle.kts b/adapter-incoming-rest/build.gradle.kts new file mode 100644 index 0000000..5879445 --- /dev/null +++ b/adapter-incoming-rest/build.gradle.kts @@ -0,0 +1,7 @@ +dependencies { + implementation("org.springframework.boot:spring-boot-starter-webflux") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + + implementation(project(":model")) + implementation(project(":ports-incoming")) +} \ 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 new file mode 100644 index 0000000..55c9cd4 --- /dev/null +++ b/adapter-incoming-rest/src/main/kotlin/pl/grondek/fitapp/adapter/rest/WeightController.kt @@ -0,0 +1,29 @@ +package pl.grondek.fitapp.adapter.rest + +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import pl.grondek.fitapp.model.CreateWeightDTO +import pl.grondek.fitapp.model.Weight +import pl.grondek.fitapp.port.incoming.WeightService +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono + +@RestController +@RequestMapping("/weight") +class WeightController( + private val weightService: WeightService +) { + + @GetMapping + fun listAll(): Flux{ + return weightService.listAll() + } + + @PostMapping + fun save(@RequestBody createWeightDTO: CreateWeightDTO): Mono { + return weightService.add(createWeightDTO) + } +} \ No newline at end of file diff --git a/adapter-repository-mongodb/build.gradle.kts b/adapter-repository-mongodb/build.gradle.kts new file mode 100644 index 0000000..0b10f1b --- /dev/null +++ b/adapter-repository-mongodb/build.gradle.kts @@ -0,0 +1,8 @@ +dependencies { + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + + implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive") + + 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/WeightCrudRepository.kt b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightCrudRepository.kt new file mode 100644 index 0000000..86e6a7f --- /dev/null +++ b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightCrudRepository.kt @@ -0,0 +1,6 @@ +package pl.grondek.fitapp.adapter.mongo + +import org.springframework.data.repository.reactive.ReactiveCrudRepository + +interface WeightCrudRepository : ReactiveCrudRepository { +} \ 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 new file mode 100644 index 0000000..f193571 --- /dev/null +++ b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMapper.kt @@ -0,0 +1,24 @@ +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 new file mode 100644 index 0000000..bc944d6 --- /dev/null +++ b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightMongoRepository.kt @@ -0,0 +1,35 @@ +package pl.grondek.fitapp.adapter.mongo + +import org.springframework.stereotype.Component +import pl.grondek.fitapp.model.CreateWeightDTO +import pl.grondek.fitapp.model.Weight +import pl.grondek.fitapp.repository.WeightRepository +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono + +@Component +class WeightMongoRepository( + private val weightCrudRepository: WeightCrudRepository, + private val mapper: WeightMapper +) : WeightRepository { + + override fun add(createWeightDTO: CreateWeightDTO): Mono { + val entity = mapper.map(createWeightDTO) + val savedMono = weightCrudRepository.save(entity) + return savedMono.map(mapper::map) + } + + override fun listAll(): Flux { + return weightCrudRepository.findAll() + .map(mapper::map) + } + + override fun getLatest(): Mono { + TODO("Not yet implemented") + } + + override fun getPrevious(): Mono { + TODO("Not yet implemented") + } + +} \ No newline at end of file diff --git a/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightRO.kt b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightRO.kt new file mode 100644 index 0000000..5458a9d --- /dev/null +++ b/adapter-repository-mongodb/src/main/kotlin/pl/grondek/fitapp/adapter/mongo/WeightRO.kt @@ -0,0 +1,14 @@ +package pl.grondek.fitapp.adapter.mongo + +import org.springframework.data.annotation.Id +import java.math.BigDecimal +import java.time.LocalDateTime + +data class WeightRO( + @field:Id + val id: String?, + + val weight: BigDecimal, + + val date: LocalDateTime +) \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index f0c7a17..13d5753 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,17 +11,38 @@ group = "pl.grondek" version = "0.0.1-SNAPSHOT" java.sourceCompatibility = JavaVersion.VERSION_1_8 -repositories { - mavenCentral() +allprojects { + apply{ + plugin("io.spring.dependency-management") + } + apply{ + plugin("org.jetbrains.kotlin.jvm") + } + repositories { + mavenCentral() + mavenLocal() + } + + dependencyManagement { + imports { + mavenBom("org.springframework.boot:spring-boot-dependencies:2.3.1.RELEASE") + } + } + + dependencies{ + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + } } dependencies { - implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive") - implementation("org.springframework.boot:spring-boot-starter-webflux") - implementation("com.fasterxml.jackson.module:jackson-module-kotlin") - implementation("io.projectreactor.kotlin:reactor-kotlin-extensions") + implementation(project(":model")) + implementation(project(":core")) + implementation(project(":ports-repository")) + implementation(project(":adapter-repository-mongodb")) + implementation(project(":adapter-incoming-rest")) + + implementation("org.springframework.boot:spring-boot-starter") implementation("org.jetbrains.kotlin:kotlin-reflect") - implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor") testImplementation("org.springframework.boot:spring-boot-starter-test") { exclude(group = "org.junit.vintage", module = "junit-vintage-engine") diff --git a/core/build.gradle.kts b/core/build.gradle.kts new file mode 100644 index 0000000..0c7ebda --- /dev/null +++ b/core/build.gradle.kts @@ -0,0 +1,7 @@ +dependencies { + implementation(project(":ports-incoming")) + implementation(project(":model")) + implementation(project(":ports-repository")) + + api ("org.springframework:spring-context") +} \ No newline at end of file diff --git a/core/src/main/kotlin/pl/grondek/fitapp/core/WeightsServiceImpl.kt b/core/src/main/kotlin/pl/grondek/fitapp/core/WeightsServiceImpl.kt new file mode 100644 index 0000000..0a54b6b --- /dev/null +++ b/core/src/main/kotlin/pl/grondek/fitapp/core/WeightsServiceImpl.kt @@ -0,0 +1,22 @@ +package pl.grondek.fitapp.core + +import org.springframework.stereotype.Service +import pl.grondek.fitapp.model.CreateWeightDTO +import pl.grondek.fitapp.model.Weight +import pl.grondek.fitapp.port.incoming.WeightService +import pl.grondek.fitapp.repository.WeightRepository +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono + +@Service +class WeightsServiceImpl( + private val weightRepository: WeightRepository +):WeightService { + override fun add(createWeightDTO: CreateWeightDTO): Mono { + return weightRepository.add(createWeightDTO) + } + + override fun listAll(): Flux { + return weightRepository.listAll() + } +} \ No newline at end of file diff --git a/model/build.gradle.kts b/model/build.gradle.kts new file mode 100644 index 0000000..e69de29 diff --git a/model/src/main/kotlin/pl/grondek/fitapp/model/CreateWeightDTO.kt b/model/src/main/kotlin/pl/grondek/fitapp/model/CreateWeightDTO.kt new file mode 100644 index 0000000..da5a2eb --- /dev/null +++ b/model/src/main/kotlin/pl/grondek/fitapp/model/CreateWeightDTO.kt @@ -0,0 +1,9 @@ +package pl.grondek.fitapp.model + +import java.math.BigDecimal +import java.time.LocalDateTime + +data class CreateWeightDTO( + val weight: BigDecimal, + val date: LocalDateTime +) \ No newline at end of file diff --git a/model/src/main/kotlin/pl/grondek/fitapp/model/Weight.kt b/model/src/main/kotlin/pl/grondek/fitapp/model/Weight.kt new file mode 100644 index 0000000..4c6e2a8 --- /dev/null +++ b/model/src/main/kotlin/pl/grondek/fitapp/model/Weight.kt @@ -0,0 +1,9 @@ +package pl.grondek.fitapp.model + +import java.math.BigDecimal +import java.time.LocalDateTime + +data class Weight ( + val weight: BigDecimal, + val date: LocalDateTime +) \ No newline at end of file diff --git a/ports-incoming/build.gradle.kts b/ports-incoming/build.gradle.kts new file mode 100644 index 0000000..ffc94ba --- /dev/null +++ b/ports-incoming/build.gradle.kts @@ -0,0 +1,5 @@ +dependencies { + api("io.projectreactor:reactor-core") + + implementation(project(":model")) +} \ No newline at end of file diff --git a/ports-incoming/src/main/kotlin/pl/grondek/fitapp/port/incoming/WeightService.kt b/ports-incoming/src/main/kotlin/pl/grondek/fitapp/port/incoming/WeightService.kt new file mode 100644 index 0000000..1d4b944 --- /dev/null +++ b/ports-incoming/src/main/kotlin/pl/grondek/fitapp/port/incoming/WeightService.kt @@ -0,0 +1,13 @@ +package pl.grondek.fitapp.port.incoming + +import pl.grondek.fitapp.model.CreateWeightDTO +import pl.grondek.fitapp.model.Weight +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono + +interface WeightService { + fun add(createWeightDTO: CreateWeightDTO): Mono + + fun listAll(): Flux + +} \ No newline at end of file diff --git a/ports-repository/build.gradle.kts b/ports-repository/build.gradle.kts new file mode 100644 index 0000000..ffc94ba --- /dev/null +++ b/ports-repository/build.gradle.kts @@ -0,0 +1,5 @@ +dependencies { + api("io.projectreactor:reactor-core") + + implementation(project(":model")) +} \ No newline at end of file diff --git a/ports-repository/src/main/kotlin/pl/grondek/fitapp/repository/WeightRepository.kt b/ports-repository/src/main/kotlin/pl/grondek/fitapp/repository/WeightRepository.kt new file mode 100644 index 0000000..9e14846 --- /dev/null +++ b/ports-repository/src/main/kotlin/pl/grondek/fitapp/repository/WeightRepository.kt @@ -0,0 +1,17 @@ +package pl.grondek.fitapp.repository + +import pl.grondek.fitapp.model.CreateWeightDTO +import pl.grondek.fitapp.model.Weight +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono + +interface WeightRepository { + + fun add(createWeightDTO: CreateWeightDTO): Mono + + fun listAll(): Flux + + fun getLatest(): Mono + + fun getPrevious(): Mono +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index f14a72e..e12e05d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1 +1,8 @@ rootProject.name = "fitapp" + +include("model") +include("core") +include("ports-repository") +include("ports-incoming") +include("adapter-repository-mongodb") +include("adapter-incoming-rest") \ No newline at end of file