Skip to content

Commit 46e80d3

Browse files
authored
Merge pull request #76 from Solucitation/feature/favorites_places
feat: 즐겨찾는 장소 관련해서 favPlaceId 대신 addrType을 통해 삭제, 수정, 상세보기를 구현
2 parents d2bc164 + 6f55b28 commit 46e80d3

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/api/FavPlaceController.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ResponseEntity<?> saveFavPlace(Authentication authentication, @Valid @Req
3232
.collect(Collectors.joining(", "));
3333
return ResponseEntity
3434
.status(HttpStatus.BAD_REQUEST)
35-
.body(new ApiResponse(false,errorMessage));
35+
.body(new ApiResponse(false, errorMessage));
3636
}
3737

3838
try {
@@ -68,10 +68,10 @@ public ResponseEntity<?> saveFavPlace(Authentication authentication, @Valid @Req
6868
}
6969

7070
@DeleteMapping("/delete")
71-
public ResponseEntity<?> deleteFavPlace(Authentication authentication, @RequestParam Long favPlaceId) {
71+
public ResponseEntity<?> deleteFavPlace(Authentication authentication, @RequestParam FavPlace.AddrType addrType) {
7272
String email = authentication.getName();
7373
try {
74-
favPlaceService.deleteFavoritePlace(favPlaceId, email);
74+
favPlaceService.deleteFavoritePlace(addrType, email);
7575
return ResponseEntity.ok(new ApiResponse(true, "즐겨찾는 장소 삭제에 성공했습니다."));
7676
} catch (IllegalArgumentException e) {
7777
return ResponseEntity
@@ -95,10 +95,10 @@ public ResponseEntity<?> deleteFavPlace(Authentication authentication, @RequestP
9595
}
9696

9797
@GetMapping("/details")
98-
public ResponseEntity<?> getFavPlaceDetails(Authentication authentication, @RequestParam Long favPlaceId) {
98+
public ResponseEntity<?> getFavPlaceDetails(Authentication authentication, @RequestParam FavPlace.AddrType addrType) {
9999
String email = authentication.getName();
100100
try {
101-
FavPlace favPlace = favPlaceService.getFavoritePlaceDetails(favPlaceId, email);
101+
FavPlace favPlace = favPlaceService.getFavoritePlaceDetails(addrType, email);
102102
return ResponseEntity.ok(new FavPlaceResponse(favPlace.getFavPlaceId(), favPlace.getAddr(), favPlace.getAddrType().name()));
103103
} catch (IllegalArgumentException e) {
104104
return ResponseEntity
@@ -122,11 +122,11 @@ public ResponseEntity<?> getFavPlaceDetails(Authentication authentication, @Requ
122122
}
123123

124124
@GetMapping("/list")
125-
public ResponseEntity<?> getAllFavPlaces(Authentication authentication) {
125+
public ResponseEntity<?> listFavPlaces(Authentication authentication) {
126126
String email = authentication.getName();
127127
try {
128-
List<FavPlaceResponse> favPlacesList = favPlaceService.getAllFavoritePlaces(email);
129-
return ResponseEntity.ok(favPlacesList);
128+
List<FavPlaceResponse> favPlaces = favPlaceService.getAllFavoritePlaces(email);
129+
return ResponseEntity.ok(favPlaces);
130130
} catch (IllegalArgumentException e) {
131131
return ResponseEntity
132132
.status(HttpStatus.BAD_REQUEST)
@@ -164,15 +164,15 @@ public ResponseEntity<?> updateFavPlace(
164164
.body(new ApiResponse(false, "입력 값이 잘못되었습니다: " + errorMessage));
165165
}
166166

167-
if (favPlaceUpdateRequest.getFavPlaceId() == null) {
167+
if (favPlaceUpdateRequest.getAddrType() == null) {
168168
return ResponseEntity
169169
.status(HttpStatus.BAD_REQUEST)
170-
.body(new ApiResponse(false, "favPlaceId는 필수 입력 항목입니다."));
170+
.body(new ApiResponse(false, "addrType은 필수 입력 항목입니다."));
171171
}
172172

173173
try {
174174
FavPlace updatedPlace = favPlaceService.updateFavoritePlace(
175-
favPlaceUpdateRequest.getFavPlaceId(),
175+
FavPlace.AddrType.valueOf(favPlaceUpdateRequest.getAddrType()),
176176
favPlaceUpdateRequest.getAddr(),
177177
favPlaceUpdateRequest.getLatitude(),
178178
favPlaceUpdateRequest.getLongitude(),

src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/dto/FavPlaceRequest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
@Getter
1111
@Setter
1212
public class FavPlaceRequest {
13-
14-
private Long favPlaceId;
15-
1613
@NotBlank(message = "Address is required")
1714
@Size(max = 255, message = "Address must be less than 255 characters")
1815
private String addr;

src/main/java/com/solucitation/midpoint_backend/domain/FavPlace/service/FavPlaceService.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
import com.solucitation.midpoint_backend.domain.FavPlace.repository.FavPlaceRepository;
66
import com.solucitation.midpoint_backend.domain.member.entity.Member;
77
import com.solucitation.midpoint_backend.domain.member.repository.MemberRepository;
8+
import lombok.AllArgsConstructor;
9+
import lombok.Getter;
810
import lombok.RequiredArgsConstructor;
11+
import lombok.Setter;
912
import org.springframework.stereotype.Service;
1013
import org.springframework.transaction.annotation.Transactional;
1114

15+
import java.util.HashMap;
1216
import java.util.List;
17+
import java.util.Map;
18+
import java.util.stream.Collectors;
1319

1420
@Service
1521
@RequiredArgsConstructor
@@ -39,11 +45,11 @@ public FavPlace saveFavoritePlace(String addr, Float latitude, Float longitude,
3945
}
4046

4147
@Transactional
42-
public void deleteFavoritePlace(Long favPlaceId, String email) {
48+
public void deleteFavoritePlace(FavPlace.AddrType addrType, String email) {
4349
Member member = memberRepository.findByEmail(email)
4450
.orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다."));
4551

46-
FavPlace favPlace = favPlaceRepository.findById(favPlaceId)
52+
FavPlace favPlace = favPlaceRepository.findByAddrTypeAndMemberId(addrType, member.getId())
4753
.orElseThrow(() -> new RuntimeException("존재하지 않는 장소입니다."));
4854

4955
if (!favPlace.getMember().equals(member)) {
@@ -54,11 +60,11 @@ public void deleteFavoritePlace(Long favPlaceId, String email) {
5460
}
5561

5662
@Transactional(readOnly = true)
57-
public FavPlace getFavoritePlaceDetails(Long favPlaceId, String email) {
63+
public FavPlace getFavoritePlaceDetails(FavPlace.AddrType addrType, String email) {
5864
Member member = memberRepository.findByEmail(email)
5965
.orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다."));
6066

61-
FavPlace favPlace = favPlaceRepository.findById(favPlaceId)
67+
FavPlace favPlace = favPlaceRepository.findByAddrTypeAndMemberId(addrType, member.getId())
6268
.orElseThrow(() -> new RuntimeException("존재하지 않는 장소입니다."));
6369

6470
if (!favPlace.getMember().equals(member)) {
@@ -73,36 +79,36 @@ public List<FavPlaceResponse> getAllFavoritePlaces(String email) {
7379
Member member = memberRepository.findByEmail(email)
7480
.orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다."));
7581

76-
FavPlaceResponse homeResponse = new FavPlaceResponse("HOME");
77-
FavPlaceResponse workResponse = new FavPlaceResponse("WORK");
78-
7982
List<FavPlace> favPlaces = favPlaceRepository.findAllByMemberId(member.getId());
8083

84+
Map<FavPlace.AddrType, FavPlaceResponse> responseMap = new HashMap<>();
85+
86+
responseMap.put(FavPlace.AddrType.HOME, new FavPlaceResponse(null, null, FavPlace.AddrType.HOME.name()));
87+
responseMap.put(FavPlace.AddrType.WORK, new FavPlaceResponse(null, null, FavPlace.AddrType.WORK.name()));
88+
8189
for (FavPlace favPlace : favPlaces) {
82-
if (favPlace.getAddrType() == FavPlace.AddrType.HOME) {
83-
homeResponse = new FavPlaceResponse(
90+
FavPlace.AddrType addrType = favPlace.getAddrType();
91+
if (responseMap.containsKey(addrType)) {
92+
responseMap.put(addrType, new FavPlaceResponse(
8493
favPlace.getFavPlaceId(),
8594
favPlace.getAddr(),
86-
favPlace.getAddrType().name()
87-
);
88-
} else if (favPlace.getAddrType() == FavPlace.AddrType.WORK) {
89-
workResponse = new FavPlaceResponse(
90-
favPlace.getFavPlaceId(),
91-
favPlace.getAddr(),
92-
favPlace.getAddrType().name()
93-
);
95+
addrType.name()
96+
));
9497
}
9598
}
9699

97-
return List.of(homeResponse, workResponse);
100+
List<FavPlaceResponse> responses = responseMap.values().stream()
101+
.collect(Collectors.toList());
102+
103+
return responses;
98104
}
99105

100106
@Transactional
101-
public FavPlace updateFavoritePlace(Long favPlaceId, String addr, Float latitude, Float longitude, String email) {
107+
public FavPlace updateFavoritePlace(FavPlace.AddrType addrType, String addr, Float latitude, Float longitude, String email) {
102108
Member member = memberRepository.findByEmail(email)
103109
.orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다."));
104110

105-
FavPlace favPlace = favPlaceRepository.findById(favPlaceId)
111+
FavPlace favPlace = favPlaceRepository.findByAddrTypeAndMemberId(addrType, member.getId())
106112
.orElseThrow(() -> new RuntimeException("존재하지 않는 장소입니다."));
107113

108114
if (!favPlace.getMember().equals(member)) {

0 commit comments

Comments
 (0)