Skip to content

Commit

Permalink
Merge pull request #76 from Solucitation/feature/favorites_places
Browse files Browse the repository at this point in the history
feat: 즐겨찾는 장소 관련해서 favPlaceId 대신 addrType을 통해 삭제, 수정, 상세보기를 구현
  • Loading branch information
anstjgus922 authored Aug 2, 2024
2 parents d2bc164 + 6f55b28 commit 46e80d3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<FavPlaceResponse> favPlacesList = favPlaceService.getAllFavoritePlaces(email);
return ResponseEntity.ok(favPlacesList);
List<FavPlaceResponse> favPlaces = favPlaceService.getAllFavoritePlaces(email);
return ResponseEntity.ok(favPlaces);
} catch (IllegalArgumentException e) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -73,36 +79,36 @@ public List<FavPlaceResponse> getAllFavoritePlaces(String email) {
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다."));

FavPlaceResponse homeResponse = new FavPlaceResponse("HOME");
FavPlaceResponse workResponse = new FavPlaceResponse("WORK");

List<FavPlace> favPlaces = favPlaceRepository.findAllByMemberId(member.getId());

Map<FavPlace.AddrType, FavPlaceResponse> 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<FavPlaceResponse> 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)) {
Expand Down

0 comments on commit 46e80d3

Please sign in to comment.