Skip to content
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
wants to merge 49 commits into
base: tmxhsk99
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
c677ea9
refactor : 패키지 구조 변경
tmxhsk99 Jul 31, 2023
a2ee8df
refactor : Advice 통합을 위해 삭제
tmxhsk99 Jul 31, 2023
e254615
refactor : Advice 통합을 위해 새로 Advice 생성
tmxhsk99 Jul 31, 2023
6cf8d05
test : userCreator 테스트 작성
tmxhsk99 Jul 31, 2023
cbedd61
test : test Depth 중복으로 작성되어 수정
tmxhsk99 Jul 31, 2023
64ab92a
feat : 회원 저장기능 작성
tmxhsk99 Jul 31, 2023
c82f079
test : assertJ사용 및 fixture 구조 수정
tmxhsk99 Jul 31, 2023
1cee88f
test : 회원 업데이트 테스트 작성
tmxhsk99 Jul 31, 2023
9e7a588
feat : 회원 업데이트 기능 작성
tmxhsk99 Jul 31, 2023
1c2aa84
test : 중복되는 어노테이션 삭제
tmxhsk99 Aug 1, 2023
aa522ce
test : UserReader 테스트 작성
tmxhsk99 Aug 1, 2023
eb487d4
test : 누락된 @Test 추가
tmxhsk99 Aug 1, 2023
85623bd
test : 단일 회원 읽기 기능 추가
tmxhsk99 Aug 1, 2023
be2cac9
refactor : 회원 반환시 리탄 구조 변경
tmxhsk99 Aug 2, 2023
1c6feef
chore : 가독성을 위한 띄어쓰기 추가
tmxhsk99 Aug 2, 2023
2ab0ce1
refactor : UserReader 사용으로 변경
tmxhsk99 Aug 2, 2023
e8efe64
refactor : return 형태 변경
tmxhsk99 Aug 2, 2023
fef6b4f
refactor : 테스트 필요한 준비 위치 변경
tmxhsk99 Aug 2, 2023
447bd82
test : 회원 삭제 테스트 작성
tmxhsk99 Aug 2, 2023
837db42
feat : 회원 삭제 기능 작성
tmxhsk99 Aug 2, 2023
5315ce9
refactor : 기존 상품 서비스 분리
tmxhsk99 Aug 2, 2023
9f86d7c
test : ProductCreator 테스트 작성
tmxhsk99 Aug 2, 2023
ba5eb76
test : ProductReader 테스트 작성
tmxhsk99 Aug 2, 2023
1877491
refactor : 변수명 수정
tmxhsk99 Aug 3, 2023
9912fd4
test : 제품 삭제 테스트 작성
tmxhsk99 Aug 3, 2023
3d5c073
test : 제품 수정 테스트 작성
tmxhsk99 Aug 3, 2023
0052a8d
refactor : 유저 요청 객체 변수명 수정
tmxhsk99 Aug 3, 2023
e7b6602
refactor : 컨트롤러 의존성 변경 및 테스트 변경
tmxhsk99 Aug 3, 2023
c2d1915
chore : 오타 수정
tmxhsk99 Aug 3, 2023
a19d7c0
chore : 불필요한 의존성 및 패키지 경로 수정에 따른수정
tmxhsk99 Aug 3, 2023
526053c
chore : 불필요한 의존성 및 패키지 경로 수정에 따른수정
tmxhsk99 Aug 3, 2023
673231e
feat : validation 메시지 추가
tmxhsk99 Aug 3, 2023
0ade4a5
feat : 상품 검증 에러 관련 로직및 테스트 수정
tmxhsk99 Aug 3, 2023
d64f8bc
refactor : 중복되거나 너무 자세한 변수명,메서드명 수정
tmxhsk99 Aug 4, 2023
0b03c55
chore : 패키지 이동
tmxhsk99 Aug 4, 2023
d3c52c4
test : UserController 유저 생성 테스트 작성
tmxhsk99 Aug 4, 2023
4a38d3b
test : UserController 유저 생성 기능 작성
tmxhsk99 Aug 5, 2023
1d999b9
test : UserController 유저 수정 기능 작성
tmxhsk99 Aug 5, 2023
6484f61
chore : 오타 수정
tmxhsk99 Aug 5, 2023
6a7729a
feat : UserController 유저 수정기능 작성 및 테스트 수정
tmxhsk99 Aug 5, 2023
bd6881b
test : UserController 유저 삭제 테스트 작성
tmxhsk99 Aug 5, 2023
8bd4a18
refactor : 메서드 명 수정
tmxhsk99 Aug 5, 2023
49fe211
feat : UserController 유저 삭제 기능 추가
tmxhsk99 Aug 5, 2023
bcae44a
test : 유저 예외 테스트 추가
tmxhsk99 Aug 5, 2023
5ea2b96
feat : 유저 검증 기능 추가
tmxhsk99 Aug 5, 2023
e9aaef7
test : 유저 요청 검증 추가
tmxhsk99 Aug 5, 2023
e5e4d16
refactor : 유저 컨트롤러 분리
tmxhsk99 Aug 5, 2023
7952552
refactor : 프로덕트컨트롤러 분리
tmxhsk99 Aug 5, 2023
9cb7e38
test : ParameterizedTest 활용 예외 테스트 추가
tmxhsk99 Aug 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

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
);
}
}
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;
}
}
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.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());
}
}
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.UserData;
import org.springframework.stereotype.Service;

@Service
public class UserCreator {

private final UserRepository userRepository;

public UserCreator(UserRepository userRepository) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위에 빈 줄 하나를 추가해서 생성자와 속성을 분리하면 좋을 것 같아요

this.userRepository = userRepository;
}

public User createUser(UserData userRequest) {
return userRepository.save(userRequest.toUser());
}
}
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());
}
}
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.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);

}
}
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;
}

}
Loading