diff --git a/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/api/FavPlaceController.java b/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/api/FavPlaceController.java index cfb238c..e813e8e 100644 --- a/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/api/FavPlaceController.java +++ b/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/api/FavPlaceController.java @@ -32,7 +32,7 @@ public ResponseEntity saveFavPlace(Authentication authentication, @Valid @Req .collect(Collectors.joining(", ")); return ResponseEntity .status(HttpStatus.BAD_REQUEST) - .body(new ApiResponse(false,errorMessage)); + .body(new ApiResponse(false, errorMessage)); } try { @@ -68,10 +68,10 @@ public ResponseEntity saveFavPlace(Authentication authentication, @Valid @Req } @DeleteMapping("/delete") - public ResponseEntity deleteFavPlace(Authentication authentication, @RequestParam Long favPlaceId) { + public ResponseEntity deleteFavPlace(Authentication authentication, @RequestParam FavPlace.AddrType addrType) { String email = authentication.getName(); try { - favPlaceService.deleteFavoritePlace(favPlaceId, email); + favPlaceService.deleteFavoritePlace(addrType, email); return ResponseEntity.ok(new ApiResponse(true, "즐겨찾는 장소 삭제에 성공했습니다.")); } catch (IllegalArgumentException e) { return ResponseEntity @@ -95,10 +95,10 @@ public ResponseEntity deleteFavPlace(Authentication authentication, @RequestP } @GetMapping("/details") - public ResponseEntity getFavPlaceDetails(Authentication authentication, @RequestParam Long favPlaceId) { + public ResponseEntity getFavPlaceDetails(Authentication authentication, @RequestParam FavPlace.AddrType addrType) { String email = authentication.getName(); try { - FavPlace favPlace = favPlaceService.getFavoritePlaceDetails(favPlaceId, email); + FavPlace favPlace = favPlaceService.getFavoritePlaceDetails(addrType, email); return ResponseEntity.ok(new FavPlaceResponse(favPlace.getFavPlaceId(), favPlace.getAddr(), favPlace.getAddrType().name())); } catch (IllegalArgumentException e) { return ResponseEntity @@ -122,11 +122,11 @@ public ResponseEntity getFavPlaceDetails(Authentication authentication, @Requ } @GetMapping("/list") - public ResponseEntity getAllFavPlaces(Authentication authentication) { + public ResponseEntity listFavPlaces(Authentication authentication) { String email = authentication.getName(); try { - List favPlacesList = favPlaceService.getAllFavoritePlaces(email); - return ResponseEntity.ok(favPlacesList); + List favPlaces = favPlaceService.getAllFavoritePlaces(email); + return ResponseEntity.ok(favPlaces); } catch (IllegalArgumentException e) { return ResponseEntity .status(HttpStatus.BAD_REQUEST) @@ -164,15 +164,15 @@ public ResponseEntity updateFavPlace( .body(new ApiResponse(false, "입력 값이 잘못되었습니다: " + errorMessage)); } - if (favPlaceUpdateRequest.getFavPlaceId() == null) { + if (favPlaceUpdateRequest.getAddrType() == null) { return ResponseEntity .status(HttpStatus.BAD_REQUEST) - .body(new ApiResponse(false, "favPlaceId는 필수 입력 항목입니다.")); + .body(new ApiResponse(false, "addrType은 필수 입력 항목입니다.")); } try { FavPlace updatedPlace = favPlaceService.updateFavoritePlace( - favPlaceUpdateRequest.getFavPlaceId(), + FavPlace.AddrType.valueOf(favPlaceUpdateRequest.getAddrType()), favPlaceUpdateRequest.getAddr(), favPlaceUpdateRequest.getLatitude(), favPlaceUpdateRequest.getLongitude(), diff --git a/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/dto/FavPlaceRequest.java b/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/dto/FavPlaceRequest.java index b902d02..8094771 100644 --- a/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/dto/FavPlaceRequest.java +++ b/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/dto/FavPlaceRequest.java @@ -10,9 +10,6 @@ @Getter @Setter public class FavPlaceRequest { - - private Long favPlaceId; - @NotBlank(message = "Address is required") @Size(max = 255, message = "Address must be less than 255 characters") private String addr; diff --git a/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/service/FavPlaceService.java b/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/service/FavPlaceService.java index 9e713f5..9b17852 100644 --- a/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/service/FavPlaceService.java +++ b/src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/service/FavPlaceService.java @@ -5,11 +5,17 @@ import com.solucitation.midpoint_backend.domain.FavPlace.repository.FavPlaceRepository; import com.solucitation.midpoint_backend.domain.member.entity.Member; import com.solucitation.midpoint_backend.domain.member.repository.MemberRepository; +import lombok.AllArgsConstructor; +import lombok.Getter; import lombok.RequiredArgsConstructor; +import lombok.Setter; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -39,11 +45,11 @@ public FavPlace saveFavoritePlace(String addr, Float latitude, Float longitude, } @Transactional - public void deleteFavoritePlace(Long favPlaceId, String email) { + public void deleteFavoritePlace(FavPlace.AddrType addrType, String email) { Member member = memberRepository.findByEmail(email) .orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다.")); - FavPlace favPlace = favPlaceRepository.findById(favPlaceId) + FavPlace favPlace = favPlaceRepository.findByAddrTypeAndMemberId(addrType, member.getId()) .orElseThrow(() -> new RuntimeException("존재하지 않는 장소입니다.")); if (!favPlace.getMember().equals(member)) { @@ -54,11 +60,11 @@ public void deleteFavoritePlace(Long favPlaceId, String email) { } @Transactional(readOnly = true) - public FavPlace getFavoritePlaceDetails(Long favPlaceId, String email) { + public FavPlace getFavoritePlaceDetails(FavPlace.AddrType addrType, String email) { Member member = memberRepository.findByEmail(email) .orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다.")); - FavPlace favPlace = favPlaceRepository.findById(favPlaceId) + FavPlace favPlace = favPlaceRepository.findByAddrTypeAndMemberId(addrType, member.getId()) .orElseThrow(() -> new RuntimeException("존재하지 않는 장소입니다.")); if (!favPlace.getMember().equals(member)) { @@ -73,36 +79,36 @@ public List getAllFavoritePlaces(String email) { Member member = memberRepository.findByEmail(email) .orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다.")); - FavPlaceResponse homeResponse = new FavPlaceResponse("HOME"); - FavPlaceResponse workResponse = new FavPlaceResponse("WORK"); - List favPlaces = favPlaceRepository.findAllByMemberId(member.getId()); + Map responseMap = new HashMap<>(); + + responseMap.put(FavPlace.AddrType.HOME, new FavPlaceResponse(null, null, FavPlace.AddrType.HOME.name())); + responseMap.put(FavPlace.AddrType.WORK, new FavPlaceResponse(null, null, FavPlace.AddrType.WORK.name())); + for (FavPlace favPlace : favPlaces) { - if (favPlace.getAddrType() == FavPlace.AddrType.HOME) { - homeResponse = new FavPlaceResponse( + FavPlace.AddrType addrType = favPlace.getAddrType(); + if (responseMap.containsKey(addrType)) { + responseMap.put(addrType, new FavPlaceResponse( favPlace.getFavPlaceId(), favPlace.getAddr(), - favPlace.getAddrType().name() - ); - } else if (favPlace.getAddrType() == FavPlace.AddrType.WORK) { - workResponse = new FavPlaceResponse( - favPlace.getFavPlaceId(), - favPlace.getAddr(), - favPlace.getAddrType().name() - ); + addrType.name() + )); } } - return List.of(homeResponse, workResponse); + List responses = responseMap.values().stream() + .collect(Collectors.toList()); + + return responses; } @Transactional - public FavPlace updateFavoritePlace(Long favPlaceId, String addr, Float latitude, Float longitude, String email) { + public FavPlace updateFavoritePlace(FavPlace.AddrType addrType, String addr, Float latitude, Float longitude, String email) { Member member = memberRepository.findByEmail(email) .orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다.")); - FavPlace favPlace = favPlaceRepository.findById(favPlaceId) + FavPlace favPlace = favPlaceRepository.findByAddrTypeAndMemberId(addrType, member.getId()) .orElseThrow(() -> new RuntimeException("존재하지 않는 장소입니다.")); if (!favPlace.getMember().equals(member)) {