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 all 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
Original file line number Diff line number Diff line change
@@ -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<Keyword> 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<Interest> currentInterests = interestRepository.findAllByUserId(userId);
interestRepository.deleteAll(currentInterests);

for (Keyword keyword : interests) {
Interest newInterest = new Interest(keyword, user);
interestRepository.save(newInterest);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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,
@Valid @RequestBody UpdateProfileImageUrlRequest request) {
userService.updateProfileImage(authInfo.userId(), request.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,7 @@
package coffeemeet.server.user.dto;

import jakarta.validation.constraints.NotBlank;

public record UpdateProfileImageUrlRequest(@NotBlank String profileImageUrl) {

}
Original file line number Diff line number Diff line change
@@ -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<Keyword> interests) {

}
23 changes: 21 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,34 @@ 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("해당 사용자를 찾을 수 없습니다."));

user.getProfile().updateNickname(nickname);
interestService.updateInterests(userId, interests);
}

}