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}")
|
@DeleteMapping("/{amenityId}")
|
||||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||||
|
@org.springframework.transaction.annotation.Transactional
|
||||||
fun deleteAmenity(
|
fun deleteAmenity(
|
||||||
@PathVariable amenityId: UUID,
|
@PathVariable amenityId: UUID,
|
||||||
@AuthenticationPrincipal principal: MyPrincipal?
|
@AuthenticationPrincipal principal: MyPrincipal?
|
||||||
@@ -95,8 +96,12 @@ class RoomAmenities(
|
|||||||
val amenity = roomAmenityRepo.findById(amenityId).orElse(null)
|
val amenity = roomAmenityRepo.findById(amenityId).orElse(null)
|
||||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Amenity not found")
|
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Amenity not found")
|
||||||
|
|
||||||
if (roomTypeRepo.existsByAmenitiesId(amenityId)) {
|
val roomTypes = roomTypeRepo.findAllByAmenitiesId(amenityId)
|
||||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Amenity is used by room types")
|
if (roomTypes.isNotEmpty()) {
|
||||||
|
for (roomType in roomTypes) {
|
||||||
|
roomType.amenities.removeIf { it.id == amenityId }
|
||||||
|
}
|
||||||
|
roomTypeRepo.saveAll(roomTypes)
|
||||||
}
|
}
|
||||||
roomAmenityRepo.delete(amenity)
|
roomAmenityRepo.delete(amenity)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,6 @@ interface RoomTypeRepo : JpaRepository<RoomType, UUID> {
|
|||||||
fun existsByPropertyIdAndCode(propertyId: UUID, code: String): Boolean
|
fun existsByPropertyIdAndCode(propertyId: UUID, code: String): Boolean
|
||||||
fun existsByPropertyIdAndCodeAndIdNot(propertyId: UUID, code: String, id: UUID): Boolean
|
fun existsByPropertyIdAndCodeAndIdNot(propertyId: UUID, code: String, id: UUID): Boolean
|
||||||
fun existsByAmenitiesId(id: UUID): Boolean
|
fun existsByAmenitiesId(id: UUID): Boolean
|
||||||
|
@EntityGraph(attributePaths = ["amenities"])
|
||||||
|
fun findAllByAmenitiesId(id: UUID): List<RoomType>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user