Add addType to TypeService

This commit is contained in:
2024-11-19 12:43:39 +01:00
parent b5d1c04d6b
commit 2a98cdf38f
3 changed files with 63 additions and 0 deletions

View File

@@ -3,7 +3,13 @@ package ltd.hlaeja.service
import kotlinx.coroutines.flow.Flow
import ltd.hlaeja.entity.TypeEntity
import ltd.hlaeja.repository.TypeRepository
import mu.KotlinLogging
import org.springframework.dao.DuplicateKeyException
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException
private val log = KotlinLogging.logger {}
@Service
class TypeService(
@@ -11,4 +17,14 @@ class TypeService(
) {
fun getTypes(): Flow<TypeEntity> = typeRepository.findAll()
suspend fun addType(
entity: TypeEntity,
): TypeEntity = try {
typeRepository.save(entity)
.also { log.debug("Added new type: {}", it.id) }
} catch (e: DuplicateKeyException) {
log.warn(e.localizedMessage)
throw ResponseStatusException(HttpStatus.CONFLICT)
}
}