Add all layers implementation.

This commit is contained in:
Florian THIERRY
2025-04-14 14:55:55 +02:00
parent 8fa13103f3
commit db7bcfa60a
10 changed files with 152 additions and 4 deletions

View File

@@ -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<Product> = productPort.getAll()
}

View File

@@ -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")
}

View File

@@ -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<ProductDto> = productUseCases.getAll().map(::ProductDto)
}

View File

@@ -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()
)
}

View File

@@ -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
}
}
}

View File

@@ -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")
}

View File

@@ -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>(
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<Product> = 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 }
}
}

View File

@@ -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")
}

View File

@@ -1,9 +1,18 @@
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<String>) {

View File

@@ -1,4 +1,6 @@
rootProject.name = "demo"
include(":demo-domain")
include(":demo-application")
include(":demo-infrastructure")
include(":demo-exposition")
include(":demo-launcher")