Allow room upsert by roomTypeCode
All checks were successful
build-and-deploy / build-deploy (push) Successful in 27s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 27s
This commit is contained in:
@@ -176,8 +176,7 @@ class Rooms(
|
||||
val property = propertyRepo.findById(propertyId).orElseThrow {
|
||||
ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||
}
|
||||
val roomType = roomTypeRepo.findByIdAndPropertyId(request.roomTypeId, propertyId)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Room type not found")
|
||||
val roomType = resolveRoomType(propertyId, request)
|
||||
|
||||
val room = Room(
|
||||
property = property,
|
||||
@@ -212,8 +211,7 @@ class Rooms(
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Room number already exists for property")
|
||||
}
|
||||
|
||||
val roomType = roomTypeRepo.findByIdAndPropertyId(request.roomTypeId, propertyId)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Room type not found")
|
||||
val roomType = resolveRoomType(propertyId, request)
|
||||
|
||||
room.roomNumber = request.roomNumber
|
||||
room.floor = request.floor
|
||||
@@ -247,6 +245,20 @@ class Rooms(
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid date format")
|
||||
}
|
||||
}
|
||||
|
||||
private fun resolveRoomType(propertyId: UUID, request: RoomUpsertRequest): com.android.trisolarisserver.models.room.RoomType {
|
||||
request.roomTypeId?.let { id ->
|
||||
roomTypeRepo.findByIdAndPropertyId(id, propertyId)?.let { return it }
|
||||
}
|
||||
val code = request.roomTypeCode?.trim()
|
||||
if (!code.isNullOrBlank()) {
|
||||
roomTypeRepo.findByPropertyIdAndCodeIgnoreCase(propertyId, code)?.let { return it }
|
||||
}
|
||||
if (request.roomTypeId == null && code.isNullOrBlank()) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "roomTypeId or roomTypeCode required")
|
||||
}
|
||||
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Room type not found")
|
||||
}
|
||||
}
|
||||
|
||||
private fun Room.toRoomResponse(): RoomResponse {
|
||||
|
||||
@@ -52,7 +52,8 @@ enum class RoomBoardStatus {
|
||||
data class RoomUpsertRequest(
|
||||
val roomNumber: Int,
|
||||
val floor: Int?,
|
||||
val roomTypeId: UUID,
|
||||
val roomTypeId: UUID? = null,
|
||||
val roomTypeCode: String? = null,
|
||||
val hasNfc: Boolean,
|
||||
val active: Boolean,
|
||||
val maintenance: Boolean,
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.UUID
|
||||
|
||||
interface RoomTypeRepo : JpaRepository<RoomType, UUID> {
|
||||
fun findByIdAndPropertyId(id: UUID, propertyId: UUID): RoomType?
|
||||
fun findByPropertyIdAndCodeIgnoreCase(propertyId: UUID, code: String): RoomType?
|
||||
@EntityGraph(attributePaths = ["property"])
|
||||
fun findByPropertyIdOrderByCode(propertyId: UUID): List<RoomType>
|
||||
fun existsByPropertyIdAndCode(propertyId: UUID, code: String): Boolean
|
||||
|
||||
Reference in New Issue
Block a user