Delete amenities by unlinking from room types
All checks were successful
build-and-deploy / build-deploy (push) Successful in 1m54s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 1m54s
This commit is contained in:
@@ -86,6 +86,7 @@ class RoomAmenities(
|
||||
|
||||
@DeleteMapping("/{amenityId}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
@org.springframework.transaction.annotation.Transactional
|
||||
fun deleteAmenity(
|
||||
@PathVariable amenityId: UUID,
|
||||
@AuthenticationPrincipal principal: MyPrincipal?
|
||||
@@ -95,8 +96,12 @@ class RoomAmenities(
|
||||
val amenity = roomAmenityRepo.findById(amenityId).orElse(null)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Amenity not found")
|
||||
|
||||
if (roomTypeRepo.existsByAmenitiesId(amenityId)) {
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Amenity is used by room types")
|
||||
val roomTypes = roomTypeRepo.findAllByAmenitiesId(amenityId)
|
||||
if (roomTypes.isNotEmpty()) {
|
||||
for (roomType in roomTypes) {
|
||||
roomType.amenities.removeIf { it.id == amenityId }
|
||||
}
|
||||
roomTypeRepo.saveAll(roomTypes)
|
||||
}
|
||||
roomAmenityRepo.delete(amenity)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user