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/#9 닉네임 중복 체크 기능 구현 #43

Merged
merged 8 commits into from
Oct 25, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ResponseEntity<AuthTokens> renew(@Login AuthInfo authInfo) {
@PostMapping("/logout")
public ResponseEntity<Void> logout(@Login AuthInfo authInfo) {
authService.logout(authInfo.userId());
return ResponseEntity.noContent().build();
return ResponseEntity.ok().build();
}

}
22 changes: 9 additions & 13 deletions src/main/java/coffeemeet/server/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import coffeemeet.server.user.domain.Profile;
import coffeemeet.server.user.domain.User;
import coffeemeet.server.user.repository.UserRepository;
import coffeemeet.server.user.service.UserService;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -26,11 +27,12 @@
public class AuthService {

private static final String EXPIRED_REFRESH_TOKEN_MESSAGE = "리프레시 토큰이 만료되었습니다. 다시 로그인해 주세요.";
private static final String ALREADY_REGISTERED_MESSAGE = "이미 가입된 사용자입니다.";
private static final String USER_NOT_REGISTERED_MESSAGE = "해당 아이디(%s)와 로그인 타입(%s)의 유저는 회원가입되지 않았습니다.";
private static final String DEFAULT_IMAGE_URL = "기본 이미지 URL";

private final AuthCodeRequestUrlProviderComposite authCodeRequestUrlProviderComposite;
private final OAuthMemberClientComposite oauthMemberClientComposite;
private final UserService userService;
private final UserRepository userRepository;
private final InterestRepository interestRepository;
private final AuthTokensGenerator authTokensGenerator;
Expand All @@ -44,16 +46,17 @@ public String getAuthCodeRequestUrl(OAuthProvider oAuthProvider) {
public AuthTokens signup(SignupRequest request) {
OAuthInfoResponse response = oauthMemberClientComposite.fetch(request.oAuthProvider(),
request.authCode());
checkDuplicateUser(response);

userService.checkDuplicatedUser(response);
userService.checkDuplicatedNickname(request.nickname());
String profileImage = checkProfileImage(response.profileImage());

User user = new User(new OAuthInfo(response.oAuthProvider(), response.oAuthProviderId()),
Profile.builder().name(response.name()).nickname(request.nickname()).email(response.email())
.profileImageUrl(profileImage).birth(response.birth()).build());

User newUser = userRepository.save(user);

generateInterests(request, newUser);
saveInterests(request, newUser);

return authTokensGenerator.generate(newUser.getId());
}
Expand All @@ -80,21 +83,14 @@ public void logout(Long userId) {
refreshTokenRepository.deleteById(userId);
}

private void checkDuplicateUser(OAuthInfoResponse response) {
if (userRepository.existsUserByOauthInfo_oauthProviderAndOauthInfo_oauthProviderId(
response.oAuthProvider(), response.oAuthProviderId())) {
throw new IllegalArgumentException(ALREADY_REGISTERED_MESSAGE);
}
}

private String checkProfileImage(String profileImage) {
if (profileImage == null) {
profileImage = "기본 이미지 URL";
profileImage = DEFAULT_IMAGE_URL;
}
return profileImage;
}

private void generateInterests(SignupRequest request, User newUser) {
private void saveInterests(SignupRequest request, User newUser) {
List<Interest> interests = new ArrayList<>();
for (Keyword value : request.keywords()) {
interests.add(new Interest(value, newUser));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import coffeemeet.server.user.dto.UserProfileResponse;
import coffeemeet.server.user.service.UserService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -17,6 +18,7 @@
import org.springframework.web.bind.annotation.PostMapping;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -41,9 +43,8 @@ public ResponseEntity<MyProfileResponse> findMyProfile(@Login AuthInfo authInfo)
@PostMapping("/me/profile-image")
public ResponseEntity<Void> updateProfileImage(
@Login AuthInfo authInfo,
@NotNull
@RequestPart("profileImage")
MultipartFile profileImage) {
@NotNull MultipartFile profileImage) {
userService.updateProfileImage(
authInfo.userId(),
FileUtils.convertMultipartFileToFile(profileImage));
Expand All @@ -58,4 +59,10 @@ public ResponseEntity<Void> updateProfileInfo(@Login AuthInfo authInfo,
return ResponseEntity.ok().build();
}

@GetMapping("/duplicate")
public ResponseEntity<Void> checkDuplicatedNickname(@RequestParam @NotBlank String nickname) {
userService.checkDuplicatedNickname(nickname);
return ResponseEntity.ok().build();
}

}
19 changes: 18 additions & 1 deletion src/main/java/coffeemeet/server/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static coffeemeet.server.common.media.S3MediaService.KeyType.PROFILE_IMAGE;

import coffeemeet.server.auth.dto.OAuthInfoResponse;
import coffeemeet.server.common.media.S3MediaService;
import coffeemeet.server.interest.domain.Interest;
import coffeemeet.server.interest.domain.Keyword;
Expand All @@ -23,7 +24,8 @@
@Transactional(readOnly = true)
public class UserService {

public static final String EXISTED_COMPANY_EMAIL_ERROR = "이미 사용 중인 회사이메일입니다.";
private static final String EXISTED_COMPANY_EMAIL_ERROR = "이미 사용 중인 회사이메일입니다.";
private static final String ALREADY_REGISTERED_MESSAGE = "이미 가입된 사용자입니다.";

private final S3MediaService s3MediaService;
private final UserRepository userRepository;
Expand Down Expand Up @@ -82,12 +84,27 @@ public void updateProfileInfo(Long userId, String nickname, String name,
List<Keyword> interests) {
User user = getUserById(userId);

checkDuplicatedNickname(nickname);

user.updateNickname(nickname);
user.updateName(name);

interestService.updateInterests(userId, interests);
}

public void checkDuplicatedNickname(String nickname) {
if (userRepository.findUserByProfileNickname(nickname).isPresent()) {
throw new IllegalArgumentException("이미 존재하는 닉네임입니다.");
}
}

public void checkDuplicatedUser(OAuthInfoResponse response) {
if (userRepository.existsUserByOauthInfo_oauthProviderAndOauthInfo_oauthProviderId(
response.oAuthProvider(), response.oAuthProviderId())) {
throw new IllegalArgumentException(ALREADY_REGISTERED_MESSAGE);
}
}
Comment on lines 94 to +106
Copy link
Collaborator Author

@1o18z 1o18z Oct 25, 2023

Choose a reason for hiding this comment

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

AuthService에 책임이 너무 많고 유저 중복 체크와 닉네임 중복 체크 로직은 UserService에 위치하는게 더 적절할 것 같아 AuthService에서 UserService로 이동했습니다!

추후에 AuthService의 역할을 OAuthService처럼 분리하여 개선할 예정입니다! 🙇🏻‍♀️🙆🏻‍♀️


private User getUserById(Long userId) {
return userRepository.findById(userId)
.orElseThrow(IllegalArgumentException::new);
Expand Down