-
Notifications
You must be signed in to change notification settings - Fork 76
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 23 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,25 @@ | ||
| package com.codesoom.assignment.application.product; | ||
|
|
||
| import com.codesoom.assignment.domain.product.Product; | ||
| import com.codesoom.assignment.domain.product.ProductRepository; | ||
| import com.codesoom.assignment.dto.product.ProductData; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class ProductCreator { | ||
| private final ProductRepository productRepository; | ||
|
|
||
| public ProductCreator(ProductRepository productRepository) { | ||
| this.productRepository = productRepository; | ||
| } | ||
|
|
||
| public Product createProduct(ProductData productData) { | ||
| Product product = Product.builder() | ||
| .name(productData.getName()) | ||
| .maker(productData.getMaker()) | ||
| .price(productData.getPrice()) | ||
| .imageUrl(productData.getImageUrl()) | ||
| .build(); | ||
| return productRepository.save(product); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.codesoom.assignment.application.product; | ||
|
|
||
| import com.codesoom.assignment.domain.product.Product; | ||
| import com.codesoom.assignment.domain.product.ProductRepository; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class ProductDeleter { | ||
|
|
||
| private final ProductRepository productRepository; | ||
| private final ProductReader productReader; | ||
|
|
||
| public ProductDeleter(ProductRepository productRepository, ProductReader productReader) { | ||
| this.productRepository = productRepository; | ||
| this.productReader = productReader; | ||
| } | ||
|
|
||
| public Product deleteProduct(Long id) { | ||
| Product product = productReader.getProduct(id); | ||
|
|
||
| productRepository.delete(product); | ||
|
|
||
| return product; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.codesoom.assignment.application.product; | ||
|
|
||
| import com.codesoom.assignment.ProductNotFoundException; | ||
| import com.codesoom.assignment.domain.product.Product; | ||
| import com.codesoom.assignment.domain.product.ProductRepository; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class ProductReader { | ||
|
|
||
| private final ProductRepository productRepository; | ||
|
|
||
| public ProductReader(ProductRepository productRepository) { | ||
| this.productRepository = productRepository; | ||
| } | ||
|
|
||
| public List<Product> getProducts() { | ||
| return productRepository.findAll(); | ||
| } | ||
|
|
||
| public Product getProduct(Long id) { | ||
| return findProduct(id); | ||
| } | ||
|
|
||
| private Product findProduct(Long id) { | ||
| return productRepository.findById(id) | ||
| .orElseThrow(() -> new ProductNotFoundException(id)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.codesoom.assignment.application.product; | ||
|
|
||
| import com.codesoom.assignment.domain.product.Product; | ||
| import com.codesoom.assignment.domain.product.ProductRepository; | ||
| import com.codesoom.assignment.dto.product.ProductData; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import javax.transaction.Transactional; | ||
|
|
||
| @Service | ||
| public class ProductUpdater { | ||
|
|
||
| private final ProductRepository productRepository; | ||
| private final ProductReader productReader; | ||
|
|
||
| public ProductUpdater(ProductRepository productRepository, ProductReader productReader) { | ||
| this.productRepository = productRepository; | ||
| this.productReader = productReader; | ||
| } | ||
|
|
||
| @Transactional | ||
| public Product updateProduct(Long id, ProductData productData) { | ||
| Product product = productReader.getProduct(id); | ||
|
|
||
| product.change( | ||
| productData.getName(), | ||
| productData.getMaker(), | ||
| productData.getPrice(), | ||
| productData.getImageUrl() | ||
| ); | ||
|
|
||
| return productRepository.save(product); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 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 UserDeleter { | ||
| private final UserRepository userRepository; | ||
|
|
||
| private final UserReader userReader; | ||
|
|
||
| public UserDeleter(UserRepository userRepository, UserReader userReader) { | ||
| this.userRepository = userRepository; | ||
| this.userReader = userReader; | ||
| } | ||
| public void deleteUser(Long id) { | ||
| User findeduser = userReader.getUser(id); | ||
|
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. foundUser라고 할 수 있겠네요. 혹은 단순히 user라고 해도 될 것 같아요. 왜냐하면 변수의 스코프가 짧아서 단순해도 괜찮을 것 같아요. |
||
| userRepository.deleteById(findeduser.getId()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
|
|
||
| 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) { | ||
| return userRepository.findById(id) | ||
| .orElseThrow(() -> new IllegalArgumentException("해당하는 유저가 없습니다.")); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 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; | ||
|
|
||
| private UserReader userReader; | ||
|
|
||
| public UserUpdater(UserRepository userRepository, UserReader userReader) { | ||
| this.userRepository = userRepository; | ||
| this.userReader = userReader; | ||
| } | ||
|
|
||
| @Transactional | ||
| public User updateUser(Long id, UserRequest userRequest) { | ||
| User user = userReader.getUser(id); | ||
| user.change(userRequest.getName(), userRequest.getEmail(), userRequest.getPassword()); | ||
|
|
||
| return userRepository.save(user); | ||
|
|
||
| } | ||
| } |
| 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 deleteById(Long id); | ||
|
|
||
| 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,23 @@ | ||
| package com.codesoom.assignment.application; | ||
|
|
||
| import com.codesoom.assignment.domain.product.ProductRepository; | ||
| import com.codesoom.assignment.domain.user.UserRepository; | ||
| import org.checkerframework.checker.units.qual.A; | ||
| 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; | ||
| @Autowired | ||
| public ProductRepository productRepository; | ||
|
|
||
| public UserRepository getUserRepository() { | ||
| return userRepository; | ||
| } | ||
|
|
||
| public ProductRepository getProductRepository() { | ||
| return productRepository; | ||
| } | ||
| } | ||
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.
위에 빈 줄 하나를 추가해서 생성자와 속성을 분리하면 좋을 것 같아요