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

강원대 BE_김동혁6주차 과제 (1단계) #154

Open
wants to merge 8 commits into
base: kdonghs
Choose a base branch
from
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,29 @@
- 카카오 토큰 만료 여부는 카카오로부터 조회시 검증한다.
- 함수명은 소문자로 시작해야 함
- 주석 들여쓰기 수정
- HttpHeaders는 전역변수로 구현할 필요 없음
- HttpHeaders는 전역변수로 구현할 필요 없음

## step1 구현 기능

- 1명의 백엔드 코드를 기준으로 api명세서 통합
- 기존 fe이 사용하던 명세서는 요청 request만 존재하고 response는 명시되어 있지 않음
- 따라서 response의 형식은 자유임
- 선정결과 본인(강원대_BE 김동혁)의 request, response의 형태를 따르게 됨
- fe가 사용하던 명세와 be의 명세를 대조하는 엑셀 작성
- 버전을 명시하기 위해 스프레드시트가 아닌 엑셀 사용
- fe는 엑셀과 swagger를 참조해서(서버로 빌드해 놓음) 기능을 작성함
- be는 엑셀과 swagger를 참조해서(서버로 빌드해 놓음) 입출력 명세를 통일함
- 본인은 나머지 be의 의견 취합 및 반영, 엑셀 작성을 담당함
- 유저생성 url 변경
- /api/user/register
- 직관적 회원가입을 명시하기 위함
- 상품 주문시 상품id를 요구하지 않음
- 상품 옵션 id로 충분히 검증 가능
- 상품 주문시 주문내역 저장
- 주문 내역을 조회하기 위해서 변경함
- 주문 내역 호출 로그를 조회하는 방식으로 주문내역을 내보일 예정이었지만
- 명시적으로 서버에 저장하게 변경
- 상품 주문 리스트 조회
- /api/product/order/{optionId}
- 로그인한 유저의 상품 주문 내역을 반환한다.
- 유저id, 상품옵션id, 재고를 반환한다.
21 changes: 16 additions & 5 deletions src/main/java/gift/controller/ProductOrderController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package gift.controller;

import gift.domain.ProductOption.optionDetail;
import gift.domain.ProductOrder;
import gift.domain.ProductOrder.OrderDetail;
import gift.domain.ProductOrder.OrderSimple;
import gift.domain.ProductOrder.decreaseProductOption;
import gift.service.ProductOrderService;
import gift.util.page.PageMapper;
import gift.util.page.PageResult;
import gift.util.page.SingleResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -17,7 +22,7 @@

@Tag(name = "상품 주문 관련 서비스")
@RestController
@RequestMapping("/api/product/{productId}/order")
@RequestMapping("/api/product/order")
public class ProductOrderController {

private final ProductOrderService productOrderService;
Expand All @@ -29,11 +34,17 @@ public ProductOrderController(ProductOrderService productOrderService) {

@Operation(summary = "상품 주문", description = "어느 유저가 요구만큼 주문, 주문 성공시 카카오톡 메세지 송신")
@PostMapping("/{optionId}")
public SingleResult<optionDetail> decreaseProductOption(
public SingleResult<OrderDetail> decreaseProductOption(
HttpServletRequest req,
@PathVariable Long productId,
@PathVariable Long optionId, @Valid @RequestBody decreaseProductOption decrease) {
return new SingleResult<>(
productOrderService.decreaseProductOption(req, productId, optionId, decrease));
productOrderService.decreaseProductOption(req, optionId, decrease));
}

@Operation(summary = "상품 주문 리스트 조회", description = "본인 상품 주문 내역 리스트 반환")
@GetMapping()
public PageResult<OrderSimple> ProductOrderList(
HttpServletRequest req, @Valid @RequestBody ProductOrder.getList param) {
return PageMapper.toPageResult(productOrderService.getOrderList(req,param));
}
}
2 changes: 1 addition & 1 deletion src/main/java/gift/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public SingleResult<UserEntity> getUser(@PathVariable long id) {
}

@Operation(summary = "유저 생성")
@PostMapping
@PostMapping("/register")
public SingleResult<Long> createUser(@Valid @RequestBody User.CreateUser create) {
return new SingleResult<>(userService.createUser(create));
}
Expand Down
91 changes: 90 additions & 1 deletion src/main/java/gift/domain/ProductOrder.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package gift.domain;

import gift.util.page.PageParam;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import java.time.LocalDateTime;

public class ProductOrder {

public ProductOrder() {
public static class getList extends PageParam {
}

public static class decreaseProductOption {
Expand All @@ -25,4 +27,91 @@ public Long getQuantity() {
return quantity;
}
}

public static class OrderDetail {

private Long productId;
private String productName;
private Integer productPrice;
private Long optionId;
private String OptionName;
private Long quantity;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

public OrderDetail() {
}

public OrderDetail(Long productId, String productName, Integer productPrice,
Long optionId, String optionName, Long quantity, LocalDateTime createdAt,
LocalDateTime updatedAt) {
this.productId = productId;
this.productName = productName;
this.productPrice = productPrice;
this.optionId = optionId;
OptionName = optionName;
this.quantity = quantity;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public Long getProductId() {
return productId;
}

public String getProductName() {
return productName;
}

public Integer getProductPrice() {
return productPrice;
}

public Long getOptionId() {
return optionId;
}

public String getOptionName() {
return OptionName;
}

public Long getQuantity() {
return quantity;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}
}

public static class OrderSimple {
private Long userId;
private Long optionId;
private Long quantity;

public OrderSimple() {
}

public OrderSimple(Long userId, Long optionId, Long quantity) {
this.userId = userId;
this.optionId = optionId;
this.quantity = quantity;
}

public Long getUserId() {
return userId;
}

public Long getOptionId() {
return optionId;
}

public Long getQuantity() {
return quantity;
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/gift/entity/ProductOptionEntity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gift.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
Expand All @@ -9,7 +11,9 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -32,6 +36,11 @@ public class ProductOptionEntity {
@JoinColumn(name = "product_id")
private ProductEntity product;

@JsonIgnore
@OneToMany(mappedBy = "options", fetch = FetchType.LAZY,
cascade = CascadeType.ALL, orphanRemoval = true)
private List<ProductOrderEntity> orders;

@CreatedDate
private LocalDateTime createdAt;

Expand Down
66 changes: 66 additions & 0 deletions src/main/java/gift/entity/ProductOrderEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package gift.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalDateTime;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity(name = "ProductOrder")
@EntityListeners(AuditingEntityListener.class)
public class ProductOrderEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
private Long quantity;

@CreatedDate
private LocalDateTime createdAt;

@ManyToOne(targetEntity = UserEntity.class, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private UserEntity userEntity;

@ManyToOne(targetEntity = ProductOptionEntity.class, fetch = FetchType.LAZY)
@JoinColumn(name = "productOption_id")
private ProductOptionEntity options;

public ProductOrderEntity() {
}

public ProductOrderEntity(Long quantity, UserEntity userEntity,
ProductOptionEntity options) {
this.quantity = quantity;
this.userEntity = userEntity;
this.options = options;
}

public Long getId() {
return id;
}

public Long getQuantity() {
return quantity;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

public UserEntity getUserEntity() {
return userEntity;
}

public ProductOptionEntity getOptions() {
return options;
}
}
5 changes: 5 additions & 0 deletions src/main/java/gift/entity/UserEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public class UserEntity {
cascade = CascadeType.ALL, orphanRemoval = true)
private List<SocialEntity> socials;

@JsonIgnore
@OneToMany(mappedBy = "userEntity", fetch = FetchType.LAZY,
cascade = CascadeType.ALL, orphanRemoval = true)
private List<ProductOrderEntity> orders;

public UserEntity() {
}

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/gift/mapper/ProductOrderMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package gift.mapper;

import gift.domain.ProductOrder.OrderDetail;
import gift.domain.ProductOrder.OrderSimple;
import gift.entity.ProductOptionEntity;
import gift.entity.ProductOrderEntity;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Component;

@Component
public class ProductOrderMapper {
public Page<OrderSimple> toSimple(Page<ProductOrderEntity> all) {
List<OrderSimple> simpleList = all.stream()
.map(entity -> new OrderSimple(
entity.getUserEntity().getId(),
entity.getOptions().getId(),
entity.getQuantity()
))
.collect(Collectors.toList());
return new PageImpl<>(simpleList, all.getPageable(), all.getTotalElements());
}

public OrderDetail toDetail(ProductOptionEntity entity) {
return new OrderDetail(
entity.getProduct().getId(),
entity.getProduct().getName(),
entity.getProduct().getPrice(),
entity.getId(),
entity.getName(),
entity.getQuantity(),
entity.getCreatedAt(),
entity.getUpdatedAt()
);
}
}
13 changes: 13 additions & 0 deletions src/main/java/gift/repository/ProductOrderRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gift.repository;

import gift.entity.ProductOrderEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductOrderRepository extends JpaRepository<ProductOrderEntity,Long> {

Page<ProductOrderEntity> findAllByUserEntityId(long userId, Pageable pageable);
}
Loading