From b1497a35684a9ddb18010fba6e2ecaa80c6556bb Mon Sep 17 00:00:00 2001 From: anstjgus922 Date: Fri, 2 Aug 2024 12:24:14 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20favPlaceId=EB=8C=80=EC=8B=A0=20addr?= =?UTF-8?q?Type=EC=9D=84=20=ED=86=B5=ED=95=B4=20=EC=82=AD=EC=A0=9C,=20?= =?UTF-8?q?=EC=83=81=EC=84=B8=EB=B3=B4=EA=B8=B0,=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?=EA=B0=80=EB=8A=A5=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FavPlace/api/FavPlaceController.java | 22 +++++----- .../domain/FavPlace/dto/FavPlaceRequest.java | 3 -- .../FavPlace/service/FavPlaceService.java | 44 ++++++++++--------- 3 files changed, 35 insertions(+), 34 deletions(-) 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..02b51e5 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)) { @@ -70,39 +76,37 @@ public FavPlace getFavoritePlaceDetails(Long favPlaceId, String email) { @Transactional(readOnly = true) 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<>(); + 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)) { From 6f55b28d8e8ebc59f7b3df85931736e6d6e8966e Mon Sep 17 00:00:00 2001 From: anstjgus922 Date: Fri, 2 Aug 2024 12:26:43 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EC=A6=90=EA=B2=A8=EC=B0=BE?= =?UTF-8?q?=EB=8A=94=20=EC=9E=A5=EC=86=8C=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=B3=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 출력 모양 변경 --- .../domain/FavPlace/service/FavPlaceService.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 02b51e5..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 @@ -76,7 +76,6 @@ public FavPlace getFavoritePlaceDetails(FavPlace.AddrType addrType, String email @Transactional(readOnly = true) public List getAllFavoritePlaces(String email) { - // 회원 조회 Member member = memberRepository.findByEmail(email) .orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다.")); @@ -84,9 +83,12 @@ public List getAllFavoritePlaces(String email) { 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) { FavPlace.AddrType addrType = favPlace.getAddrType(); - if (!responseMap.containsKey(addrType)) { + if (responseMap.containsKey(addrType)) { responseMap.put(addrType, new FavPlaceResponse( favPlace.getFavPlaceId(), favPlace.getAddr(),