diff --git a/service/src/main/java/com/myhome/controllers/AmenityController.java b/service/src/main/java/com/myhome/controllers/AmenityController.java index cdc58a52..18593a3d 100644 --- a/service/src/main/java/com/myhome/controllers/AmenityController.java +++ b/service/src/main/java/com/myhome/controllers/AmenityController.java @@ -21,6 +21,7 @@ import com.myhome.model.AmenityDto; import com.myhome.model.GetAmenityDetailsResponse; import com.myhome.model.UpdateAmenityRequest; +import com.myhome.services.AmenityBookingService; import com.myhome.services.AmenityService; import com.myhome.services.CommunityService; import javax.validation.Valid; @@ -40,6 +41,7 @@ public class AmenityController implements AmenitiesApi { private final AmenityService amenitySDJpaService; private final AmenityApiMapper amenityApiMapper; + private final AmenityBookingService amenityBookingService; private final CommunityService communityService; @Override @@ -77,7 +79,7 @@ public ResponseEntity updateAmenity(@PathVariable String amenityId, // TODO: Move to api.yaml @DeleteMapping(path = "/bookings/{bookingId}") public ResponseEntity deleteBooking(@PathVariable String bookingId) { - boolean isBookingDeleted = amenitySDJpaService.deleteBooking(bookingId); + boolean isBookingDeleted = amenityBookingService.deleteBooking(bookingId); if (isBookingDeleted) { return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } else { diff --git a/service/src/main/java/com/myhome/controllers/CommunityController.java b/service/src/main/java/com/myhome/controllers/CommunityController.java index ba8a2763..7ce26910 100644 --- a/service/src/main/java/com/myhome/controllers/CommunityController.java +++ b/service/src/main/java/com/myhome/controllers/CommunityController.java @@ -42,6 +42,7 @@ import com.myhome.model.GetHouseDetailsResponse; import com.myhome.model.ListAdminPaymentsResponse; import com.myhome.model.ListCommunityAdminsResponse; +import com.myhome.services.HouseService; import com.myhome.utils.PageInfo; import com.myhome.services.AmenityService; import com.myhome.services.CommunityService; @@ -226,7 +227,7 @@ public ResponseEntity deleteCommunity(@PathVariable String communityId) { @Override public ResponseEntity> listAllAmenities( @PathVariable String communityId) { - Set amenities = amenitySDJpaService.listAllAmenities(communityId); + Set amenities = communityService.listAllAmenities(communityId); Set response = amenityApiMapper.amenitiesSetToAmenityDetailsResponseSet(amenities); return ResponseEntity.ok(response); diff --git a/service/src/main/java/com/myhome/repositories/AmenityBookingItemRepository.java b/service/src/main/java/com/myhome/repositories/AmenityBookingItemRepository.java index 5e3ce8e1..f53a1dfb 100644 --- a/service/src/main/java/com/myhome/repositories/AmenityBookingItemRepository.java +++ b/service/src/main/java/com/myhome/repositories/AmenityBookingItemRepository.java @@ -7,8 +7,11 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.util.HashSet; import java.util.Optional; public interface AmenityBookingItemRepository extends JpaRepository { Optional findByAmenityBookingItemId(String amenityBookingItemId); + + HashSet findAllByAmenity_AmenityId(String amenityId); } diff --git a/service/src/main/java/com/myhome/services/AmenityService.java b/service/src/main/java/com/myhome/services/AmenityService.java index 8da143b6..061906bf 100644 --- a/service/src/main/java/com/myhome/services/AmenityService.java +++ b/service/src/main/java/com/myhome/services/AmenityService.java @@ -30,9 +30,6 @@ public interface AmenityService { boolean deleteAmenity(String amenityId); - Set listAllAmenities(String communityId); - boolean updateAmenity(AmenityDto updatedAmenityDto); - boolean deleteBooking(String bookingId); } diff --git a/service/src/main/java/com/myhome/services/CommunityService.java b/service/src/main/java/com/myhome/services/CommunityService.java index cc4a4f2e..e3c1a0e4 100644 --- a/service/src/main/java/com/myhome/services/CommunityService.java +++ b/service/src/main/java/com/myhome/services/CommunityService.java @@ -17,6 +17,7 @@ package com.myhome.services; import com.myhome.controllers.dto.CommunityDto; +import com.myhome.domain.Amenity; import com.myhome.domain.Community; import com.myhome.domain.CommunityHouse; import com.myhome.domain.User; @@ -34,6 +35,8 @@ public interface CommunityService { Optional getCommunityDetailsById(String communityId); + Set listAllAmenities(String communityId); + Optional> findCommunityHousesById(String communityId, Pageable pageable); Optional> findCommunityAdminsById(String communityId, Pageable pageable); diff --git a/service/src/main/java/com/myhome/services/HouseService.java b/service/src/main/java/com/myhome/services/HouseService.java index c19f3368..777f643f 100644 --- a/service/src/main/java/com/myhome/services/HouseService.java +++ b/service/src/main/java/com/myhome/services/HouseService.java @@ -37,4 +37,6 @@ public interface HouseService { Optional> getHouseMembersById(String houseId, Pageable pageable); Optional> listHouseMembersForHousesOfUserId(String userId, Pageable pageable); + + boolean removeAmenityFromHouse(CommunityHouse house, String amenityId); } diff --git a/service/src/main/java/com/myhome/services/springdatajpa/AmenitySDJpaService.java b/service/src/main/java/com/myhome/services/springdatajpa/AmenitySDJpaService.java index 709fc514..b337a557 100644 --- a/service/src/main/java/com/myhome/services/springdatajpa/AmenitySDJpaService.java +++ b/service/src/main/java/com/myhome/services/springdatajpa/AmenitySDJpaService.java @@ -20,16 +20,17 @@ import com.myhome.domain.Amenity; import com.myhome.domain.Community; import com.myhome.model.AmenityDto; -import com.myhome.repositories.AmenityBookingItemRepository; import com.myhome.repositories.AmenityRepository; -import com.myhome.repositories.CommunityRepository; +import com.myhome.services.AmenityBookingService; import com.myhome.services.AmenityService; import com.myhome.services.CommunityService; -import java.util.HashSet; + import java.util.List; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; + +import com.myhome.services.HouseService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -38,10 +39,10 @@ public class AmenitySDJpaService implements AmenityService { private final AmenityRepository amenityRepository; - private final CommunityRepository communityRepository; private final CommunityService communityService; private final AmenityApiMapper amenityApiMapper; - private final AmenityBookingItemRepository bookingRepository; + private final AmenityBookingService amenityBookingService; + private final HouseService houseService; @Override public Optional> createAmenities(Set amenities, String communityId) { @@ -83,19 +84,11 @@ public boolean deleteAmenity(String amenityId) { .orElse(false); } - @Override - public Set listAllAmenities(String communityId) { - return communityRepository.findByCommunityIdWithAmenities(communityId) - .map(Community::getAmenities) - .orElse(new HashSet<>()); - } - @Override public boolean updateAmenity(AmenityDto updatedAmenity) { String amenityId = updatedAmenity.getAmenityId(); return amenityRepository.findByAmenityId(amenityId) - .map(amenity -> communityRepository.findByCommunityId(updatedAmenity.getCommunityId()) - .map(community -> { + .map(amenity -> { Amenity updated = new Amenity(); updated.setName(updatedAmenity.getName()); updated.setPrice(updatedAmenity.getPrice()); @@ -104,17 +97,6 @@ public boolean updateAmenity(AmenityDto updatedAmenity) { updated.setDescription(updatedAmenity.getDescription()); return updated; }) - .orElse(null)) .map(amenityRepository::save).isPresent(); } - - @Override - public boolean deleteBooking(String bookingId) { - return bookingRepository.findByAmenityBookingItemId(bookingId) - .map(bookingItem -> { - bookingRepository.delete(bookingItem); - return true; - }) - .orElse(false); - } } diff --git a/service/src/main/java/com/myhome/services/springdatajpa/CommunitySDJpaService.java b/service/src/main/java/com/myhome/services/springdatajpa/CommunitySDJpaService.java index 8182a56e..5d22220e 100644 --- a/service/src/main/java/com/myhome/services/springdatajpa/CommunitySDJpaService.java +++ b/service/src/main/java/com/myhome/services/springdatajpa/CommunitySDJpaService.java @@ -18,6 +18,7 @@ import com.myhome.controllers.dto.CommunityDto; import com.myhome.controllers.dto.mapper.CommunityMapper; +import com.myhome.domain.Amenity; import com.myhome.domain.Community; import com.myhome.domain.CommunityHouse; import com.myhome.domain.HouseMember; @@ -84,6 +85,13 @@ public Set listAll(Pageable pageable) { return communities; } + @Override + public Set listAllAmenities(String communityId) { + return communityRepository.findByCommunityIdWithAmenities(communityId) + .map(Community::getAmenities) + .orElse(new HashSet<>()); + } + @Override public Optional> findCommunityHousesById(String communityId, Pageable pageable) { diff --git a/service/src/main/java/com/myhome/services/springdatajpa/HouseSDJpaService.java b/service/src/main/java/com/myhome/services/springdatajpa/HouseSDJpaService.java index da732cac..e1118515 100644 --- a/service/src/main/java/com/myhome/services/springdatajpa/HouseSDJpaService.java +++ b/service/src/main/java/com/myhome/services/springdatajpa/HouseSDJpaService.java @@ -115,4 +115,13 @@ public Optional> listHouseMembersForHousesOfUserId(String user houseMemberRepository.findAllByCommunityHouse_Community_Admins_UserId(userId, pageable) ); } + + @Override + public boolean removeAmenityFromHouse(CommunityHouse house, String amenityId) { + boolean removed = house.getAmenities().removeIf(amenity -> amenity.getAmenityId().equals(amenityId)); + if (removed) { + communityHouseRepository.save(house); + } + return removed; + } }