Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #257 delete amenity cases error 500 #258

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
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 java.util.Set;
import javax.validation.Valid;

import com.myhome.services.CommunityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -43,6 +46,8 @@ public class AmenityController implements AmenitiesApi {

private final AmenityService amenitySDJpaService;
private final AmenityApiMapper amenityApiMapper;
private final AmenityBookingService amenityBookingService;
private final CommunityService communityService;

@Override
public ResponseEntity<GetAmenityDetailsResponse> getAmenityDetails(
Expand All @@ -56,7 +61,7 @@ public ResponseEntity<GetAmenityDetailsResponse> getAmenityDetails(
@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 Expand Up @@ -98,7 +103,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 @@ -34,12 +34,6 @@
import com.myhome.model.GetHouseDetailsResponse;
import com.myhome.model.ListCommunityAdminsResponse;
import com.myhome.services.CommunityService;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
Expand All @@ -50,13 +44,21 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* REST Controller which provides endpoints for managing community
*/
@RequiredArgsConstructor
@RestController
@Slf4j
public class CommunityController implements CommunitiesApi {

private final CommunityService communityService;
private final CommunityApiMapper communityApiMapper;

Expand Down
26 changes: 19 additions & 7 deletions service/src/main/java/com/myhome/domain/Amenity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import javax.persistence.ManyToOne;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.NamedEntityGraphs;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -35,12 +37,22 @@
@Getter
@Setter
@With
@NamedEntityGraph(
name = "Amenity.community",
attributeNodes = {
@NamedAttributeNode("community"),
}
)
@NamedEntityGraphs({
@NamedEntityGraph(
name = "Amenity.community",
attributeNodes = {
@NamedAttributeNode("community"),
}
),
@NamedEntityGraph(
name = "Amenity.community_house",
attributeNodes = {
@NamedAttributeNode("community"),
@NamedAttributeNode("communityHouse")
}
)
})

public class Amenity extends BaseEntity {
@Column(nullable = false, unique = true)
private String amenityId;
Expand All @@ -52,6 +64,6 @@ public class Amenity extends BaseEntity {
private BigDecimal price;
@ManyToOne(fetch = FetchType.LAZY)
private Community community;
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
private CommunityHouse communityHouse;
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
public interface AmenityRepository extends JpaRepository<Amenity, Long> {

@Query("from Amenity amenity where amenity.amenityId = :amenityId")
@EntityGraph(value = "Amenity.community")
Optional<Amenity> findByAmenityIdWithCommunity(@Param("amenityId") String amenityId);
@EntityGraph(value = "Amenity.community_house")
Optional<Amenity> findByAmenityIdWithCommunityAndHouse(@Param("amenityId") String amenityId);

Optional<Amenity> findByAmenityId(String amenityId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.myhome.services;

import com.myhome.domain.Amenity;
import com.myhome.domain.AmenityBookingItem;

import java.time.LocalDateTime;

public interface AmenityBookingService {
AmenityBookingItem bookAmenity(Amenity amenity, LocalDateTime bookingStartDate, LocalDateTime bookingEndDate);

boolean deleteBooking(String bookingId);

void removeAllAmenityBookings(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
@@ -0,0 +1,43 @@
package com.myhome.services.springdatajpa;

import com.myhome.domain.Amenity;
import com.myhome.domain.AmenityBookingItem;
import com.myhome.repositories.AmenityBookingItemRepository;
import com.myhome.services.AmenityBookingService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;

@Service
@RequiredArgsConstructor
public class AmenityBookingSDJpaService implements AmenityBookingService {

private final AmenityBookingItemRepository bookingItemRepository;

@Override
public AmenityBookingItem bookAmenity(Amenity amenity, LocalDateTime bookingStartDate, LocalDateTime bookingEndDate) {
AmenityBookingItem newBooking = new AmenityBookingItem(null, amenity,
bookingStartDate, bookingEndDate, null);
return bookingItemRepository.save(newBooking);
}

@Override
public boolean deleteBooking(String bookingId) {
return bookingItemRepository.findByAmenityBookingItemId(bookingId)
.map(bookingItem -> {
bookingItemRepository.delete(bookingItem);
return true;
})
.orElse(false);
}

@Override
public void removeAllAmenityBookings(String amenityId) {
HashSet<AmenityBookingItem> bookings = bookingItemRepository.findAllByAmenity_AmenityId(amenityId);
if(!bookings.isEmpty()) {
bookingItemRepository.deleteAll(bookings);
}
}
}
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 @@ -70,29 +71,24 @@ public Optional<Amenity> getAmenityDetails(String amenityId) {

@Override
public boolean deleteAmenity(String amenityId) {
return amenityRepository.findByAmenityIdWithCommunity(amenityId)
return amenityRepository.findByAmenityIdWithCommunityAndHouse(amenityId)
.map(amenity -> {
Community community = amenity.getCommunity();
community.getAmenities().remove(amenity);
amenityBookingService.removeAllAmenityBookings(amenity.getAmenityId());
if(amenity.getCommunityHouse() != null) {
houseService.removeAmenityFromHouse(amenity.getCommunityHouse(),
amenity.getAmenityId());
}
amenityRepository.delete(amenity);
return true;
})
.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 @@ -101,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;
}
}
Loading