Delete amenities by unlinking from room types
All checks were successful
build-and-deploy / build-deploy (push) Successful in 1m54s

This commit is contained in:
androidlover5842
2026-01-28 20:25:58 +05:30
parent 7b72dd45e3
commit f8bdb8e759
2 changed files with 9 additions and 2 deletions

View File

@@ -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)
} }

View File

@@ -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>
} }