Skip to content

Commit

Permalink
Issue #257 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DDmit04 committed Mar 10, 2021
1 parent 53767d1 commit 3bf9f06
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -77,7 +79,7 @@ public ResponseEntity<Void> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -226,7 +227,7 @@ public ResponseEntity<Void> deleteCommunity(@PathVariable String communityId) {
@Override
public ResponseEntity<Set<GetAmenityDetailsResponse>> listAllAmenities(
@PathVariable String communityId) {
Set<Amenity> amenities = amenitySDJpaService.listAllAmenities(communityId);
Set<Amenity> amenities = communityService.listAllAmenities(communityId);
Set<GetAmenityDetailsResponse> response =
amenityApiMapper.amenitiesSetToAmenityDetailsResponseSet(amenities);
return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AmenityBookingItem, String> {
Optional<AmenityBookingItem> findByAmenityBookingItemId(String amenityBookingItemId);

HashSet<AmenityBookingItem> findAllByAmenity_AmenityId(String amenityId);
}
3 changes: 0 additions & 3 deletions service/src/main/java/com/myhome/services/AmenityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public interface AmenityService {

boolean deleteAmenity(String amenityId);

Set<Amenity> listAllAmenities(String communityId);

boolean updateAmenity(AmenityDto updatedAmenityDto);

boolean deleteBooking(String bookingId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +35,8 @@ public interface CommunityService {

Optional<Community> getCommunityDetailsById(String communityId);

Set<Amenity> listAllAmenities(String communityId);

Optional<List<CommunityHouse>> findCommunityHousesById(String communityId, Pageable pageable);

Optional<List<User>> findCommunityAdminsById(String communityId, Pageable pageable);
Expand Down
2 changes: 2 additions & 0 deletions service/src/main/java/com/myhome/services/HouseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public interface HouseService {
Optional<List<HouseMember>> getHouseMembersById(String houseId, Pageable pageable);

Optional<List<HouseMember>> listHouseMembersForHousesOfUserId(String userId, Pageable pageable);

boolean removeAmenityFromHouse(CommunityHouse house, String amenityId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<List<AmenityDto>> createAmenities(Set<AmenityDto> amenities, String communityId) {
Expand Down Expand Up @@ -83,19 +84,11 @@ public boolean deleteAmenity(String amenityId) {
.orElse(false);
}

@Override
public Set<Amenity> 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());
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +85,13 @@ public Set<Community> listAll(Pageable pageable) {
return communities;
}

@Override
public Set<Amenity> listAllAmenities(String communityId) {
return communityRepository.findByCommunityIdWithAmenities(communityId)
.map(Community::getAmenities)
.orElse(new HashSet<>());
}

@Override
public Optional<List<CommunityHouse>> findCommunityHousesById(String communityId,
Pageable pageable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,13 @@ public Optional<List<HouseMember>> 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;
}
}

0 comments on commit 3bf9f06

Please sign in to comment.