Implementation of getAll and create products endpoints.

This commit is contained in:
Florian THIERRY
2025-04-23 23:13:48 +02:00
parent ed0acfc5dc
commit f98e1227e8
14 changed files with 136 additions and 31 deletions

View File

@@ -1,12 +1,45 @@
package com.example.demo.application.product
import com.example.demo.domain.core.error.FunctionalError
import com.example.demo.domain.product.inputport.ProductInputPort
import com.example.demo.domain.product.model.Product
import com.example.demo.domain.product.port.ProductPort
import com.example.demo.domain.product.model.ProductType
import com.example.demo.domain.product.outputport.ProductOutputPort
import com.github.michaelbull.result.*
import com.github.michaelbull.result.coroutines.coroutineBinding
import org.springframework.stereotype.Service
import java.util.*
@Service
class ProductUseCases(
private val productPort: ProductPort
) {
fun getAll(): List<Product> = productPort.getAll()
private val productOutputPort: ProductOutputPort
) : ProductInputPort {
override suspend fun getAll(): Result<List<Product>, FunctionalError> = productOutputPort.getAll()
.mapError { FunctionalError(it.message) }
// override suspend fun create(name: String, type: ProductType): Result<Product, FunctionalError> {
// if (name.isBlank()) {
// return Err(FunctionalError("Product name should be set."))
// }
//
// val newProduct = Product(id = UUID.randomUUID(), name, type)
// return productOutputPort.save(newProduct)
// .mapError { FunctionalError(it.message) }
// .map { newProduct }
// }
override suspend fun create(name: String, type: ProductType): Result<Product, FunctionalError> = coroutineBinding {
validateName(name).bind()
val newProduct = Product(id = UUID.randomUUID(), name, type)
productOutputPort.save(newProduct)
.mapError { FunctionalError(it.message) }
.map { newProduct }
.bind()
}
private fun validateName(name: String): Result<Unit, FunctionalError> = when {
name.isBlank() -> Err(FunctionalError("Product name should be set."))
else -> Ok(Unit)
}
}