From f8bdb8e759d55f34a85b458354d29a0ac72eec7f Mon Sep 17 00:00:00 2001 From: androidlover5842 Date: Wed, 28 Jan 2026 20:25:58 +0530 Subject: [PATCH] Delete amenities by unlinking from room types --- .../android/trisolarisserver/controller/RoomAmenities.kt | 9 +++++++-- .../com/android/trisolarisserver/repo/RoomTypeRepo.kt | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/RoomAmenities.kt b/src/main/kotlin/com/android/trisolarisserver/controller/RoomAmenities.kt index 75cb35f..779eff6 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/RoomAmenities.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/RoomAmenities.kt @@ -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) } diff --git a/src/main/kotlin/com/android/trisolarisserver/repo/RoomTypeRepo.kt b/src/main/kotlin/com/android/trisolarisserver/repo/RoomTypeRepo.kt index 032685e..18de351 100644 --- a/src/main/kotlin/com/android/trisolarisserver/repo/RoomTypeRepo.kt +++ b/src/main/kotlin/com/android/trisolarisserver/repo/RoomTypeRepo.kt @@ -14,4 +14,6 @@ interface RoomTypeRepo : JpaRepository { fun existsByPropertyIdAndCode(propertyId: UUID, code: String): Boolean fun existsByPropertyIdAndCodeAndIdNot(propertyId: UUID, code: String, id: UUID): Boolean fun existsByAmenitiesId(id: UUID): Boolean + @EntityGraph(attributePaths = ["amenities"]) + fun findAllByAmenitiesId(id: UUID): List }