Skip to content

Commit

Permalink
fix: getClothingSales api (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunihs authored Aug 2, 2024
1 parent 9af19fd commit 7405658
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.example.repick.domain.clothingSales.dto;

import com.example.repick.domain.clothingSales.entity.BagCollect;
import com.example.repick.domain.clothingSales.entity.BoxCollect;
import com.example.repick.domain.clothingSales.entity.ClothingSales;
import com.example.repick.domain.clothingSales.entity.ClothingSalesStateType;
import com.example.repick.domain.product.entity.Product;
import com.example.repick.domain.product.entity.ProductStateType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;

import java.time.format.DateTimeFormatter;
import java.util.List;

@Builder
public record GetClothingSales(
Expand All @@ -32,7 +29,7 @@ public record GetClothingSales(
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yy");

// 수거 요청일 경우
public static GetClothingSales ofClothingSales(ClothingSales clothingSales, List<Product> products) {
public static GetClothingSales of(ClothingSales clothingSales) {
ClothingSalesStateType clothingSalesState = clothingSales.getClothingSalesState();
boolean isBoxCollect = clothingSales instanceof BoxCollect;
boolean isProducted = ClothingSalesStateType.AFTER_PRODUCTION.contains(clothingSalesState);
Expand All @@ -45,23 +42,14 @@ public static GetClothingSales ofClothingSales(ClothingSales clothingSales, List
.isBoxCollect(isBoxCollect)
.status(clothingSalesState.getAdminValue())
.requestDate(clothingSales.getCreatedDate().format(formatter))
.isForCollect(clothingSalesState != ClothingSalesStateType.REQUEST_CANCELLED)
.isForCollect(!ClothingSalesStateType.BEFORE_COLLECTION.contains(clothingSalesState))
.productStartDate(isSelling? clothingSales.getProductList().get(0).getSalesStartDate().format(formatter) : null)
.salesPeriod(isSelling? clothingSales.getProductList().get(0).getSalesStartDate().format(formatter)
+ " ~ " + clothingSales.getProductList().get(0).getSalesStartDate().plusDays(90).format(formatter) : null)
.settlementRequestDate(clothingSales.getUser().getSettlementRequestDate() != null ? clothingSales.getUser().getSettlementRequestDate().format(formatter) : null)
.settlementCompleteDate(clothingSales.getUser().getSettlementCompleteDate() != null ? clothingSales.getUser().getSettlementCompleteDate().format(formatter) : null)
.isRejected(isProducted? products.stream().anyMatch(product -> product.getProductState() == ProductStateType.REJECTED) : null)
.isRejected(isProducted? clothingSales.getProductList().stream().anyMatch(product -> product.getProductState() == ProductStateType.REJECTED) : null)
.isExpiredAndReturned(clothingSalesState == ClothingSalesStateType.SELLING_EXPIRED)
.build();
}

public static GetClothingSales ofBagCollect(BagCollect bagCollect) {
return GetClothingSales.builder()
.id(bagCollect.getId())
.name(bagCollect.getUser().getNickname())
.status(bagCollect.getClothingSalesState().getAdminValue())
.requestDate(bagCollect.getCreatedDate().format(formatter))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public abstract class ClothingSales {
@Column(name = "collection_date")
private LocalDate collectionDate;

@Enumerated(EnumType.STRING)
private ClothingSalesStateType clothingSalesState;

@OneToMany(mappedBy = "clothingSales")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum ClothingSalesStateType {
private final String sellerValue;
private final String adminValue;

public static final List<ClothingSalesStateType> BEFORE_COLLECTION = Arrays.asList(BAG_INIT_REQUEST, REQUEST_CANCELLED, BAG_DELIVERY, BAG_DELIVERED);
public static final List<ClothingSalesStateType> AFTER_PRODUCTION = Arrays.asList(PRODUCT_REGISTERED, SELLING, SELLING_EXPIRED);
public static final List<ClothingSalesStateType> AFTER_SELLING = Arrays.asList(SELLING, SELLING_EXPIRED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.example.repick.domain.clothingSales.entity.ClothingSales;
import com.example.repick.domain.clothingSales.entity.ClothingSalesStateType;
import com.example.repick.domain.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface ClothingSalesRepository extends JpaRepository<ClothingSales, Long> {
Page<ClothingSales> findAllByOrderByCreatedDateDesc(Pageable pageable);
List<ClothingSales> findByUserAndClothingSalesState(User user, ClothingSalesStateType clothingSalesStateType);
List<ClothingSales> findByUserOrderByCreatedDateDesc(User user);
Optional<ClothingSales> findByUserAndClothingSalesCount(User user, Integer clothingSalesCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ public BagCollectResponse registerBagCollect(PostBagCollect postBagCollect) {
// BagCollect
bagCollect.updateBagCollectInfo(postBagCollect.bagQuantity(), postBagCollect.postalCode(), postBagCollect.mainAddress(), postBagCollect.detailAddress(), postBagCollect.collectionDate());
bagCollect.updateImageUrl(s3UploadService.saveFile(postBagCollect.image(), "clothingSales/bagCollect/" + user.getId() + "/" + bagCollect.getId()));
bagCollectRepository.save(bagCollect);

// BagCollectState
ClothingSalesState clothingSalesState = ClothingSalesState.of(bagCollect.getId(), ClothingSalesStateType.BAG_COLLECT_REQUEST);
bagCollect.updateClothingSalesState(ClothingSalesStateType.BAG_COLLECT_REQUEST);
bagCollectRepository.save(bagCollect);
clothingSalesStateRepository.save(clothingSalesState);

return BagCollectResponse.of(bagCollect, clothingSalesState.getClothingSalesStateType().getSellerValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import com.example.repick.global.page.PageResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -47,7 +45,6 @@ public class ClothingSalesService {
private final ClothingSalesRepository clothingSalesRepository;
private final ClothingSalesStateRepository clothingSalesStateRepository;
private final ProductOrderRepository productOrderRepository;
private final BagCollectRepository bagCollectRepository;

public void updateSellingExpired(Product product) {
ClothingSalesState clothingSalesState = ClothingSalesState.of(product.getClothingSales().getId(), ClothingSalesStateType.SELLING_EXPIRED);
Expand Down Expand Up @@ -177,30 +174,10 @@ private ProductQuantityCounter countProductQuantity(List<GetProductByClothingSal
// Admin API
public PageResponse<List<GetClothingSales>> getClothingSalesInformation(PageCondition pageCondition){
// 수거 신청 정보 조회
List<GetClothingSales> clothingSalesList = new ArrayList<>(clothingSalesRepository.findAll().stream()
.map(clothingSales -> {
List<Product> products = clothingSales.getProductList();
return GetClothingSales.ofClothingSales(clothingSales, products);
})
.toList());

// 리픽백 배송 요청 정보 조회
List<GetClothingSales> bagCollectList = bagCollectRepository.findAll().stream()
.map(GetClothingSales::ofBagCollect)
.toList();

clothingSalesList.addAll(bagCollectList);

// createdAt 순서로 내림차순 정렬
clothingSalesList.sort((o1, o2) -> o2.requestDate().compareTo(o1.requestDate()));

// Paging 처리
Pageable pageable = pageCondition.toPageable();
int start = (int) pageable.getOffset();
int end = Math.min((start + pageable.getPageSize()), clothingSalesList.size());
List<GetClothingSales> pagedList = clothingSalesList.subList(start, end);
PageImpl<GetClothingSales> page = new PageImpl<>(pagedList, pageable, clothingSalesList.size());
return new PageResponse<>(page.getContent(), page.getTotalPages(), page.getTotalElements());
Page<ClothingSales> clothingSalesPage = clothingSalesRepository.findAllByOrderByCreatedDateDesc(pageCondition.toPageable());
List<GetClothingSales> clothingSalesList = clothingSalesPage.stream()
.map(GetClothingSales::of).toList();
return PageResponse.of(clothingSalesList, clothingSalesPage.getTotalPages(), clothingSalesPage.getTotalElements());
}


Expand Down

0 comments on commit 7405658

Please sign in to comment.