Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8c3f5d8
[refactor] : UserVO을 UserDto로 변경
JeongUijeong Aug 3, 2023
00d3067
[refactor] : 구글 자바 컨벤션을 적용한 코드 수정
JeongUijeong Aug 3, 2023
2115ebf
[test] : 예외처리 테스트 코드 작성 및 테스트 코드 전체 수정
JeongUijeong Aug 3, 2023
6a289e7
[chore] : 패키지명, 클래스명 수정
JeongUijeong Aug 4, 2023
f1258df
[chore] : 패키지명 수정
JeongUijeong Aug 4, 2023
dadbdcf
[test] : 상품 서비스 테스트 생성
JeongUijeong Aug 4, 2023
4ab0506
[feat] : 상품 도메인, 서비스 생성
JeongUijeong Aug 4, 2023
6a6a3c0
[test] : 테스트 이너 클래스명 수정
JeongUijeong Aug 4, 2023
1a7efc7
[test] : 상품 등록 및 재고 추가 서비스 테스트
JeongUijeong Aug 4, 2023
a7bf914
[feat] : 새상품 등록 및 상품 재고 추가 기능 구현
JeongUijeong Aug 4, 2023
f5ed5b1
[test] : 빈 행 제거
JeongUijeong Aug 4, 2023
0f35f07
[feat] : 관리자 여부 확인 기능 구현
JeongUijeong Aug 4, 2023
ebc9c03
[test] : 관리자 여부 확인 기능 테스트 추가
JeongUijeong Aug 4, 2023
d64056c
[test] : 상품 판매 시 재고 수정 및 판매 상태 변경 기능 테스트 추가
JeongUijeong Aug 4, 2023
9217286
[feat] : 상품 판매 시 재고 수정 및 판매 상태 변경 기능 구현
JeongUijeong Aug 4, 2023
d1f437e
[fix] : 제품 등록 및 재고 추가 요청 도메인 양식 수정
JeongUijeong Aug 4, 2023
6be7847
[test] : 상품 등록 및 재고 추가 컨트롤러 테스트 생성
JeongUijeong Aug 4, 2023
9efb5d2
[feat] : 상품 등록 및 재고 추가 api 구현
JeongUijeong Aug 4, 2023
9d094ae
[refactor] : 사용하지 않는 임포트문 삭제
JeongUijeong Aug 4, 2023
fef74b1
[fix] : 상품명 validation @NotBlank로 수정
JeongUijeong 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
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
compileOnly 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'com.fasterxml.jackson.core:jackson-core:2.11.4'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.11.4'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.4'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.11.4'
runtimeOnly 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation "com.fasterxml.jackson.core:jackson-core:2.11.4"
implementation "com.fasterxml.jackson.core:jackson-annotations:2.11.4"
implementation "com.fasterxml.jackson.core:jackson-databind:2.11.4"
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.11.4"
}

tasks.named('test') {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/swger/tddstudy/product/domain/Product.java
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 src/main/java/com/swger/tddstudy/product/domain/ProductDto.java
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();
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
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();
}
}
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;

}
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")

Choose a reason for hiding this comment

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

상품 등록만이 아닌 상품 재고 추가 부분은 생각하지 못했네요

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("관리자만 상품 재고를 추가할 수 있습니다.");
}
}
}
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);
}
}
Loading