diff --git a/src/main/java/coffeemeet/server/interest/service/InterestService.java b/src/main/java/coffeemeet/server/interest/service/InterestService.java new file mode 100644 index 00000000..a9906ed2 --- /dev/null +++ b/src/main/java/coffeemeet/server/interest/service/InterestService.java @@ -0,0 +1,43 @@ +package coffeemeet.server.interest.service; + +import coffeemeet.server.interest.domain.Interest; +import coffeemeet.server.interest.domain.Keyword; +import coffeemeet.server.interest.repository.InterestRepository; +import coffeemeet.server.user.domain.User; +import coffeemeet.server.user.repository.UserRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class InterestService { + + private final InterestRepository interestRepository; + private final UserRepository userRepository; + + @Transactional + public void updateInterests(Long userId, List interests) { + + for (Keyword interest : interests) { + try { + Keyword.valueOf(interest.name()); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("유효한 관심사가 아닙니다."); + } + } + + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다.")); + + List currentInterests = interestRepository.findAllByUserId(userId); + interestRepository.deleteAll(currentInterests); + + for (Keyword keyword : interests) { + Interest newInterest = new Interest(keyword, user); + interestRepository.save(newInterest); + } + } + +} diff --git a/src/main/java/coffeemeet/server/user/controller/UserController.java b/src/main/java/coffeemeet/server/user/controller/UserController.java index 86131025..c7223d70 100644 --- a/src/main/java/coffeemeet/server/user/controller/UserController.java +++ b/src/main/java/coffeemeet/server/user/controller/UserController.java @@ -3,12 +3,17 @@ import coffeemeet.server.common.annotation.Login; import coffeemeet.server.user.dto.AuthInfo; import coffeemeet.server.user.dto.MyProfileResponse; +import coffeemeet.server.user.dto.UpdateProfileImageUrlRequest; +import coffeemeet.server.user.dto.UpdateProfileRequest; import coffeemeet.server.user.dto.UserProfileResponse; import coffeemeet.server.user.service.UserService; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -29,4 +34,18 @@ public ResponseEntity findMyProfile(@Login AuthInfo authInfo) return ResponseEntity.ok(userService.findMyProfile(authInfo.userId())); } + @PatchMapping("/me/profile-image") + public ResponseEntity updateProfileImage(@Login AuthInfo authInfo, + @Valid @RequestBody UpdateProfileImageUrlRequest request) { + userService.updateProfileImage(authInfo.userId(), request.profileImageUrl()); + return ResponseEntity.noContent().build(); + } + + @PatchMapping("/me") + public ResponseEntity updateProfileInfo(@Login AuthInfo authInfo, + @Valid @RequestBody UpdateProfileRequest request) { + userService.updateProfileInfo(authInfo.userId(), request.nickname(), request.interests()); + return ResponseEntity.noContent().build(); + } + } diff --git a/src/main/java/coffeemeet/server/user/domain/Profile.java b/src/main/java/coffeemeet/server/user/domain/Profile.java index 97b82806..cc075942 100644 --- a/src/main/java/coffeemeet/server/user/domain/Profile.java +++ b/src/main/java/coffeemeet/server/user/domain/Profile.java @@ -69,4 +69,14 @@ private void validateProfileImage(String profileImageUrl) { } } + public void updateNickname(String nickname) { + validateNickname(nickname); + this.nickname = nickname; + } + + public void updateProfileImageUrl(String profileImageUrl) { + validateProfileImage(profileImageUrl); + this.profileImageUrl = profileImageUrl; + } + } diff --git a/src/main/java/coffeemeet/server/user/domain/User.java b/src/main/java/coffeemeet/server/user/domain/User.java index ca575d69..b28172da 100644 --- a/src/main/java/coffeemeet/server/user/domain/User.java +++ b/src/main/java/coffeemeet/server/user/domain/User.java @@ -63,4 +63,8 @@ public void updateBusinessCardUrl(String newBusinessCardUrl) { certification.updateBusinessCardUrl(newBusinessCardUrl); } + public void updateProfileImageUrl(String profileImageUrl) { + this.profile.updateProfileImageUrl(profileImageUrl); + } + } diff --git a/src/main/java/coffeemeet/server/user/dto/UpdateProfileImageUrlRequest.java b/src/main/java/coffeemeet/server/user/dto/UpdateProfileImageUrlRequest.java new file mode 100644 index 00000000..7f1c54ca --- /dev/null +++ b/src/main/java/coffeemeet/server/user/dto/UpdateProfileImageUrlRequest.java @@ -0,0 +1,7 @@ +package coffeemeet.server.user.dto; + +import jakarta.validation.constraints.NotBlank; + +public record UpdateProfileImageUrlRequest(@NotBlank String profileImageUrl) { + +} diff --git a/src/main/java/coffeemeet/server/user/dto/UpdateProfileRequest.java b/src/main/java/coffeemeet/server/user/dto/UpdateProfileRequest.java new file mode 100644 index 00000000..a0458198 --- /dev/null +++ b/src/main/java/coffeemeet/server/user/dto/UpdateProfileRequest.java @@ -0,0 +1,12 @@ +package coffeemeet.server.user.dto; + +import coffeemeet.server.interest.domain.Keyword; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import java.util.List; + +public record UpdateProfileRequest(@NotBlank String nickname, + @NotNull @Size(min = 1, max = 3) List interests) { + +} diff --git a/src/main/java/coffeemeet/server/user/service/UserService.java b/src/main/java/coffeemeet/server/user/service/UserService.java index 40e64067..78f3c518 100644 --- a/src/main/java/coffeemeet/server/user/service/UserService.java +++ b/src/main/java/coffeemeet/server/user/service/UserService.java @@ -1,7 +1,9 @@ package coffeemeet.server.user.service; import coffeemeet.server.interest.domain.Interest; +import coffeemeet.server.interest.domain.Keyword; import coffeemeet.server.interest.repository.InterestRepository; +import coffeemeet.server.interest.service.InterestService; import coffeemeet.server.user.domain.User; import coffeemeet.server.user.dto.MyProfileResponse; import coffeemeet.server.user.dto.UserProfileResponse; @@ -17,6 +19,7 @@ public class UserService { private final UserRepository userRepository; private final InterestRepository interestRepository; + private final InterestService interestService; @Transactional public void updateBusinessCardUrl(Long userId, String businessCardUrl) { @@ -27,7 +30,7 @@ public void updateBusinessCardUrl(Long userId, String businessCardUrl) { public UserProfileResponse findUserProfile(String nickname) { User user = userRepository.findUserByProfileNickname(nickname) - .orElseThrow(IllegalArgumentException::new); + .orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다.")); List interests = interestRepository.findAllByUserId(user.getId()); return UserProfileResponse.of(user, interests); @@ -35,10 +38,26 @@ public UserProfileResponse findUserProfile(String nickname) { public MyProfileResponse findMyProfile(Long userId) { User user = userRepository.findById(userId) - .orElseThrow(IllegalArgumentException::new); + .orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다.")); List interests = interestRepository.findAllByUserId(userId); return MyProfileResponse.of(user, interests); } + @Transactional + public void updateProfileImage(Long userId, String profileImageUrl) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다.")); + user.updateProfileImageUrl(profileImageUrl); + } + + @Transactional + public void updateProfileInfo(Long userId, String nickname, List interests) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다.")); + + user.getProfile().updateNickname(nickname); + interestService.updateInterests(userId, interests); + } + }