-
Notifications
You must be signed in to change notification settings - Fork 78
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
5주차: 유효성검사 추가 및 회원관리 #88
Open
tmxhsk99
wants to merge
49
commits into
CodeSoom:tmxhsk99
Choose a base branch
from
tmxhsk99:feat-user-management
base: tmxhsk99
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 46 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
c677ea9
refactor : 패키지 구조 변경
tmxhsk99 a2ee8df
refactor : Advice 통합을 위해 삭제
tmxhsk99 e254615
refactor : Advice 통합을 위해 새로 Advice 생성
tmxhsk99 6cf8d05
test : userCreator 테스트 작성
tmxhsk99 cbedd61
test : test Depth 중복으로 작성되어 수정
tmxhsk99 64ab92a
feat : 회원 저장기능 작성
tmxhsk99 c82f079
test : assertJ사용 및 fixture 구조 수정
tmxhsk99 1cee88f
test : 회원 업데이트 테스트 작성
tmxhsk99 9e7a588
feat : 회원 업데이트 기능 작성
tmxhsk99 1c2aa84
test : 중복되는 어노테이션 삭제
tmxhsk99 aa522ce
test : UserReader 테스트 작성
tmxhsk99 eb487d4
test : 누락된 @Test 추가
tmxhsk99 85623bd
test : 단일 회원 읽기 기능 추가
tmxhsk99 be2cac9
refactor : 회원 반환시 리탄 구조 변경
tmxhsk99 1c6feef
chore : 가독성을 위한 띄어쓰기 추가
tmxhsk99 2ab0ce1
refactor : UserReader 사용으로 변경
tmxhsk99 e8efe64
refactor : return 형태 변경
tmxhsk99 fef6b4f
refactor : 테스트 필요한 준비 위치 변경
tmxhsk99 447bd82
test : 회원 삭제 테스트 작성
tmxhsk99 837db42
feat : 회원 삭제 기능 작성
tmxhsk99 5315ce9
refactor : 기존 상품 서비스 분리
tmxhsk99 9f86d7c
test : ProductCreator 테스트 작성
tmxhsk99 ba5eb76
test : ProductReader 테스트 작성
tmxhsk99 1877491
refactor : 변수명 수정
tmxhsk99 9912fd4
test : 제품 삭제 테스트 작성
tmxhsk99 3d5c073
test : 제품 수정 테스트 작성
tmxhsk99 0052a8d
refactor : 유저 요청 객체 변수명 수정
tmxhsk99 e7b6602
refactor : 컨트롤러 의존성 변경 및 테스트 변경
tmxhsk99 c2d1915
chore : 오타 수정
tmxhsk99 a19d7c0
chore : 불필요한 의존성 및 패키지 경로 수정에 따른수정
tmxhsk99 526053c
chore : 불필요한 의존성 및 패키지 경로 수정에 따른수정
tmxhsk99 673231e
feat : validation 메시지 추가
tmxhsk99 0ade4a5
feat : 상품 검증 에러 관련 로직및 테스트 수정
tmxhsk99 d64f8bc
refactor : 중복되거나 너무 자세한 변수명,메서드명 수정
tmxhsk99 0b03c55
chore : 패키지 이동
tmxhsk99 d3c52c4
test : UserController 유저 생성 테스트 작성
tmxhsk99 4a38d3b
test : UserController 유저 생성 기능 작성
tmxhsk99 1d999b9
test : UserController 유저 수정 기능 작성
tmxhsk99 6484f61
chore : 오타 수정
tmxhsk99 6a7729a
feat : UserController 유저 수정기능 작성 및 테스트 수정
tmxhsk99 bd6881b
test : UserController 유저 삭제 테스트 작성
tmxhsk99 8bd4a18
refactor : 메서드 명 수정
tmxhsk99 49fe211
feat : UserController 유저 삭제 기능 추가
tmxhsk99 bcae44a
test : 유저 예외 테스트 추가
tmxhsk99 5ea2b96
feat : 유저 검증 기능 추가
tmxhsk99 e9aaef7
test : 유저 요청 검증 추가
tmxhsk99 e5e4d16
refactor : 유저 컨트롤러 분리
tmxhsk99 7952552
refactor : 프로덕트컨트롤러 분리
tmxhsk99 9cb7e38
test : ParameterizedTest 활용 예외 테스트 추가
tmxhsk99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 0 additions & 7 deletions
7
app/src/main/java/com/codesoom/assignment/ProductNotFoundException.java
This file was deleted.
Oops, something went wrong.
64 changes: 0 additions & 64 deletions
64
app/src/main/java/com/codesoom/assignment/application/ProductService.java
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/codesoom/assignment/application/common/exception/BaseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.codesoom.assignment.application.common.exception; | ||
|
||
import com.codesoom.assignment.dto.ErrorResponse; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public abstract class BaseException extends RuntimeException { | ||
public final List<ErrorValidation> errors = new ArrayList<>(); | ||
|
||
public BaseException() { | ||
} | ||
|
||
public BaseException(String message) { | ||
super(message); | ||
} | ||
|
||
public abstract int getStatusCode(); | ||
|
||
public void addValidation(String source, String type, String message) { | ||
errors.add(new ErrorValidation(source, type, message)); | ||
} | ||
|
||
public ErrorResponse toErrorResponse() { | ||
return new ErrorResponse( | ||
String.valueOf(getStatusCode()), | ||
getMessage(), | ||
errors | ||
); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/codesoom/assignment/application/common/exception/ErrorValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.codesoom.assignment.application.common.exception; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ErrorValidation { | ||
private final String source; | ||
private final String type; | ||
private final String message; | ||
|
||
@Builder | ||
public ErrorValidation(String source, String type, String message) { | ||
this.source = source; | ||
this.type = type; | ||
this.message = message; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/codesoom/assignment/application/product/ProductCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/codesoom/assignment/application/product/ProductDeleter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/codesoom/assignment/application/product/ProductReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.codesoom.assignment.application.product; | ||
|
||
import com.codesoom.assignment.infra.product.exception.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()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/codesoom/assignment/application/product/ProductUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/codesoom/assignment/application/user/UserCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.UserData; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserCreator { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public UserCreator(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
public User createUser(UserData userRequest) { | ||
return userRepository.save(userRequest.toUser()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/codesoom/assignment/application/user/UserDeleter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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 delete(Long id) { | ||
User user = userReader.getUser(id); | ||
userRepository.deleteById(user.getId()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/codesoom/assignment/application/user/UserReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("해당하는 유저가 없습니다.")); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/codesoom/assignment/application/user/UserUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.UserData; | ||
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 update(Long id, UserData userRequest) { | ||
User user = userReader.getUser(id); | ||
user.change(userRequest.getName(), userRequest.getEmail(), userRequest.getPassword()); | ||
|
||
return userRepository.save(user); | ||
|
||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/codesoom/assignment/controllers/ExceptionAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.codesoom.assignment.controllers; | ||
|
||
import com.codesoom.assignment.application.common.exception.ErrorValidation; | ||
import com.codesoom.assignment.infra.product.exception.ProductNotFoundException; | ||
import com.codesoom.assignment.dto.ErrorResponse; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@ControllerAdvice | ||
public class ExceptionAdvice { | ||
@ResponseBody | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
@ExceptionHandler(ProductNotFoundException.class) | ||
public ErrorResponse handleProductTaskNotFound() { | ||
return new ErrorResponse("해당하는 상품이 존재하지 않습니다"); | ||
} | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseBody | ||
public ErrorResponse InvalidRequestHandler(MethodArgumentNotValidException e) { | ||
|
||
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors(); | ||
List<ErrorValidation> errors = new ArrayList<>(); | ||
|
||
for (FieldError fieldError : fieldErrors) { | ||
errors.add(new ErrorValidation(fieldError.getField(),"BAD_REQUEST",fieldError.getDefaultMessage())); | ||
} | ||
|
||
ErrorResponse response = ErrorResponse.builder() | ||
.code("400") | ||
.message("잘못된 요청입니다.") | ||
.errors(errors) | ||
.build(); | ||
|
||
return response; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
위에 빈 줄 하나를 추가해서 생성자와 속성을 분리하면 좋을 것 같아요