From 2a5bbe5b57aa6e1a5a2a4f8c5aec1a063d3adf9a Mon Sep 17 00:00:00 2001 From: Florian THIERRY Date: Mon, 14 Apr 2025 14:55:55 +0200 Subject: [PATCH] Add all layers implementation. --- .../application/product/ProductUseCases.kt | 8 +++- demo-exposition/build.gradle.kts | 13 ++++++ .../exposition/product/ProductController.kt | 16 ++++++++ .../exposition/product/model/ProductDto.kt | 22 ++++++++++ .../product/model/ProductTypeDto.kt | 25 +++++++++++ demo-infrastructure/build.gradle.kts | 12 ++++++ .../product/ProductInMemoryAdapter.kt | 41 +++++++++++++++++++ demo-launcher/build.gradle.kts | 4 ++ .../demo/launcher/ApplicationLauncher.kt | 13 +++++- settings.gradle.kts | 2 + 10 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 demo-exposition/build.gradle.kts create mode 100644 demo-exposition/src/main/kotlin/com/example/demo/exposition/product/ProductController.kt create mode 100644 demo-exposition/src/main/kotlin/com/example/demo/exposition/product/model/ProductDto.kt create mode 100644 demo-exposition/src/main/kotlin/com/example/demo/exposition/product/model/ProductTypeDto.kt create mode 100644 demo-infrastructure/build.gradle.kts create mode 100644 demo-infrastructure/src/main/kotlin/com/example/demo/infrastructure/product/ProductInMemoryAdapter.kt diff --git a/demo-application/src/main/kotlin/com/example/demo/application/product/ProductUseCases.kt b/demo-application/src/main/kotlin/com/example/demo/application/product/ProductUseCases.kt index c6fba53..30b22ae 100644 --- a/demo-application/src/main/kotlin/com/example/demo/application/product/ProductUseCases.kt +++ b/demo-application/src/main/kotlin/com/example/demo/application/product/ProductUseCases.kt @@ -1,8 +1,12 @@ package com.example.demo.application.product +import com.example.demo.domain.product.model.Product +import com.example.demo.domain.product.port.ProductPort import org.springframework.stereotype.Service @Service -class ProductUseCases { - +class ProductUseCases( + private val productPort: ProductPort +) { + fun getAll(): List = productPort.getAll() } \ No newline at end of file diff --git a/demo-exposition/build.gradle.kts b/demo-exposition/build.gradle.kts new file mode 100644 index 0000000..f2632e5 --- /dev/null +++ b/demo-exposition/build.gradle.kts @@ -0,0 +1,13 @@ +plugins { + kotlin("jvm") + id("io.spring.dependency-management") version "1.1.7" + id("java") +} + +dependencies { + implementation(kotlin("stdlib")) + implementation(project(":demo-application")) + implementation(project(":demo-domain")) + implementation("org.springframework:spring-context") + implementation("org.springframework:spring-web") +} \ No newline at end of file diff --git a/demo-exposition/src/main/kotlin/com/example/demo/exposition/product/ProductController.kt b/demo-exposition/src/main/kotlin/com/example/demo/exposition/product/ProductController.kt new file mode 100644 index 0000000..8f91ed4 --- /dev/null +++ b/demo-exposition/src/main/kotlin/com/example/demo/exposition/product/ProductController.kt @@ -0,0 +1,16 @@ +package com.example.demo.exposition.product + +import com.example.demo.application.product.ProductUseCases +import com.example.demo.exposition.product.model.ProductDto +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/api/products") +class ProductController( + private val productUseCases: ProductUseCases +) { + @GetMapping + fun getAll(): List = productUseCases.getAll().map(::ProductDto) +} \ No newline at end of file diff --git a/demo-exposition/src/main/kotlin/com/example/demo/exposition/product/model/ProductDto.kt b/demo-exposition/src/main/kotlin/com/example/demo/exposition/product/model/ProductDto.kt new file mode 100644 index 0000000..10e7367 --- /dev/null +++ b/demo-exposition/src/main/kotlin/com/example/demo/exposition/product/model/ProductDto.kt @@ -0,0 +1,22 @@ +package com.example.demo.exposition.product.model + +import com.example.demo.domain.product.model.Product +import java.util.* + +data class ProductDto( + val id: UUID, + val name: String, + val type: ProductTypeDto +) { + constructor(product: Product): this( + id = product.id, + name = product.name, + type = ProductTypeDto.fromDomain(product.type) + ) + + fun toDomain() = Product( + id, + name, + type = type.toDomain() + ) +} diff --git a/demo-exposition/src/main/kotlin/com/example/demo/exposition/product/model/ProductTypeDto.kt b/demo-exposition/src/main/kotlin/com/example/demo/exposition/product/model/ProductTypeDto.kt new file mode 100644 index 0000000..1e8509f --- /dev/null +++ b/demo-exposition/src/main/kotlin/com/example/demo/exposition/product/model/ProductTypeDto.kt @@ -0,0 +1,25 @@ +package com.example.demo.exposition.product.model + +import com.example.demo.domain.product.model.ProductType + +enum class ProductTypeDto { + SOLID, + LIQUID, + GAS; + + fun toDomain(): ProductType { + return when (this) { + SOLID -> ProductType.SOLID + LIQUID -> ProductType.LIQUID + GAS -> ProductType.GAS + } + } + + companion object { + fun fromDomain(productType: ProductType) = when (productType) { + ProductType.SOLID -> SOLID + ProductType.LIQUID -> LIQUID + ProductType.GAS -> GAS + } + } +} \ No newline at end of file diff --git a/demo-infrastructure/build.gradle.kts b/demo-infrastructure/build.gradle.kts new file mode 100644 index 0000000..28d5844 --- /dev/null +++ b/demo-infrastructure/build.gradle.kts @@ -0,0 +1,12 @@ +plugins { + kotlin("jvm") + id("io.spring.dependency-management") version "1.1.7" + id("java") +} + +dependencies { + implementation(kotlin("stdlib")) + implementation(project(":demo-application")) + implementation(project(":demo-domain")) + implementation("org.springframework:spring-context") +} \ No newline at end of file diff --git a/demo-infrastructure/src/main/kotlin/com/example/demo/infrastructure/product/ProductInMemoryAdapter.kt b/demo-infrastructure/src/main/kotlin/com/example/demo/infrastructure/product/ProductInMemoryAdapter.kt new file mode 100644 index 0000000..df007f1 --- /dev/null +++ b/demo-infrastructure/src/main/kotlin/com/example/demo/infrastructure/product/ProductInMemoryAdapter.kt @@ -0,0 +1,41 @@ +package com.example.demo.infrastructure.product + +import com.example.demo.domain.product.model.Product +import com.example.demo.domain.product.model.ProductType.LIQUID +import com.example.demo.domain.product.model.ProductType.SOLID +import com.example.demo.domain.product.port.ProductPort +import org.springframework.stereotype.Component +import java.util.* + +@Component +class ProductInMemoryAdapter : ProductPort { + private val products = mutableListOf( + Product( + id = UUID.randomUUID(), + name = "Banana x5", + type = SOLID + ), + Product( + id = UUID.randomUUID(), + name = "Banana juice 1L", + type = LIQUID + ) + ) + + override fun getById(id: UUID): Product? = products.find { product -> product.id == id } + + override fun getAll(): List = products + + override fun save(product: Product) { + if (getById(product.id) == null) { + products.add(product) + } else { + deleteById(product.id) + save(product) + } + } + + override fun deleteById(id: UUID) { + products.removeIf { product -> product.id == id } + } +} \ No newline at end of file diff --git a/demo-launcher/build.gradle.kts b/demo-launcher/build.gradle.kts index cb7b30d..94a1c4a 100644 --- a/demo-launcher/build.gradle.kts +++ b/demo-launcher/build.gradle.kts @@ -7,6 +7,10 @@ plugins { dependencies { implementation(kotlin("stdlib")) + implementation(project(":demo-domain")) + implementation(project(":demo-application")) + implementation(project(":demo-infrastructure")) + implementation(project(":demo-exposition")) implementation("org.springframework.boot:spring-boot-starter-web") } diff --git a/demo-launcher/src/main/kotlin/com/example/demo/launcher/ApplicationLauncher.kt b/demo-launcher/src/main/kotlin/com/example/demo/launcher/ApplicationLauncher.kt index 5a10054..0e6270c 100644 --- a/demo-launcher/src/main/kotlin/com/example/demo/launcher/ApplicationLauncher.kt +++ b/demo-launcher/src/main/kotlin/com/example/demo/launcher/ApplicationLauncher.kt @@ -1,11 +1,20 @@ package com.example.demo.launcher +import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication -@SpringBootApplication +@SpringBootApplication( + scanBasePackages = [ + "com.example.demo.domain", + "com.example.demo.application", + "com.example.demo.infrastructure", + "com.example.demo.exposition" + ] +) +@EnableAutoConfiguration open class DemoApplication fun main(args: Array) { runApplication(*args) -} +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 108a368..df3ed54 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,4 +1,6 @@ rootProject.name = "demo" include(":demo-domain") include(":demo-application") +include(":demo-infrastructure") +include(":demo-exposition") include(":demo-launcher")