Skip to content

Commit

Permalink
Merge pull request #60 from coffee-meet/feat/#38
Browse files Browse the repository at this point in the history
�Refactor/#38 유저 도메인 리팩터링
  • Loading branch information
1o18z authored Oct 30, 2023
2 parents 702120b + b639277 commit 254377f
Show file tree
Hide file tree
Showing 27 changed files with 399 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import coffeemeet.server.common.media.S3MediaService;
import coffeemeet.server.common.util.FileUtils;
import coffeemeet.server.user.domain.User;
import coffeemeet.server.user.service.UserService;
import coffeemeet.server.user.service.cq.UserQuery;
import java.io.File;
import java.util.random.RandomGenerator;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,7 +27,7 @@ public class CertificationService {

private final S3MediaService s3MediaService;
private final EmailService emailService;
private final UserService userService;
private final UserQuery userQuery;
private final CertificationCommand certificationCommand;
private final CertificationQuery certificationQuery;
private final EmailVerificationCommand emailVerificationCommand;
Expand All @@ -41,7 +41,7 @@ public void registerCertification(long userId, String email, String departmentNa
CompanyEmail companyEmail = new CompanyEmail(email);
String businessCardUrl = s3MediaService.getUrl(key);
Department department = Department.valueOf(departmentName);
User user = userService.getUserById(userId);
User user = userQuery.getUserById(userId);
certificationCommand.newCertification(companyEmail, businessCardUrl, department, user);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package coffeemeet.server.interest.service.cq;

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 java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class InterestCommand {

private final InterestRepository interestRepository;

public List<Interest> findAllByUserId(long userId) {
return interestRepository.findAllByUserId(userId);
}

public void saveAll(List<Keyword> keywords, User user) {
List<Interest> interests = keywords.stream()
.map(keyword -> new Interest(keyword, user))
.toList();
interestRepository.saveAll(interests);
}

public void updateInterests(User user, List<Keyword> keywords) {
List<Interest> currentInterests = findAllByUserId(user.getId());
deleteAll(currentInterests);
saveAll(keywords, user);
}

public void deleteAll(List<Interest> interests) {
interestRepository.deleteAllInBatch(interests);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package coffeemeet.server.interest.service.cq;

import coffeemeet.server.interest.domain.Interest;
import coffeemeet.server.interest.domain.Keyword;
import coffeemeet.server.interest.repository.InterestRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class InterestQuery {

private final InterestRepository interestRepository;

public List<Keyword> getKeywordsByUserId(Long userId) {
return interestRepository.findAllByUserId(userId).stream()
.map(Interest::getKeyword)
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public String getAuthCodeRequestUrl(OAuthProvider oAuthProvider) {
return authCodeRequestUrlProviderComposite.provide(oAuthProvider);
}

public OAuthUserInfoDto.Response getOAuthInfo(OAuthProvider oAuthProvider, String authCode) {
public OAuthUserInfoDto.Response getOAuthUserInfo(OAuthProvider oAuthProvider, String authCode) {
return oauthMemberClientComposite.fetch(oAuthProvider,
authCode);
}
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/coffeemeet/server/user/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import coffeemeet.server.common.util.FileUtils;
import coffeemeet.server.user.domain.OAuthProvider;
import coffeemeet.server.user.dto.AuthInfo;
import coffeemeet.server.user.dto.MyProfileResponse;
import coffeemeet.server.user.dto.SignupRequest;
import coffeemeet.server.user.dto.UpdateProfileRequest;
import coffeemeet.server.user.dto.UserProfileResponse;
import coffeemeet.server.user.dto.MyProfileDto;
import coffeemeet.server.user.dto.SignupDto;
import coffeemeet.server.user.dto.UpdateProfileDto;
import coffeemeet.server.user.dto.UserProfileDto;
import coffeemeet.server.user.service.UserService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -28,18 +28,13 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/users")
@RequestMapping("/api/v1/users")
public class UserController {

private final UserService userService;

@GetMapping("/{userId}")
public ResponseEntity<UserProfileResponse> findUserProfile(@PathVariable long userId) {
return ResponseEntity.ok(userService.findUserProfile(userId));
}

@PostMapping("/sign-up")
public ResponseEntity<AuthTokens> signup(@Valid @RequestBody SignupRequest request) {
public ResponseEntity<AuthTokens> signup(@Valid @RequestBody SignupDto.Request request) {
return ResponseEntity.ok(userService.signup(request));
}

Expand All @@ -49,8 +44,13 @@ public ResponseEntity<AuthTokens> login(@PathVariable OAuthProvider oAuthProvide
return ResponseEntity.ok(userService.login(oAuthProvider, authCode));
}

@GetMapping("/{userId}")
public ResponseEntity<UserProfileDto.Response> findUserProfile(@PathVariable long userId) {
return ResponseEntity.ok(userService.findUserProfile(userId));
}

@GetMapping("/me")
public ResponseEntity<MyProfileResponse> findMyProfile(@Login AuthInfo authInfo) {
public ResponseEntity<MyProfileDto.Response> findMyProfile(@Login AuthInfo authInfo) {
return ResponseEntity.ok(userService.findMyProfile(authInfo.userId()));
}

Expand All @@ -67,9 +67,8 @@ public ResponseEntity<Void> updateProfileImage(

@PatchMapping("/me")
public ResponseEntity<Void> updateProfileInfo(@Login AuthInfo authInfo,
@Valid @RequestBody UpdateProfileRequest request) {
userService.updateProfileInfo(authInfo.userId(), request.nickname(), request.name(),
request.interests());
@Valid @RequestBody UpdateProfileDto.Request request) {
userService.updateProfileInfo(authInfo.userId(), request.nickname(), request.interests());
return ResponseEntity.ok().build();
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/coffeemeet/server/user/domain/Birth.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;
import lombok.NonNull;

@Getter
@Embeddable
Expand All @@ -20,21 +20,21 @@ public class Birth {
@Column(nullable = false, length = BIRTH_LENGTH)
String birthDay;

public Birth(String birthYear, String birthDay) {
public Birth(@NonNull String birthYear, @NonNull String birthDay) {
validateYear(birthYear);
validateDay(birthDay);
this.birthYear = birthYear;
this.birthDay = birthDay;
}

private void validateYear(String birthYear) {
if (!StringUtils.hasText(birthYear) || birthYear.length() != BIRTH_LENGTH) {
if (birthYear.length() != BIRTH_LENGTH) {
throw new IllegalArgumentException("올바르지 않은 연도 형식입니다.");
}
}

private void validateDay(String birthDay) {
if (!StringUtils.hasText(birthDay) || birthDay.length() != BIRTH_LENGTH) {
if (birthDay.length() != BIRTH_LENGTH) {
throw new IllegalArgumentException("올바르지 않은 날짜 형식입니다.");
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/coffeemeet/server/user/domain/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@Getter
@Embeddable
Expand All @@ -13,7 +14,7 @@ public class Email {

private String email;

public Email(String email) {
public Email(@NonNull String email) {
validateEmail(email);
this.email = email;
}
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/coffeemeet/server/user/domain/OAuthInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;
import lombok.NonNull;

@Getter
@Embeddable
Expand All @@ -18,16 +18,9 @@ public class OAuthInfo {

private String oauthProviderId;

public OAuthInfo(OAuthProvider oauthProvider, String oauthProviderId) {
validateOAuthProviderId(oauthProviderId);
public OAuthInfo(@NonNull OAuthProvider oauthProvider, @NonNull String oauthProviderId) {
this.oauthProvider = oauthProvider;
this.oauthProviderId = oauthProviderId;
}

private void validateOAuthProviderId(String oauthProviderId) {
if (!StringUtils.hasText(oauthProviderId)) {
throw new IllegalArgumentException("올바르지 않은 로그인 아이디입니다.");
}
}

}
39 changes: 10 additions & 29 deletions src/main/java/coffeemeet/server/user/domain/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;
import lombok.NonNull;

@Getter
@Embeddable
Expand Down Expand Up @@ -35,57 +35,38 @@ public class Profile {

@Builder
private Profile(
@NonNull
String name,
@NonNull
String nickname,
@NonNull
Email email,
@NonNull
String profileImageUrl,
@NonNull
Birth birth
) {
validateName(name);
validateNickname(nickname);
validateProfileImage(profileImageUrl);
this.name = name;
this.nickname = nickname;
this.email = email;
this.profileImageUrl = profileImageUrl;
this.birth = birth;
}

private void validateName(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("올바르지 않은 이름입니다.");
}
}

private void validateNickname(String nickname) {
if (!StringUtils.hasText(nickname) || nickname.length() > NICKNAME_MAX_LENGTH) {
throw new IllegalArgumentException("올바르지 않은 닉네임입니다.");
}
}

private void validateProfileImage(String profileImageUrl) {
if (!StringUtils.hasText(profileImageUrl)) {
throw new IllegalArgumentException("올바르지 않은 프로필 사진 url입니다.");
}
}

public void updateNickname(String newNickname) {
validateNickname(nickname);
this.nickname = newNickname;
}

public void updateProfileImageUrl(String newProfileImageUrl) {
validateProfileImage(profileImageUrl);
this.profileImageUrl = newProfileImageUrl;
}

public void updateEmail(String newEmail) {
this.email = new Email(newEmail);
}

public void updateName(String newName) {
validateName(newName);
this.name = newName;
private void validateNickname(String nickname) {
if (nickname.length() > NICKNAME_MAX_LENGTH) {
throw new IllegalArgumentException("올바르지 않은 닉네임입니다.");
}
}

}
9 changes: 3 additions & 6 deletions src/main/java/coffeemeet/server/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.hibernate.annotations.Where;

@Entity
Expand Down Expand Up @@ -47,8 +48,8 @@ public class User extends AdvancedBaseEntity {
private boolean isDeleted;

public User(
OAuthInfo oauthInfo,
Profile profile
@NonNull OAuthInfo oauthInfo,
@NonNull Profile profile
) {
this.oauthInfo = oauthInfo;
this.profile = profile;
Expand All @@ -64,8 +65,4 @@ public void updateNickname(String newNickname) {
this.profile.updateNickname(newNickname);
}

public void updateName(String newName) {
this.profile.updateName(newName);
}

}
Loading

0 comments on commit 254377f

Please sign in to comment.