update addDevice to handle violates of foreign key in DeviceService
This commit is contained in:
@@ -5,6 +5,8 @@ import java.time.ZonedDateTime
|
|||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import ltd.hlaeja.entity.DeviceEntity
|
import ltd.hlaeja.entity.DeviceEntity
|
||||||
import ltd.hlaeja.repository.DeviceRepository
|
import ltd.hlaeja.repository.DeviceRepository
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException
|
||||||
|
import org.springframework.http.HttpStatus.BAD_REQUEST
|
||||||
import org.springframework.http.HttpStatus.NOT_FOUND
|
import org.springframework.http.HttpStatus.NOT_FOUND
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import org.springframework.web.server.ResponseStatusException
|
import org.springframework.web.server.ResponseStatusException
|
||||||
@@ -18,8 +20,13 @@ class DeviceService(
|
|||||||
|
|
||||||
suspend fun addDevice(
|
suspend fun addDevice(
|
||||||
type: UUID,
|
type: UUID,
|
||||||
): DeviceEntity = deviceRepository.save(DeviceEntity(null, ZonedDateTime.now(), type))
|
): DeviceEntity = try {
|
||||||
|
deviceRepository.save(DeviceEntity(null, ZonedDateTime.now(), type))
|
||||||
.also { log.debug { "Added device ${it.id}" } }
|
.also { log.debug { "Added device ${it.id}" } }
|
||||||
|
} catch (e: DataIntegrityViolationException) {
|
||||||
|
log.warn { e.localizedMessage }
|
||||||
|
throw ResponseStatusException(BAD_REQUEST)
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun getDevice(device: UUID): DeviceEntity = deviceRepository.findById(device)
|
suspend fun getDevice(device: UUID): DeviceEntity = deviceRepository.findById(device)
|
||||||
?.also { log.debug { "Get device ${it.id}" } }
|
?.also { log.debug { "Get device ${it.id}" } }
|
||||||
|
|||||||
Reference in New Issue
Block a user