Skip to content

Commit

Permalink
[#223] HOTFIX: 캐러셀 삭제 시 promotionId로 판단하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hoonyworld committed Oct 3, 2024
1 parent 2dbb3a4 commit 46d5a3f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 90 deletions.
55 changes: 25 additions & 30 deletions src/main/java/com/beat/admin/application/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.beat.admin.port.in.AdminUseCase;
import com.beat.domain.performance.domain.Performance;
import com.beat.domain.performance.port.in.PerformanceUseCase;
import com.beat.domain.promotion.domain.CarouselNumber;
import com.beat.domain.promotion.domain.Promotion;
import com.beat.domain.promotion.port.in.PromotionUseCase;

Expand Down Expand Up @@ -35,12 +34,10 @@ public List<Promotion> findAllPromotionsSortedByCarouselNumber() {

@Override
@Transactional
public List<Promotion> processPromotionsAndSortByCarouselNumber(List<PromotionModifyRequest> modifyRequests,
List<PromotionGenerateRequest> generateRequests, List<CarouselNumber> deleteCarouselNumbers,
List<CarouselNumber> overlappingCarouselNumbers) {
public List<Promotion> processPromotionsAndSortByPromotionId(List<PromotionModifyRequest> modifyRequests,
List<PromotionGenerateRequest> generateRequests, List<Long> deletePromotionIds) {

handleOverlappingCarouselNumbersPromotionDeletion(overlappingCarouselNumbers);
handlePromotionDeletion(deleteCarouselNumbers);
handlePromotionDeletion(deletePromotionIds);
List<Promotion> modifiedPromotions = handlePromotionModification(modifyRequests);
List<Promotion> addedPromotions = handlePromotionGeneration(generateRequests);

Expand All @@ -50,40 +47,38 @@ public List<Promotion> processPromotionsAndSortByCarouselNumber(List<PromotionMo
return sortPromotionsByCarouselNumber(applyPromotionChanges);
}

private void handleOverlappingCarouselNumbersPromotionDeletion(List<CarouselNumber> overlappingCarouselNumbers) {
if (!overlappingCarouselNumbers.isEmpty()) {
promotionUseCase.deleteByCarouselNumber(overlappingCarouselNumbers);
}
}

private void handlePromotionDeletion(List<CarouselNumber> deleteCarouselNumbers) {
if (!deleteCarouselNumbers.isEmpty()) {
promotionUseCase.deleteByCarouselNumber(deleteCarouselNumbers);
private void handlePromotionDeletion(List<Long> deletePromotionIds) {
if (!deletePromotionIds.isEmpty()) {
promotionUseCase.deletePromotionsByPromotionIds(deletePromotionIds);
}
}

private List<Promotion> handlePromotionModification(List<PromotionModifyRequest> modifyRequests) {
return modifyRequests.stream().map(modifyRequest -> {
return modifyRequests.stream()
.map(modifyRequest -> {

Promotion promotion = promotionUseCase.findById(modifyRequest.promotionId());
Promotion promotion = promotionUseCase.findById(modifyRequest.promotionId());

Performance performance = Optional.ofNullable(modifyRequest.performanceId())
.map(performanceUseCase::findById)
.orElse(null);
Performance performance = Optional.ofNullable(modifyRequest.performanceId())
.map(performanceUseCase::findById)
.orElse(null);

return promotionUseCase.modifyPromotion(promotion, performance, modifyRequest);
}).toList();
return promotionUseCase.modifyPromotion(promotion, performance, modifyRequest);
})
.toList();
}

private List<Promotion> handlePromotionGeneration(List<PromotionGenerateRequest> generateRequests) {
return generateRequests.stream().map(generateRequest -> {
Performance performance = Optional.ofNullable(generateRequest.performanceId())
.map(performanceUseCase::findById)
.orElse(null);

return promotionUseCase.createPromotion(generateRequest.newImageUrl(), performance,
generateRequest.redirectUrl(), generateRequest.isExternal(), generateRequest.carouselNumber());
}).toList();
return generateRequests.stream()
.map(generateRequest -> {
Performance performance = Optional.ofNullable(generateRequest.performanceId())
.map(performanceUseCase::findById)
.orElse(null);

return promotionUseCase.createPromotion(generateRequest.newImageUrl(), performance,
generateRequest.redirectUrl(), generateRequest.isExternal(), generateRequest.carouselNumber());
})
.toList();
}

private List<Promotion> sortPromotionsByCarouselNumber(List<Promotion> promotions) {
Expand Down
48 changes: 16 additions & 32 deletions src/main/java/com/beat/admin/facade/AdminFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.beat.admin.application.dto.response.CarouselHandleAllResponse;
import com.beat.admin.port.in.AdminUseCase;
import com.beat.domain.member.port.in.MemberUseCase;
import com.beat.domain.promotion.domain.CarouselNumber;
import com.beat.domain.promotion.domain.Promotion;
import com.beat.domain.promotion.port.in.PromotionUseCase;
import com.beat.domain.user.domain.Users;
Expand All @@ -28,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
Expand Down Expand Up @@ -71,57 +71,41 @@ public CarouselHandleAllResponse checkMemberAndProcessAllPromotionsSortedByCarou

List<PromotionModifyRequest> modifyRequests = new ArrayList<>();
List<PromotionGenerateRequest> generateRequests = new ArrayList<>();
Set<CarouselNumber> requestCarouselNumbers = new HashSet<>();
Set<Long> requestPromotionIds = new HashSet<>();

categorizePromotionRequests(request, modifyRequests, generateRequests, requestCarouselNumbers);
categorizePromotionRequestsByPromotionId(request, modifyRequests, generateRequests, requestPromotionIds);

List<CarouselNumber> allExistingCarouselNumbers = promotionUseCase.findAllCarouselNumbers();
List<Promotion> allExistingPromotions = promotionUseCase.findAllPromotions();

List<CarouselNumber> deleteCarouselNumbers = findDeleteCarouselNumbers(requestCarouselNumbers,
allExistingCarouselNumbers);
List<CarouselNumber> overlappingCarouselNumbers = findOverlappingCarouselNumbers(requestCarouselNumbers,
allExistingCarouselNumbers, request);
List<Long> deletePromotionIds = extractDeletePromotionIds(allExistingPromotions, requestPromotionIds);

List<Promotion> sortedPromotions = adminUsecase.processPromotionsAndSortByCarouselNumber(modifyRequests,
generateRequests, deleteCarouselNumbers, overlappingCarouselNumbers);
List<Promotion> sortedPromotions = adminUsecase.processPromotionsAndSortByPromotionId(modifyRequests,
generateRequests, deletePromotionIds);

return CarouselHandleAllResponse.from(sortedPromotions);
}

private void categorizePromotionRequests(CarouselHandleRequest request,
private void categorizePromotionRequestsByPromotionId(CarouselHandleRequest request,
List<PromotionModifyRequest> modifyRequests, List<PromotionGenerateRequest> generateRequests,
Set<CarouselNumber> requestCarouselNumbers) {
Set<Long> requestPromotionIds) {

for (PromotionHandleRequest promotionRequest : request.carousels()) {
requestCarouselNumbers.add(promotionRequest.carouselNumber());

if (promotionRequest instanceof PromotionModifyRequest modifyRequest) {
modifyRequests.add(modifyRequest);
requestPromotionIds.add(modifyRequest.promotionId());
} else if (promotionRequest instanceof PromotionGenerateRequest generateRequest) {
generateRequests.add(generateRequest);
}
}
}

private List<CarouselNumber> findDeleteCarouselNumbers(Set<CarouselNumber> requestCarouselNumbers,
List<CarouselNumber> allExistingCarouselNumbers) {
return allExistingCarouselNumbers.stream()
.filter(existingCarouselNumber -> !requestCarouselNumbers.contains(existingCarouselNumber))
.toList();
}
private List<Long> extractDeletePromotionIds(List<Promotion> allExistingPromotions, Set<Long> requestPromotionIds) {
Set<Long> allExistingPromotionIds = allExistingPromotions.stream()
.map(Promotion::getId)
.collect(Collectors.toSet());

private List<CarouselNumber> findOverlappingCarouselNumbers(Set<CarouselNumber> requestCarouselNumbers,
List<CarouselNumber> allExistingCarouselNumbers, CarouselHandleRequest request) {
return allExistingCarouselNumbers.stream()
.filter(requestCarouselNumbers::contains)
.filter(existingCarouselNumber -> {
Promotion existingPromotion = promotionUseCase.findPromotionByCarouselNumber(existingCarouselNumber);
return request.carousels()
.stream()
.filter(req -> req instanceof CarouselHandleRequest.PromotionModifyRequest)
.map(req -> (CarouselHandleRequest.PromotionModifyRequest)req)
.noneMatch(req -> req.promotionId() != null && req.promotionId().equals(existingPromotion.getId()));
})
return allExistingPromotionIds.stream()
.filter(existingId -> !requestPromotionIds.contains(existingId))
.toList();
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/beat/admin/port/in/AdminUseCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import com.beat.admin.application.dto.request.CarouselHandleRequest.PromotionGenerateRequest;
import com.beat.admin.application.dto.request.CarouselHandleRequest.PromotionModifyRequest;
import com.beat.domain.promotion.domain.CarouselNumber;
import com.beat.domain.promotion.domain.Promotion;

import java.util.List;

public interface AdminUseCase {
List<Promotion> findAllPromotionsSortedByCarouselNumber();

List<Promotion> processPromotionsAndSortByCarouselNumber(List<PromotionModifyRequest> modifyRequests,
List<PromotionGenerateRequest> generateRequests, List<CarouselNumber> deleteCarouselNumbers, List<CarouselNumber> overlappingCarouselNumbers);
List<Promotion> processPromotionsAndSortByPromotionId(List<PromotionModifyRequest> modifyRequests,
List<PromotionGenerateRequest> generateRequests, List<Long> deletePromotionIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,7 @@ public Promotion modifyPromotion(Promotion promotion, Performance performance, P
}

@Override
public void deleteByCarouselNumber(List<CarouselNumber> carouselNumber) {
promotionRepository.deleteByCarouselNumbers(carouselNumber);
}

@Override
@Transactional(readOnly = true)
public List<CarouselNumber> findAllCarouselNumbers() {
return promotionRepository.findAllCarouselNumbers();
}

@Override
@Transactional(readOnly = true)
public Promotion findPromotionByCarouselNumber(CarouselNumber carouselNumber) {
return promotionRepository.findByCarouselNumber(carouselNumber)
.orElseThrow(() -> new NotFoundException(PromotionErrorCode.PROMOTION_NOT_FOUND));
public void deletePromotionsByPromotionIds(List<Long> promotionIds) {
promotionRepository.deleteByPromotionIds(promotionIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
public interface PromotionRepository extends JpaRepository<Promotion, Long> {
List<Promotion> findAll();

@Query("SELECT p.carouselNumber FROM Promotion p")
List<CarouselNumber> findAllCarouselNumbers();

@Modifying(clearAutomatically = true)
@Transactional
@Query("DELETE FROM Promotion p WHERE p.carouselNumber IN :carouselNumbers")
void deleteByCarouselNumbers(@Param("carouselNumbers") List<CarouselNumber> carouselNumbers);
@Query("DELETE FROM Promotion p WHERE p.id IN :promotionIds")
void deleteByPromotionIds(@Param("promotionIds") List<Long> promotionIds);

@Query("SELECT p FROM Promotion p WHERE p.carouselNumber = :carouselNumber")
Optional<Promotion> findByCarouselNumber(@Param("carouselNumber") CarouselNumber carouselNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@ Promotion createPromotion(String newImageUrl, Performance performance, String re

Promotion modifyPromotion(Promotion promotion, Performance performance, PromotionModifyRequest request);

void deleteByCarouselNumber(List<CarouselNumber> carouselNumbers);

List<CarouselNumber> findAllCarouselNumbers();

Promotion findPromotionByCarouselNumber(CarouselNumber carouselNumber);
void deletePromotionsByPromotionIds(List<Long> promotionIds);
}

0 comments on commit 46d5a3f

Please sign in to comment.