-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/#30 유저 프로필 수정 기능 구현 #35
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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 static final int MAX_INTERESTS = 3; | ||
|
||
private final InterestRepository interestRepository; | ||
private final UserRepository userRepository; | ||
|
||
@Transactional | ||
public void updateInterests(Long userId, List<Keyword> interests) { | ||
validateInterests(interests); | ||
|
||
for (Keyword interest : interests) { | ||
if (!Keyword.isValidKeyword(interest.name())) { | ||
throw new IllegalArgumentException("유효한 관심사가 아닙니다."); | ||
} | ||
} | ||
User user = userRepository.findById(userId) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다.")); | ||
|
||
List<Interest> currentInterests = interestRepository.findAllByUserId(userId); | ||
interestRepository.deleteAll(currentInterests); | ||
|
||
for (Keyword keyword : interests) { | ||
Interest newInterest = new Interest(keyword, user); | ||
interestRepository.save(newInterest); | ||
} | ||
} | ||
|
||
private void validateInterests(List<Keyword> interests) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 서비스 레이어에서 말고, |
||
if (interests.isEmpty() || interests.size() > MAX_INTERESTS) { | ||
throw new IllegalArgumentException("최소 1개, 최대 3개의 관심사를 선택하세요."); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,18 @@ | |
import coffeemeet.server.common.annotation.Login; | ||
import coffeemeet.server.user.dto.AuthInfo; | ||
import coffeemeet.server.user.dto.MyProfileResponse; | ||
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.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
|
@@ -29,4 +34,18 @@ public ResponseEntity<MyProfileResponse> findMyProfile(@Login AuthInfo authInfo) | |
return ResponseEntity.ok(userService.findMyProfile(authInfo.userId())); | ||
} | ||
|
||
@PatchMapping("/me/profile-image") | ||
public ResponseEntity<Void> updateProfileImage(@Login AuthInfo authInfo, | ||
@RequestParam String profileImageUrl) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dto 객체로 빼서 검증 해주세용 |
||
userService.updateProfileImage(authInfo.userId(), profileImageUrl); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@PatchMapping("/me") | ||
public ResponseEntity<Void> updateProfileInfo(@Login AuthInfo authInfo, | ||
@Valid @RequestBody UpdateProfileRequest request) { | ||
userService.updateProfileInfo(authInfo.userId(), request.nickname(), request.interests()); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package coffeemeet.server.user.dto; | ||
|
||
import coffeemeet.server.interest.domain.Keyword; | ||
import jakarta.validation.constraints.NotBlank; | ||
import java.util.List; | ||
|
||
public record UpdateProfileRequest(@NotBlank String nickname, List<Keyword> interests) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 바로 이곳! |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,18 +30,38 @@ public void updateBusinessCardUrl(Long userId, String businessCardUrl) { | |
|
||
public UserProfileResponse findUserProfile(String nickname) { | ||
User user = userRepository.findUserByProfileNickname(nickname) | ||
.orElseThrow(IllegalArgumentException::new); | ||
.orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다.")); | ||
|
||
List<Interest> interests = interestRepository.findAllByUserId(user.getId()); | ||
return UserProfileResponse.of(user, interests); | ||
} | ||
|
||
public MyProfileResponse findMyProfile(Long userId) { | ||
User user = userRepository.findById(userId) | ||
.orElseThrow(IllegalArgumentException::new); | ||
.orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다.")); | ||
|
||
List<Interest> 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<Keyword> interests) { | ||
User user = userRepository.findById(userId) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다.")); | ||
|
||
if (nickname != null) { | ||
user.getProfile().updateNickname(nickname); | ||
} | ||
if (interests != null && !interests.isEmpty()) { | ||
interestService.updateInterests(userId, interests); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 입력 값에 대한 검증 로직은 dto에 추가해 주세요 |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enum에서 제공하는
Keyword.valueOf()
메서드 사용하는게 더 좋아보여요!