Skip to content
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

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/coffeemeet/server/interest/domain/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@ public enum Keyword {
COOK,
GAME,
EXERCISE,
;

public static boolean isValidKeyword(String keyword) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum에서 제공하는 Keyword.valueOf() 메서드 사용하는게 더 좋아보여요!

for (Keyword key : Keyword.values()) {
if (key.name().equals(keyword)) {
return true;
}
}
return false;
}

}
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서비스 레이어에서 말고,
DTO 객체에서 입력값에 대한 검증 로직을 추가하는게 좋아보입니다!

if (interests.isEmpty() || interests.size() > MAX_INTERESTS) {
throw new IllegalArgumentException("최소 1개, 최대 3개의 관심사를 선택하세요.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
}

}
10 changes: 10 additions & 0 deletions src/main/java/coffeemeet/server/user/domain/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
4 changes: 4 additions & 0 deletions src/main/java/coffeemeet/server/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ public void updateBusinessCardUrl(String newBusinessCardUrl) {
certification.updateBusinessCardUrl(newBusinessCardUrl);
}

public void updateProfileImageUrl(String profileImageUrl) {
this.profile.updateProfileImageUrl(profileImageUrl);
}

}
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

바로 이곳!


}
27 changes: 25 additions & 2 deletions src/main/java/coffeemeet/server/user/service/UserService.java
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;
Expand All @@ -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) {
Expand All @@ -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);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

입력 값에 대한 검증 로직은 dto에 추가해 주세요

}

}