Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Yeji committed Oct 29, 2024
2 parents 0781e55 + dab38ff commit 3cbe742
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.samtuap.inong.domain.discount.dto.DiscountCreateRequest;
import org.samtuap.inong.domain.discount.dto.DiscountDetailResponse;
import org.samtuap.inong.domain.discount.dto.DiscountResponse;
import org.samtuap.inong.domain.discount.dto.DiscountUpdateRequest;
import org.samtuap.inong.domain.discount.service.DiscountService;
Expand Down Expand Up @@ -54,7 +55,7 @@ public ResponseEntity<Page<DiscountResponse>> getDiscountList(

// 할인 λ””ν…ŒμΌ 쑰회
@GetMapping("/{discountId}/detail")
public ResponseEntity<DiscountResponse> getDiscountDetail(@PathVariable("discountId") Long discountId) {
public ResponseEntity<DiscountDetailResponse> getDiscountDetail(@PathVariable("discountId") Long discountId) {
return new ResponseEntity<>(discountService.getDiscountDetail(discountId), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ public void applyDiscounts() {

Objects.requireNonNull(cacheManager.getCache("PackageDetail")).clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.samtuap.inong.domain.discount.dto;

import lombok.Builder;
import org.samtuap.inong.domain.discount.entity.Discount;

import java.time.LocalDate;
import java.util.List;

@Builder
public record DiscountDetailResponse(
Long id,
Integer discount,
LocalDate startAt,
LocalDate endAt,
boolean discountActive,
List<Long>productIdList
) {
public static DiscountDetailResponse fromEntity(Discount discount, List<Long> productIdList) {
return DiscountDetailResponse.builder()
.id(discount.getId())
.discount(discount.getDiscount())
.startAt(discount.getStartAt())
.endAt(discount.getEndAt())
.discountActive(discount.isDiscountActive())
.productIdList(productIdList)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import org.samtuap.inong.domain.discount.entity.Discount;

import java.time.LocalDate;
import java.util.List;

@Builder
public record DiscountUpdateRequest(
Integer discount,
LocalDate startAt,
LocalDate endAt,
boolean discountActive
boolean discountActive,
List<Long> productIdList
) {
public void updateEntity(Discount discount) {
discount.updateDiscount(this.discount, this.startAt, this.endAt, this.discountActive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.hibernate.annotations.SQLRestriction;
import org.samtuap.inong.domain.common.BaseEntity;
import java.time.LocalDate;
import java.util.List;

@Getter
@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public interface DiscountRepository extends JpaRepository<Discount, Long> {

List<Discount> findAllByEndAtBefore(LocalDate now);

List<Discount> findAllByStartAtLessThanEqualAndDiscountActiveFalse(LocalDate now);

List<Discount> findAllByEndAtLessThanEqualAndDiscountActiveTrue(LocalDate now);

List<Discount> findAll();

default Discount findByIdThrow(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.extern.slf4j.Slf4j;
import org.samtuap.inong.common.exception.BaseCustomException;
import org.samtuap.inong.domain.discount.dto.DiscountCreateRequest;
import org.samtuap.inong.domain.discount.dto.DiscountDetailResponse;
import org.samtuap.inong.domain.discount.dto.DiscountResponse;
import org.samtuap.inong.domain.discount.dto.DiscountUpdateRequest;
import org.samtuap.inong.domain.discount.entity.Discount;
Expand All @@ -18,6 +19,8 @@

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

import static org.samtuap.inong.common.exceptionType.ProductExceptionType.*;

@Slf4j
Expand Down Expand Up @@ -69,13 +72,47 @@ public void updateDiscount(Long discountId, DiscountUpdateRequest request) {
log.info(">>>>λ‚ μ§œ : " + existingDiscount.getStartAt() + " >>>> μ˜€λŠ˜λ³΄λ‹€ 이후 => λΉ„ν™œμ„±ν™”");
existingDiscount.updateDiscountActive(false);
}
// λ³€κ²½λœ 할인이 적용된 μƒν’ˆμ˜ μΊμ‹œ μ‚­μ œ 처리
List<PackageProduct> discountProducts = packageProductRepository.findAllByDiscountId(discountId);
if(!discountProducts.isEmpty()){
for (PackageProduct product : discountProducts) {
cacheManager.getCache("PackageDetail").evict(product.getId());

// 기쑴에 할인에 적용된 μƒν’ˆλ“€μ˜ ID 리슀트
List<PackageProduct> existingProducts = packageProductRepository.findAllByDiscountId(discountId);
List<Long> existingProductIds = existingProducts.stream()
.map(PackageProduct::getId)
.collect(Collectors.toList());

// μš”μ²­μœΌλ‘œ 받은 μƒˆλ‘œμš΄ μƒν’ˆ ID 리슀트
List<Long> newProductIds = request.productIdList();

// ν• μΈμ—μ„œ μ œκ±°ν•  μƒν’ˆλ“€ (기쑴에 μžˆμ—ˆμ§€λ§Œ μƒˆ λ¦¬μŠ€νŠΈμ—λŠ” μ—†λŠ” μƒν’ˆλ“€)
List<Long> productsToRemoveDiscount = existingProductIds.stream()
.filter(id -> !newProductIds.contains(id))
.collect(Collectors.toList());
for (Long productId : productsToRemoveDiscount) {
PackageProduct product = packageProductRepository.findByIdOrThrow(productId);
product.updateDiscountId(null);
cacheManager.getCache("PackageDetail").evict(productId);
}

// 할인에 μƒˆλ‘œ μΆ”κ°€ν•  μƒν’ˆλ“€ (μƒˆ λ¦¬μŠ€νŠΈμ—λŠ” μžˆμ§€λ§Œ 기쑴에 μ—†μ—ˆλ˜ μƒν’ˆλ“€)
List<Long> productsToAddDiscount = newProductIds.stream()
.filter(id -> !existingProductIds.contains(id))
.collect(Collectors.toList());
for (Long productId : productsToAddDiscount) {
PackageProduct product = packageProductRepository.findByIdOrThrow(productId);
if (product.getDiscountId() == null) {
product.updateDiscountId(discountId);
} else {
throw new BaseCustomException(DISCOUNT_ALREADY_EXISTS);
}
cacheManager.getCache("PackageDetail").evict(productId);
}

// // λ³€κ²½λœ 할인이 적용된 μƒν’ˆμ˜ μΊμ‹œ μ‚­μ œ 처리
// List<PackageProduct> discountProducts = packageProductRepository.findAllByDiscountId(discountId);
// if(!discountProducts.isEmpty()){
// for (PackageProduct product : discountProducts) {
// cacheManager.getCache("PackageDetail").evict(product.getId());
// }
// }
discountRepository.save(existingDiscount);
}

Expand All @@ -101,9 +138,16 @@ public Page<DiscountResponse> getDiscountList(Pageable pageable) {
}

// 할인 λ””ν…ŒμΌ 쑰회
public DiscountResponse getDiscountDetail(Long discountId) {
public DiscountDetailResponse getDiscountDetail(Long discountId) {
Discount discount = discountRepository.findById(discountId)
.orElseThrow(() -> new BaseCustomException(DISCOUNT_NOT_FOUND));
return DiscountResponse.fromEntity(discount);

// ν•΄λ‹Ή 할인에 적용된 μƒν’ˆλ“€μ˜ ID 리슀트 쑰회
List<PackageProduct> productList = packageProductRepository.findAllByDiscountId(discountId);
List<Long> productIdList = productList.stream()
.map(PackageProduct::getId)
.collect(Collectors.toList());

return DiscountDetailResponse.fromEntity(discount, productIdList);
}
}

0 comments on commit 3cbe742

Please sign in to comment.