Skip to content

Commit

Permalink
Merge pull request #74 from Solucitation/feature/favorites_friends
Browse files Browse the repository at this point in the history
  • Loading branch information
anstjgus922 authored Aug 1, 2024
2 parents 4b78507 + ff95423 commit 7bbd393
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,26 @@ public ResponseEntity<?> getFavFriendsList(Authentication authentication) {
}

@PatchMapping("/update")
public ResponseEntity<?> updateFavFriend(Authentication authentication, @RequestParam Long favFriendId, @RequestParam(required = false) String name, @RequestParam(required = false) String address) {
public ResponseEntity<?> updateFavFriend(Authentication authentication, @Valid @RequestBody FavFriendRequest updateRequest, BindingResult result) {
String email = authentication.getName();
if (result.hasErrors()) {
String errorMessage = result.getAllErrors().stream()
.map(error -> error.getDefaultMessage())
.collect(Collectors.joining(", "));
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(new ApiResponse(false, "입력 값이 잘못되었습니다: " + errorMessage));
}

try {
FavFriend updatedFriend = favFriendService.updateFavoriteFriend(favFriendId, name, address, email);
FavFriend updatedFriend = favFriendService.updateFavoriteFriend(
updateRequest.getFavFriendId(),
updateRequest.getName(),
updateRequest.getAddress(),
updateRequest.getLatitude(),
updateRequest.getLongitude(),
email
);
return ResponseEntity.ok(new ApiResponse(true, "즐겨찾는 친구 수정에 성공했습니다.", updatedFriend.getFavFriendId()));
} catch (IllegalArgumentException e) {
return ResponseEntity
Expand Down Expand Up @@ -144,6 +160,14 @@ public FavFriendResponse(Long favFriendId, String name, String address) {
}
}

@Getter
@Setter
public static class FavFriendUpdateRequest {
private Long favFriendId;
private String name;
private String address;
}

public static class ApiResponse {
private boolean success;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@Setter
public class FavFriendRequest {

private Long favFriendId;

@NotBlank(message = "Address is required")
@Size(max = 255, message = "Address must be less than 255 characters")
private String address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ public void setAddress(String address) {
public void setName(String name) {
this.name = name;
}
}

public void setLatitude(Float latitude) {
this.latitude = latitude;
}

public void setLongitude(Float longitude) {
this.longitude = longitude;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,36 @@ public List<FavFriend> getFavoriteFriends(String email) {
}

@Transactional
public FavFriend updateFavoriteFriend(Long favFriendId, String name, String address, String email) {
public FavFriend updateFavoriteFriend(Long favFriendId, String name, String address, Float latitude, Float longitude, String email) {
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다."));

FavFriend favFriend = favoriteFriendRepository.findByFavFriendIdAndMemberId(favFriendId, member.getId())
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 즐겨찾는 친구입니다."));

if (name == null || name.trim().isEmpty() || name.length() > 100) {
if (name != null && !name.trim().isEmpty() && name.length() <= 100) {
if (!name.equals(favFriend.getName())) {
if (favoriteFriendRepository.findByNameAndMemberId(name, member.getId()).isPresent()) {
throw new IllegalArgumentException("이미 존재하는 친구 이름입니다.");
}
favFriend.setName(name);
}
} else if (name != null) {
throw new IllegalArgumentException("이름은 최소 1글자 이상 최대 100글자 이하로 입력해야 합니다.");
}
if (address == null || address.trim().isEmpty() || address.length() > 255) {

if (address != null && !address.trim().isEmpty() && address.length() <= 255) {
favFriend.setAddress(address);
} else if (address != null) {
throw new IllegalArgumentException("주소는 최소 1글자 이상 최대 255글자 이하로 입력해야 합니다.");
}

if (!address.equals(favFriend.getAddress())) {
favFriend.setAddress(address);
if (latitude != null) {
favFriend.setLatitude(latitude);
}

if (!name.equals(favFriend.getName())) {
if (favoriteFriendRepository.findByNameAndMemberId(name, member.getId()).isPresent()) {
throw new IllegalArgumentException("이미 존재하는 친구 이름입니다.");
}
favFriend.setName(name);
if (longitude != null) {
favFriend.setLongitude(longitude);
}

return favoriteFriendRepository.save(favFriend);
Expand Down

0 comments on commit 7bbd393

Please sign in to comment.