-
Notifications
You must be signed in to change notification settings - Fork 4
[Day 3-4] Product 도메인 개발 #6
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
Open
JeongUijeong
wants to merge
20
commits into
boocam-project:JeongUijeong
Choose a base branch
from
JeongUijeong:main
base: JeongUijeong
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 all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8c3f5d8
[refactor] : UserVO을 UserDto로 변경
JeongUijeong 00d3067
[refactor] : 구글 자바 컨벤션을 적용한 코드 수정
JeongUijeong 2115ebf
[test] : 예외처리 테스트 코드 작성 및 테스트 코드 전체 수정
JeongUijeong 6a289e7
[chore] : 패키지명, 클래스명 수정
JeongUijeong f1258df
[chore] : 패키지명 수정
JeongUijeong dadbdcf
[test] : 상품 서비스 테스트 생성
JeongUijeong 4ab0506
[feat] : 상품 도메인, 서비스 생성
JeongUijeong 6a6a3c0
[test] : 테스트 이너 클래스명 수정
JeongUijeong 1a7efc7
[test] : 상품 등록 및 재고 추가 서비스 테스트
JeongUijeong a7bf914
[feat] : 새상품 등록 및 상품 재고 추가 기능 구현
JeongUijeong f5ed5b1
[test] : 빈 행 제거
JeongUijeong 0f35f07
[feat] : 관리자 여부 확인 기능 구현
JeongUijeong ebc9c03
[test] : 관리자 여부 확인 기능 테스트 추가
JeongUijeong d64056c
[test] : 상품 판매 시 재고 수정 및 판매 상태 변경 기능 테스트 추가
JeongUijeong 9217286
[feat] : 상품 판매 시 재고 수정 및 판매 상태 변경 기능 구현
JeongUijeong d1f437e
[fix] : 제품 등록 및 재고 추가 요청 도메인 양식 수정
JeongUijeong 6be7847
[test] : 상품 등록 및 재고 추가 컨트롤러 테스트 생성
JeongUijeong 9efb5d2
[feat] : 상품 등록 및 재고 추가 api 구현
JeongUijeong 9d094ae
[refactor] : 사용하지 않는 임포트문 삭제
JeongUijeong fef74b1
[fix] : 상품명 validation @NotBlank로 수정
JeongUijeong 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
This file contains hidden or 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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/swger/tddstudy/product/domain/Product.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,28 +1,53 @@ | ||
| package com.swger.tddstudy.product.domain; | ||
|
|
||
| import com.swger.tddstudy.util.BaseEntity; | ||
| import javax.persistence.Column; | ||
| import javax.persistence.Entity; | ||
| import javax.persistence.EnumType; | ||
| import javax.persistence.Enumerated; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.GenerationType; | ||
| import javax.persistence.Id; | ||
| import javax.persistence.Table; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.NonNull; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @AllArgsConstructor(access = AccessLevel.PACKAGE) | ||
| @Builder | ||
| @Entity | ||
| @Table(name = "Products") | ||
| public class Product extends BaseEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @NonNull | ||
| @Column(name = "name", nullable = false, length = 30) | ||
| private String name; | ||
|
|
||
| @NonNull | ||
| @Column(name = "price", nullable = false) | ||
| private int price; | ||
|
|
||
| @NonNull | ||
| @Column(name = "amount", nullable = false) | ||
| private int amount; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| private SellingStatus sellingStatus; | ||
|
|
||
| public ProductDto toProductDto() { | ||
| return ProductDto.builder().id(this.id).name(this.name).price(this.price) | ||
| .amount(this.amount).sellingStatus(this.sellingStatus.name()).build(); | ||
| } | ||
|
|
||
| } |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/swger/tddstudy/product/domain/ProductDto.java
This file contains hidden or 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,42 @@ | ||
| package com.swger.tddstudy.product.domain; | ||
|
|
||
| import javax.persistence.Column; | ||
| import javax.persistence.EnumType; | ||
| import javax.persistence.Enumerated; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.GenerationType; | ||
| import javax.persistence.Id; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.NonNull; | ||
| import lombok.Setter; | ||
|
|
||
| @Data | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Getter | ||
| @Setter | ||
| @Builder | ||
| public class ProductDto { | ||
|
|
||
| private Long id; | ||
| private String name; | ||
| private int price; | ||
| private int amount; | ||
| private String sellingStatus; | ||
|
|
||
| public ProductDto(String name, int price, int amount, String sellingStatus) { | ||
| this.name = name; | ||
| this.price = price; | ||
| this.amount = amount; | ||
| this.sellingStatus = sellingStatus; | ||
| } | ||
|
|
||
| public Product toEntity() { | ||
| return Product.builder().name(this.name).price(this.price).amount(this.amount) | ||
| .sellingStatus(SellingStatus.valueOf(this.sellingStatus)).build(); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/swger/tddstudy/product/exception/ProductNotFoundException.java
This file contains hidden or 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,8 @@ | ||
| package com.swger.tddstudy.product.exception; | ||
|
|
||
| public class ProductNotFoundException extends RuntimeException { | ||
|
|
||
| public ProductNotFoundException(String message) { | ||
| super(message); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/swger/tddstudy/product/exception/ProductSoldOutExcpetion.java
This file contains hidden or 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,8 @@ | ||
| package com.swger.tddstudy.product.exception; | ||
|
|
||
| public class ProductSoldOutExcpetion extends RuntimeException{ | ||
|
|
||
| public ProductSoldOutExcpetion(String message) { | ||
| super(message); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/swger/tddstudy/product/exception/UnauthorizedException.java
This file contains hidden or 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,10 @@ | ||
| package com.swger.tddstudy.product.exception; | ||
|
|
||
| import org.springframework.web.client.HttpClientErrorException.Unauthorized; | ||
|
|
||
| public class UnauthorizedException extends RuntimeException { | ||
|
|
||
| public UnauthorizedException(String message) { | ||
| super(message); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/swger/tddstudy/product/repository/ProductRepository.java
This file contains hidden or 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,11 @@ | ||
| package com.swger.tddstudy.product.repository; | ||
|
|
||
| import com.swger.tddstudy.product.domain.Product; | ||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface ProductRepository extends JpaRepository<Product, Long> { | ||
|
|
||
| @Override | ||
| Optional<Product> findById(Long id); | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/swger/tddstudy/product/request/ProductAddRequest.java
This file contains hidden or 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,34 @@ | ||
| package com.swger.tddstudy.product.request; | ||
|
|
||
| import com.swger.tddstudy.product.domain.ProductDto; | ||
| import javax.validation.constraints.Min; | ||
| import javax.validation.constraints.NotBlank; | ||
| import javax.validation.constraints.NotNull; | ||
| import javax.validation.constraints.Size; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ProductAddRequest { | ||
|
|
||
| @NotBlank(message = "상품명을 입력하세요.") | ||
| @Size(min = 2, max = 10, message = "상품명을 2자 이상 30자 이하로 입력하세요.") | ||
| private String name; | ||
|
|
||
| @NotNull(message = "상품 가격을 입력하세요.") | ||
| @Min(value = 0, message = "상품 가격은 0 이상으로 입력하세요.") | ||
| private int price; | ||
|
|
||
| @NotNull(message = "상품 수량을 입력하세요.") | ||
| @Min(value = 1, message = "상품 수량은 1 이상으로 입력하세요.") | ||
| private int amount; | ||
|
|
||
| public ProductDto toProductDto() { | ||
| return ProductDto.builder().name(this.name).price(this.price).amount(this.amount).build(); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/swger/tddstudy/product/request/ProductStockUpRequest.java
This file contains hidden or 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,23 @@ | ||
| package com.swger.tddstudy.product.request; | ||
|
|
||
| import javax.validation.constraints.Min; | ||
| import javax.validation.constraints.NotNull; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ProductStockUpRequest { | ||
|
|
||
| @NotNull(message = "상품 ID를 입력하세요.") | ||
| private Long id; | ||
|
|
||
| @NotNull(message = "상품 수량을 입력하세요.") | ||
| @Min(value = 1, message = "상품 수량은 1 이상으로 입력하세요.") | ||
| private int amount; | ||
|
|
||
| } |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/swger/tddstudy/product/restcontroller/ProductRestController.java
This file contains hidden or 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,64 @@ | ||
| package com.swger.tddstudy.product.restcontroller; | ||
|
|
||
| import com.swger.tddstudy.product.domain.Product; | ||
| import com.swger.tddstudy.product.exception.UnauthorizedException; | ||
| import com.swger.tddstudy.product.request.ProductAddRequest; | ||
| import com.swger.tddstudy.product.request.ProductStockUpRequest; | ||
| import com.swger.tddstudy.product.service.ProductService; | ||
| import com.swger.tddstudy.user.request.JoinRequest; | ||
| import com.swger.tddstudy.user.service.UserService; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpSession; | ||
| import javax.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("api/product") | ||
| public class ProductRestController { | ||
|
|
||
| private final ProductService productService; | ||
| private final UserService userService; | ||
|
|
||
| @PostMapping("/add") | ||
| public ResponseEntity<?> join(@Valid @RequestBody ProductAddRequest productAddRequest, | ||
| HttpServletRequest request) { | ||
| Map<String, Object> message = new HashMap<>(); | ||
| HttpSession session = request.getSession(false); | ||
| if (session == null) { | ||
| throw new UnauthorizedException("권한이 없습니다."); | ||
| } | ||
| if (userService.isAdmin((Long) session.getAttribute("id"))) { | ||
| message.put("status", 200); | ||
| message.put("data", productService.productAdd(productAddRequest)); | ||
| return ResponseEntity.status(HttpStatus.OK).body(message); | ||
| } else { | ||
| throw new UnauthorizedException("관리자만 상품을 등록할 수 있습니다."); | ||
| } | ||
| } | ||
|
|
||
| @PostMapping("/stock-up") | ||
| public ResponseEntity<?> join(@Valid @RequestBody ProductStockUpRequest productStockUpRequest, | ||
| HttpServletRequest request) { | ||
| Map<String, Object> message = new HashMap<>(); | ||
| HttpSession session = request.getSession(false); | ||
| if (session == null) { | ||
| throw new UnauthorizedException("권한이 없습니다."); | ||
| } | ||
| if (userService.isAdmin((Long) session.getAttribute("id"))) { | ||
| message.put("status", 200); | ||
| message.put("data", productService.productStockUp(productStockUpRequest)); | ||
| return ResponseEntity.status(HttpStatus.OK).body(message); | ||
| } else { | ||
| throw new UnauthorizedException("관리자만 상품 재고를 추가할 수 있습니다."); | ||
| } | ||
| } | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
src/main/java/com/swger/tddstudy/product/restcontroller/ProductRestControllerAdvice.java
This file contains hidden or 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,34 @@ | ||
| package com.swger.tddstudy.product.restcontroller; | ||
|
|
||
| import com.swger.tddstudy.product.exception.ProductSoldOutExcpetion; | ||
| import com.swger.tddstudy.product.exception.UnauthorizedException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import org.springframework.web.client.HttpClientErrorException.Unauthorized; | ||
|
|
||
| @RestControllerAdvice | ||
| public class ProductRestControllerAdvice { | ||
|
|
||
| @ExceptionHandler | ||
| public ResponseEntity<?> unauthorizedException(UnauthorizedException e) { | ||
| Map<String, String> errorMessage = new HashMap<>(); | ||
| errorMessage.put("error", e.getMessage()); | ||
| return ResponseEntity.status(HttpStatus.UNAUTHORIZED) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .body(errorMessage); | ||
| } | ||
|
|
||
| @ExceptionHandler | ||
| public ResponseEntity<?> productSoldOutException(ProductSoldOutExcpetion e) { | ||
| Map<String, String> errorMessage = new HashMap<>(); | ||
| errorMessage.put("error", e.getMessage()); | ||
| return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .body(errorMessage); | ||
| } | ||
| } |
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.
상품 등록만이 아닌 상품 재고 추가 부분은 생각하지 못했네요