Implementation of getAll and create products endpoints.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user