|
1 | | - package org.ezcode.codetest.domain.user.service; |
| 1 | +package org.ezcode.codetest.domain.user.service; |
2 | 2 |
|
3 | 3 | import org.ezcode.codetest.domain.user.model.enums.Adjective; |
4 | 4 | import org.ezcode.codetest.domain.user.model.enums.Noun; |
|
9 | 9 | import org.ezcode.codetest.common.security.util.PasswordEncoder; |
10 | 10 | import org.springframework.stereotype.Service; |
11 | 11 |
|
12 | | - import org.springframework.transaction.annotation.Transactional; |
13 | | - import lombok.RequiredArgsConstructor; |
14 | | - import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.springframework.transaction.annotation.Transactional; |
| 13 | +import lombok.RequiredArgsConstructor; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
15 | 15 |
|
16 | | - @Slf4j |
17 | | - @Service |
18 | | - @RequiredArgsConstructor |
19 | | - @Transactional |
20 | | - public class UserDomainService { |
21 | | - private final UserRepository userRepository; |
22 | | - private final PasswordEncoder passwordEncoder; |
23 | | - private static final java.util.Random RANDOM = new java.util.Random(); |
| 16 | +@Slf4j |
| 17 | +@Service |
| 18 | +@RequiredArgsConstructor |
| 19 | +@Transactional |
| 20 | +public class UserDomainService { |
| 21 | + private final UserRepository userRepository; |
| 22 | + private final PasswordEncoder passwordEncoder; |
| 23 | + private static final java.util.Random RANDOM = new java.util.Random(); |
24 | 24 |
|
25 | | - public void checkEmailUnique(String email) { |
26 | | - if (userRepository.findByEmail(email).isPresent()) { |
27 | | - throw new AuthException(AuthExceptionCode.ALREADY_EXIST_USER); |
28 | | - } |
| 25 | + public void checkEmailUnique(String email) { |
| 26 | + if (userRepository.findByEmail(email).isPresent()) { |
| 27 | + throw new AuthException(AuthExceptionCode.ALREADY_EXIST_USER); |
29 | 28 | } |
| 29 | + } |
30 | 30 |
|
31 | | - public void createUser(User user) { |
32 | | - userRepository.createUser(user); |
33 | | - } |
| 31 | + public void createUser(User user) { |
| 32 | + userRepository.createUser(user); |
| 33 | + } |
34 | 34 |
|
35 | | - public User getUser(String email) { |
36 | | - return userRepository.findByEmail(email) |
37 | | - .orElseThrow(() -> new AuthException(AuthExceptionCode.USER_NOT_FOUND)); |
38 | | - } |
| 35 | + public User getUser(String email) { |
| 36 | + return userRepository.findByEmail(email) |
| 37 | + .orElseThrow(() -> new AuthException(AuthExceptionCode.USER_NOT_FOUND)); |
| 38 | + } |
39 | 39 |
|
40 | | - public User getUserById(Long id) { |
41 | | - return userRepository.findUserById(id) |
42 | | - .orElseThrow(()->new AuthException(AuthExceptionCode.USER_NOT_FOUND)); |
43 | | - } |
| 40 | + public User getUserById(Long id) { |
| 41 | + return userRepository.findUserById(id) |
| 42 | + .orElseThrow(()->new AuthException(AuthExceptionCode.USER_NOT_FOUND)); |
| 43 | + } |
44 | 44 |
|
45 | | - public void userPasswordCheck(String email, String password) { |
46 | | - User user = userRepository.findByEmail(email) |
47 | | - .orElseThrow(() -> new AuthException(AuthExceptionCode.USER_NOT_FOUND));; |
| 45 | + public void userPasswordCheck(String email, String password) { |
| 46 | + User user = userRepository.findByEmail(email) |
| 47 | + .orElseThrow(() -> new AuthException(AuthExceptionCode.USER_NOT_FOUND));; |
48 | 48 |
|
49 | | - if (!passwordEncoder.matches(password, user.getPassword())) { |
50 | | - throw new AuthException(AuthExceptionCode.PASSWORD_NOT_MATCH); |
51 | | - } |
| 49 | + if (!passwordEncoder.matches(password, user.getPassword())) { |
| 50 | + throw new AuthException(AuthExceptionCode.PASSWORD_NOT_MATCH); |
52 | 51 | } |
| 52 | + } |
53 | 53 |
|
54 | | - public String encodePassword(String password) { |
55 | | - return passwordEncoder.encode(password); |
56 | | - } |
| 54 | + public String encodePassword(String password) { |
| 55 | + return passwordEncoder.encode(password); |
| 56 | + } |
57 | 57 |
|
58 | | - public User getOAuthUser(String email, String provider) { |
59 | | - return userRepository.findByEmailAndProvider(email, provider); |
60 | | - } |
| 58 | + public User getOAuthUser(String email, String provider) { |
| 59 | + return userRepository.findByEmailAndProvider(email, provider); |
| 60 | + } |
61 | 61 |
|
62 | | - public void passwordComparison(String newPassword, String oldPassword) { |
63 | | - if (passwordEncoder.matches(newPassword, oldPassword)) { |
64 | | - throw new AuthException(AuthExceptionCode.PASSWORD_IS_SAME); |
65 | | - } |
| 62 | + public void passwordComparison(String newPassword, String oldPassword) { |
| 63 | + if (passwordEncoder.matches(newPassword, oldPassword)) { |
| 64 | + throw new AuthException(AuthExceptionCode.PASSWORD_IS_SAME); |
66 | 65 | } |
| 66 | + } |
67 | 67 |
|
68 | | - public void isDeletedUser(User user) { |
69 | | - if (user.isDeleted()) { |
70 | | - throw new AuthException(AuthExceptionCode.ALREADY_WITHDRAW_USER); |
71 | | - } |
| 68 | + public void isDeletedUser(User user) { |
| 69 | + if (user.isDeleted()) { |
| 70 | + throw new AuthException(AuthExceptionCode.ALREADY_WITHDRAW_USER); |
72 | 71 | } |
| 72 | + } |
73 | 73 |
|
74 | | - public String generateUniqueNickname() { |
75 | | - for (int i = 0; i < 10000000; i++) { |
76 | | - String nickname = generateRandomNickname(); |
77 | | - if(!userRepository.existsByNickname(nickname)) { |
78 | | - return nickname; |
79 | | - } |
| 74 | + public String generateUniqueNickname() { |
| 75 | + for (int i = 0; i < 10000000; i++) { |
| 76 | + String nickname = generateRandomNickname(); |
| 77 | + if(!userRepository.existsByNickname(nickname)) { |
| 78 | + return nickname; |
80 | 79 | } |
81 | | - throw new IllegalStateException("중복된 닉네임 생성 불가"); |
82 | 80 | } |
| 81 | + throw new IllegalStateException("중복된 닉네임 생성 불가"); |
| 82 | + } |
83 | 83 |
|
84 | | - private static String generateRandomNickname() { |
85 | | - Adjective adjective = Adjective.values()[RANDOM.nextInt(Adjective.values().length)]; |
86 | | - Noun noun = Noun.values()[RANDOM.nextInt(Noun.values().length)]; |
87 | | - int number = RANDOM.nextInt(1000); |
88 | | - return adjective.name() + noun.name() + number; |
89 | | - } |
| 84 | + private static String generateRandomNickname() { |
| 85 | + Adjective adjective = Adjective.values()[RANDOM.nextInt(Adjective.values().length)]; |
| 86 | + Noun noun = Noun.values()[RANDOM.nextInt(Noun.values().length)]; |
| 87 | + int number = RANDOM.nextInt(1000); |
| 88 | + return adjective.name() + noun.name() + number; |
90 | 89 | } |
| 90 | +} |
0 commit comments