Skip to content

Commit

Permalink
Merge pull request #94 from ClothingStoreService/refactor/pagination-…
Browse files Browse the repository at this point in the history
…response

Refactor/pagination response
  • Loading branch information
Ogu1208 authored Jul 20, 2024
2 parents d59af18 + 515331f commit 338d0ec
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.store.clothstar.category.service.CategoryService;
import org.store.clothstar.common.dto.MessageDTO;
import org.store.clothstar.common.util.URIBuilder;
import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse;
import org.store.clothstar.productLine.dto.response.ProductLineDetailResponse;
import org.store.clothstar.productLine.service.ProductLineService;

import java.net.URI;
Expand Down Expand Up @@ -68,21 +68,21 @@ public ResponseEntity<MessageDTO> updateCategories(

@Operation(summary = "카테고리별 상품 조회 (Offset Paging)", description = "카테고리 ID로 해당 카테고리에 속하는 모든 상품을 Offset Paging을 통해 조회한다.")
@GetMapping("/{categoryId}/productLines/offset")
public ResponseEntity<Page<ProductLineWithProductsJPAResponse>> getProductLinesByCategory(
public ResponseEntity<Page<ProductLineDetailResponse>> getProductLinesByCategory(
@PathVariable Long categoryId,
@PageableDefault(size = 18) Pageable pageable,
@RequestParam(required = false) String keyword) {
Page<ProductLineWithProductsJPAResponse> productLineResponses = productLineService.getProductLinesByCategoryWithOffsetPaging(categoryId, pageable, keyword);
Page<ProductLineDetailResponse> productLineResponses = productLineService.getProductLinesByCategoryWithOffsetPaging(categoryId, pageable, keyword);
return ResponseEntity.ok().body(productLineResponses);
}

@Operation(summary = "카테고리별 상품 조회 (Slice Paging)", description = "카테고리 ID로 해당 카테고리에 속하는 모든 상품을 Slice Paging을 통해 조회한다.")
@GetMapping("/{categoryId}/productLines/slice")
public ResponseEntity<Slice<ProductLineWithProductsJPAResponse>> getProductLinesByCategorySlice(
public ResponseEntity<Slice<ProductLineDetailResponse>> getProductLinesByCategorySlice(
@PathVariable Long categoryId,
@PageableDefault(size = 18) Pageable pageable,
@RequestParam(required = false) String keyword) {
Slice<ProductLineWithProductsJPAResponse> productLineResponses = productLineService.getProductLinesByCategoryWithSlicePaging(categoryId, pageable, keyword);
Slice<ProductLineDetailResponse> productLineResponses = productLineService.getProductLinesByCategoryWithSlicePaging(categoryId, pageable, keyword);
return ResponseEntity.ok().body(productLineResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,24 @@
import org.store.clothstar.product.dto.request.UpdateProductRequest;
import org.store.clothstar.productLine.entity.ProductLineEntity;

@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@BatchSize(size = 100)
@Table(name = "product")
//@BatchSize(size = 100)
@Entity(name = "product")
public class ProductEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long productId;
private String name;
private int extraCharge;
private Long stock;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_line_id", nullable = false)
// @JsonManagedReference
private ProductLineEntity productLine;

private String name;

private int extraCharge;

private Long stock;

public void updateOption(UpdateProductRequest updateProductRequest) {
this.name = updateProductRequest.getName();
this.extraCharge = updateProductRequest.getExtraCharge();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.store.clothstar.productLine.dto.request.CreateProductLineRequest;
import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest;
import org.store.clothstar.productLine.dto.response.ProductLineResponse;
import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse;
import org.store.clothstar.productLine.dto.response.ProductLineDetailResponse;
import org.store.clothstar.productLine.service.ProductLineService;

import java.net.URI;
Expand All @@ -30,6 +30,7 @@ public class ProductLineController {

private final ProductLineService productLineService;

@Deprecated
@Operation(summary = "전체 상품 조회", description = "삭제되지 않은 모든 상품을 조회한다.")
@GetMapping("/v1/productLines")
public ResponseEntity<List<ProductLineResponse>> getAllProductLines() {
Expand All @@ -39,26 +40,26 @@ public ResponseEntity<List<ProductLineResponse>> getAllProductLines() {

@Operation(summary = "전체 상품 Offset Paging 조회", description = "삭제되지 않은 모든 상품을 조회한다.")
@GetMapping("/v1/productLines/offset")
public ResponseEntity<Page<ProductLineWithProductsJPAResponse>> getAllProductLinesOffsetPaging(
public ResponseEntity<Page<ProductLineDetailResponse>> getAllProductLinesOffsetPaging(
@PageableDefault(size = 18) Pageable pageable,
@RequestParam(required = false) String keyword){
Page<ProductLineWithProductsJPAResponse> productLineResponses = productLineService.getAllProductLinesWithProductsOffsetPaging(pageable, keyword);
Page<ProductLineDetailResponse> productLineResponses = productLineService.getAllProductLinesWithProductsOffsetPaging(pageable, keyword);
return ResponseEntity.ok().body(productLineResponses);
}

@Operation(summary = "전체 상품 Slice Paging 조회", description = "삭제되지 않은 모든 상품을 조회한다.")
@GetMapping("/v1/productLines/slice")
public ResponseEntity<Slice<ProductLineWithProductsJPAResponse>> getAllProductLinesSlicePaging(
public ResponseEntity<Slice<ProductLineDetailResponse>> getAllProductLinesSlicePaging(
@PageableDefault(size = 18) Pageable pageable,
@RequestParam(required = false) String keyword) {
Slice<ProductLineWithProductsJPAResponse> productLineResponses = productLineService.getAllProductLinesWithProductsSlicePaging(pageable, keyword);
Slice<ProductLineDetailResponse> productLineResponses = productLineService.getAllProductLinesWithProductsSlicePaging(pageable, keyword);
return ResponseEntity.ok().body(productLineResponses);
}

@Operation(summary = "상품 상세 조회", description = "productLineId로 상품과 하위 옵션들을 상세 조회한다.")
@GetMapping("/v1/productLines/{productLineId}")
public ResponseEntity<ProductLineWithProductsJPAResponse> getProductLine(@PathVariable("productLineId") Long productLineId) {
ProductLineWithProductsJPAResponse productLineWithProducts = productLineService.getProductLineWithProducts(productLineId);
public ResponseEntity<ProductLineDetailResponse> getProductLine(@PathVariable("productLineId") Long productLineId) {
ProductLineDetailResponse productLineWithProducts = productLineService.getProductLineWithProducts(productLineId);
return ResponseEntity.ok().body(productLineWithProducts);
}

Expand All @@ -82,6 +83,7 @@ public ResponseEntity<MessageDTO> updateProductLine(
return ResponseEntity.ok().body(new MessageDTO(HttpStatus.OK.value(), "ProductLine updated successfully"));
}

@Operation(summary = "상품 삭제", description = "productLineId를 통해 deletedAt 컬럼을 설정해 soft delete를 수행한다.")
@DeleteMapping("/v1/productLines/{productLineId}")
public ResponseEntity<Void> deleteProductLine(@PathVariable("productLineId") Long productLineId) {
productLineService.setDeletedAt(productLineId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,74 @@
package org.store.clothstar.productLine.dto.response;

import lombok.Builder;
import lombok.Getter;
import org.store.clothstar.productLine.domain.ProductLine;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.querydsl.core.annotations.QueryProjection;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.store.clothstar.member.dto.response.SellerSimpleResponse;
import org.store.clothstar.product.dto.response.ProductResponse;
import org.store.clothstar.product.entity.ProductEntity;
import org.store.clothstar.productLine.domain.type.ProductLineStatus;
import org.store.clothstar.productLine.entity.ProductLineEntity;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Builder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ProductLineDetailResponse {
private Long productId;

@Schema(description = "상품 id", example = "1")
private Long productLineId;

@Schema(description = "상품 이름", example = "우유 모자")
private String name;
private String brandName;

@Schema(description = "상품 설명", example = "우유 모자입니다.")
private String content;

@Schema(description = "상품 가격", example = "10000")
private int price;

@Schema(description = "상품 전체 재고", example = "100")
private Long totalStock;
private Long saleCount;
private ProductLineStatus productLineStatus;
private String biz_no;

@Schema(description = "상품 상태", example = "FOR_SALE")
private ProductLineStatus status;

@Schema(description = "상품 판매량", example = "10")
private Long saleCount; // ~개 판매중

@Schema(description = "상품 옵션")
private List<ProductResponse> productList;

@Schema(description = "판매자 정보")
private SellerSimpleResponse seller;

@Schema(description = "생성일시")
private LocalDateTime createdAt;

@Schema(description = "수정일시")
private LocalDateTime modifiedAt;
private LocalDateTime deletedAt;

public static ProductLineDetailResponse from(ProductLine productLine) {
return ProductLineDetailResponse.builder()
.productId(productLine.getProductLineId())
.name(productLine.getName())
.content(productLine.getContent())
.brandName(productLine.getBrandName())
.price(productLine.getPrice())
.totalStock(productLine.getTotalStock())
.saleCount(productLine.getSaleCount())
.productLineStatus(productLine.getStatus())
.biz_no(productLine.getBiz_no())
.createdAt(productLine.getCreatedAt())
.modifiedAt(productLine.getModifiedAt())
.deletedAt(productLine.getDeletedAt())
.build();

public ProductLineDetailResponse(ProductLineEntity productLine) {
this.productLineId = productLine.getProductLineId();
this.name = productLine.getName();
this.content = productLine.getContent();
this.price = productLine.getPrice();
this.totalStock = productLine.getProducts().stream().mapToLong(ProductEntity::getStock).sum();
this.status = productLine.getStatus();
this.saleCount = productLine.getSaleCount();
this.createdAt = productLine.getCreatedAt();
this.modifiedAt = productLine.getModifiedAt();
this.seller = SellerSimpleResponse.from(productLine.getSeller());
this.productList = productLine.getProducts().stream()
.map(ProductResponse::from)
.collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ProductLineWithProductsJPAResponse {
public class ProductLinePaginationResponse {

@Schema(description = "상품 id", example = "1")
private Long productLineId;
Expand All @@ -41,7 +41,7 @@ public class ProductLineWithProductsJPAResponse {
private ProductLineStatus status;

@Schema(description = "상품 옵션")
private List<ProductResponse> productList = new ArrayList<>();
private List<ProductResponse> productList;

@Schema(description = "상품 판매량", example = "10")
private Long saleCount; // ~개 판매중
Expand All @@ -56,8 +56,7 @@ public class ProductLineWithProductsJPAResponse {
@Schema(description = "수정일시")
private LocalDateTime modifiedAt;

@QueryProjection
public ProductLineWithProductsJPAResponse(ProductLineEntity productLine, Seller seller, Long totalStock) {
public ProductLinePaginationResponse(ProductLineEntity productLine, Seller seller, Long totalStock) {
this.productLineId = productLine.getProductLineId();
this.name = productLine.getName();
this.content = productLine.getContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,33 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
@BatchSize(size = 100)
@Entity(name = "product_line")
//@Table(name = "product_line")
public class ProductLineEntity extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long productLineId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Seller seller;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private CategoryEntity category;

private String name;

private String content;

private int price;

// private Long totalStock;
private Long saleCount;

@Enumerated(EnumType.STRING)
private ProductLineStatus status;

private Long saleCount;

@OneToMany(mappedBy = "productLine", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
// @JsonBackReference
@JsonIgnore
private List<ProductEntity> products;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private CategoryEntity category;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Seller seller;

public void updateProductLine(UpdateProductLineRequest updateProductLineRequest) {
this.name = updateProductLineRequest.getName();
this.content = updateProductLineRequest.getContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Repository;
import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse;
import org.store.clothstar.productLine.entity.ProductLineEntity;

import java.util.Optional;

@Repository
public interface ProductLineRepositoryCustom {
Page<ProductLineWithProductsJPAResponse> getProductLinesWithOptions(Pageable pageable);

Optional<ProductLineWithProductsJPAResponse> findProductLineWithOptionsById(Long productLineId);

Page<ProductLineWithProductsJPAResponse> findAllOffsetPaging(Pageable pageable, String keyword);
Page<ProductLineEntity> findAllOffsetPaging(Pageable pageable, String keyword);

Slice<ProductLineWithProductsJPAResponse> findAllSlicePaging(Pageable pageable, String keyword);
Slice<ProductLineEntity> findAllSlicePaging(Pageable pageable, String keyword);

Page<ProductLineEntity> findEntitiesByCategoryWithOffsetPaging(Long categoryId, Pageable pageable, String keyword);

Expand Down
Loading

0 comments on commit 338d0ec

Please sign in to comment.