-
Notifications
You must be signed in to change notification settings - Fork 77
5주차: 유효성검사 추가 및 회원관리 #88
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
base: tmxhsk99
Are you sure you want to change the base?
Changes from 13 commits
c677ea9
a2ee8df
e254615
6cf8d05
cbedd61
64ab92a
c82f079
1cee88f
9e7a588
1c2aa84
aa522ce
eb487d4
85623bd
be2cac9
1c6feef
2ab0ce1
e8efe64
fef6b4f
447bd82
837db42
5315ce9
9f86d7c
ba5eb76
1877491
9912fd4
3d5c073
0052a8d
e7b6602
c2d1915
a19d7c0
526053c
673231e
0ade4a5
d64f8bc
0b03c55
d3c52c4
4a38d3b
1d999b9
6484f61
6a7729a
bd6881b
8bd4a18
49fe211
bcae44a
5ea2b96
e9aaef7
e5e4d16
7952552
9cb7e38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.codesoom.assignment.application.user; | ||
|
|
||
| import com.codesoom.assignment.domain.user.User; | ||
| import com.codesoom.assignment.domain.user.UserRepository; | ||
| import com.codesoom.assignment.dto.user.UserRequest; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class UserCreator { | ||
| private final UserRepository userRepository; | ||
| public UserCreator(UserRepository userRepository) { | ||
| this.userRepository = userRepository; | ||
| } | ||
|
|
||
| public User createUser(UserRequest userRequest) { | ||
| return userRepository.save(userRequest.toUser()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
|
|
||
| package com.codesoom.assignment.application.user; | ||
|
|
||
| import com.codesoom.assignment.domain.user.User; | ||
| import com.codesoom.assignment.domain.user.UserRepository; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class UserReader { | ||
| private final UserRepository userRepository; | ||
|
|
||
| public UserReader(UserRepository userRepository) { | ||
| this.userRepository = userRepository; | ||
| } | ||
|
|
||
| public User getUser(Long id) { | ||
| User user = userRepository.findById(id) | ||
| .orElseThrow(() -> new IllegalArgumentException("해당하는 유저가 없습니다.")); | ||
| return user; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.codesoom.assignment.application.user; | ||
|
|
||
| import com.codesoom.assignment.domain.user.User; | ||
| import com.codesoom.assignment.domain.user.UserRepository; | ||
| import com.codesoom.assignment.dto.user.UserRequest; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import javax.transaction.Transactional; | ||
|
|
||
| @Service | ||
| public class UserUpdater { | ||
| private UserRepository userRepository; | ||
|
|
||
| public UserUpdater(UserRepository userRepository) { | ||
| this.userRepository = userRepository; | ||
| } | ||
|
|
||
| @Transactional | ||
| public User updateUser(Long id, UserRequest userRequest) { | ||
| User user = userRepository.findById(id) | ||
| .orElseThrow(() -> new IllegalArgumentException("해당하는 유저가 없습니다.")); | ||
|
||
| user.change(userRequest.getName(), userRequest.getEmail(), userRequest.getPassword()); | ||
|
|
||
| User updatedUser = userRepository.save(user); | ||
| return updatedUser; | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.codesoom.assignment.domain.user; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import javax.persistence.Entity; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.Id; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Builder | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class User { | ||
| @Id | ||
| @GeneratedValue | ||
| private Long id; | ||
|
|
||
| private String name; | ||
|
|
||
| private String email; | ||
|
|
||
| private String password; | ||
|
|
||
| public void change(String name, String email, String password) { | ||
| this.name = name; | ||
| this.email = email; | ||
| this.password = password; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.codesoom.assignment.domain.user; | ||
|
|
||
| import com.codesoom.assignment.domain.product.Product; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface UserRepository { | ||
| User save(User user); | ||
|
|
||
| Optional<User> findById(Long id); | ||
|
|
||
| void delete(User user); | ||
|
|
||
| void deleteAll(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package com.codesoom.assignment.dto; | ||
| package com.codesoom.assignment.dto.product; | ||
|
|
||
| import lombok.*; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.codesoom.assignment.dto.user; | ||
|
|
||
| import com.codesoom.assignment.domain.user.User; | ||
| import lombok.*; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @Builder | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class UserRequest { | ||
| private String name; | ||
| private String email; | ||
| private String password; | ||
|
|
||
| public User toUser() { | ||
| return User.builder() | ||
| .name(name) | ||
| .email(email) | ||
| .password(password) | ||
| .build(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.codesoom.assignment.infra.user; | ||
|
|
||
| import com.codesoom.assignment.domain.user.User; | ||
| import com.codesoom.assignment.domain.user.UserRepository; | ||
| import org.springframework.context.annotation.Primary; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.repository.CrudRepository; | ||
|
|
||
| @Primary | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
이번 기회에 https://tech.kakaopay.com/post/martin-dev-honey-tip-2/ 이 글을 한 번 읽어봅시다 |
||
| public interface JpaUserRepository extends | ||
| UserRepository, CrudRepository<User, Long> { | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.codesoom.assignment.application; | ||
|
|
||
| import com.codesoom.assignment.domain.user.UserRepository; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
|
|
||
| @DataJpaTest | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| public class JpaTest { | ||
| @Autowired | ||
| public UserRepository userRepository; | ||
|
|
||
| public UserRepository getUserRepository() { | ||
| return userRepository; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.codesoom.assignment.application.user; | ||
|
|
||
| import com.codesoom.assignment.application.JpaTest; | ||
| import com.codesoom.assignment.domain.user.User; | ||
| import com.codesoom.assignment.dto.user.UserRequest; | ||
| import org.junit.jupiter.api.*; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
|
|
||
| @SuppressWarnings({"InnerClassMayBeStatic", "NonAsciiCharacters"}) | ||
| @DisplayName("UserCreator 클래스") | ||
| class UserCreatorTest extends JpaTest { | ||
| private final String TEST_NAME = "testName"; | ||
| private final String TEST_EMAIL = "test@Email.com"; | ||
| private final String TEST_PASSWORD = "testPassword"; | ||
|
|
||
| private UserRequest createUserRequest() { | ||
| return new UserRequest( | ||
| TEST_NAME, | ||
| TEST_EMAIL, | ||
| TEST_PASSWORD | ||
| ); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
| class createUser_메서드는 { | ||
| @Nested | ||
| @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
| class 유저_정보가_주어지면 { | ||
| private UserRequest USER_REQUEST; | ||
| private UserCreator userCreator; | ||
|
||
| @BeforeEach | ||
| void setUp() { | ||
| USER_REQUEST = createUserRequest(); | ||
| userCreator = new UserCreator(getUserRepository()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("해당_유저정보를_저장_후_저장한_유저정보를_리턴한다") | ||
| void it_saves_and_returns_user() { | ||
| User user = userCreator.createUser(USER_REQUEST); | ||
|
|
||
| Assertions.assertThat(user.getName()).isEqualTo(TEST_NAME); | ||
| Assertions.assertThat(user.getEmail()).isEqualTo(TEST_EMAIL); | ||
| Assertions.assertThat(user.getPassword()).isEqualTo(TEST_PASSWORD); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.codesoom.assignment.application.user; | ||
|
|
||
| import com.codesoom.assignment.application.JpaTest; | ||
| import com.codesoom.assignment.dto.user.UserRequest; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.*; | ||
|
|
||
|
|
||
| @SuppressWarnings({"InnerClassMayBeStatic", "NonAsciiCharacters"}) | ||
| @DisplayName("UserReader 클래스") | ||
| class UserReaderTest extends JpaTest { | ||
| private final String TEST_NAME = "testName"; | ||
| private final String TEST_EMAIL = "test@Email.com"; | ||
| private final String TEST_PASSWORD = "testPassword"; | ||
|
|
||
| private UserRequest createUserRequest() { | ||
| return new UserRequest( | ||
| TEST_NAME, | ||
| TEST_EMAIL, | ||
| TEST_PASSWORD | ||
| ); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
| class getUser_메서드는 { | ||
| @Nested | ||
| @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
| class 유저_아이디가_주어지면 { | ||
| private UserRequest USER_REQUEST; | ||
| private Long savedUserId; | ||
| private UserReader userReader; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| USER_REQUEST = createUserRequest(); | ||
| savedUserId = getUserRepository().save(USER_REQUEST.toUser()).getId(); | ||
| userReader = new UserReader(getUserRepository()); | ||
| } | ||
|
|
||
| @DisplayName("해당_유저정보를_리턴한다") | ||
| @Test | ||
| void it_returns_user() { | ||
| Assertions.assertThat(userReader.getUser(savedUserId).getName()).isEqualTo(TEST_NAME); | ||
| Assertions.assertThat(userReader.getUser(savedUserId).getEmail()).isEqualTo(TEST_EMAIL); | ||
| Assertions.assertThat(userReader.getUser(savedUserId).getPassword()).isEqualTo(TEST_PASSWORD); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
위에 빈 줄 하나를 추가해서 생성자와 속성을 분리하면 좋을 것 같아요