diff --git a/server/src/main/java/server/haengdong/application/EventImageFacadeService.java b/server/src/main/java/server/haengdong/application/EventImageFacadeService.java index 76fb6111..1a0f26bc 100644 --- a/server/src/main/java/server/haengdong/application/EventImageFacadeService.java +++ b/server/src/main/java/server/haengdong/application/EventImageFacadeService.java @@ -1,88 +1,88 @@ - package server.haengdong.application; +package server.haengdong.application; - import java.util.List; - import java.util.concurrent.CompletableFuture; - import java.util.concurrent.CompletionException; - import java.util.concurrent.ExecutorService; - import java.util.stream.IntStream; - import lombok.RequiredArgsConstructor; - import org.springframework.stereotype.Service; - import org.springframework.web.multipart.MultipartFile; - import server.haengdong.application.response.EventImageSaveAppResponse; - import server.haengdong.exception.HaengdongErrorCode; - import server.haengdong.exception.HaengdongException; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutorService; +import java.util.stream.IntStream; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import server.haengdong.application.response.EventImageSaveAppResponse; +import server.haengdong.exception.HaengdongErrorCode; +import server.haengdong.exception.HaengdongException; - @RequiredArgsConstructor - @Service - public class EventImageFacadeService { +@RequiredArgsConstructor +@Service +public class EventImageFacadeService { - private final EventService eventService; - private final ImageService imageService; - private final ExecutorService executorService; + private final EventService eventService; + private final ImageService imageService; + private final ExecutorService executorService; - public void uploadImages(String token, List images) { - List imageNames = eventService.saveImages(token, getOriginalNames(images)); - List> futures = createFutures(images, imageNames); - CompletableFuture> allFutures = createAllFutures(token, futures, imageNames); + public void uploadImages(String token, List images) { + List imageNames = eventService.saveImages(token, getOriginalNames(images)); + List> futures = createFutures(images, imageNames); + CompletableFuture> allFutures = createAllFutures(token, futures, imageNames); - try { - allFutures.join(); - } catch (CompletionException e) { - throw new HaengdongException(HaengdongErrorCode.IMAGE_UPLOAD_FAIL, e); - } + try { + allFutures.join(); + } catch (CompletionException e) { + throw new HaengdongException(HaengdongErrorCode.IMAGE_UPLOAD_FAIL, e); } + } - private List> createFutures( - List images, List imageNames - ) { - return IntStream.range(0, imageNames.size()) - .mapToObj(i -> CompletableFuture.supplyAsync(() -> { - imageService.uploadImage(images.get(i), imageNames.get(i).name()); - return imageNames.get(i).name(); - }, executorService)) - .toList(); - } + private List> createFutures( + List images, List imageNames + ) { + return IntStream.range(0, imageNames.size()) + .mapToObj(i -> CompletableFuture.supplyAsync(() -> { + imageService.uploadImage(images.get(i), imageNames.get(i).name()); + return imageNames.get(i).name(); + }, executorService)) + .toList(); + } - private CompletableFuture> createAllFutures( - String token, List> futures, List imageNames - ) { - return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) - .thenApply(v -> applyFutures(futures)) - .exceptionally(ex -> { - eventService.deleteImages(token, getImageIds(imageNames)); - getSuccessUploadImages(futures).forEach(imageService::deleteImage); + private CompletableFuture> createAllFutures( + String token, List> futures, List imageNames + ) { + return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .thenApply(v -> applyFutures(futures)) + .exceptionally(ex -> { + eventService.deleteImages(token, getImageIds(imageNames)); + getSuccessUploadImages(futures).forEach(imageService::deleteImage); - throw new HaengdongException(HaengdongErrorCode.IMAGE_UPLOAD_FAIL, ex); - }); - } + throw new HaengdongException(HaengdongErrorCode.IMAGE_UPLOAD_FAIL, ex); + }); + } - private List applyFutures(List> futures) { - return futures.stream() - .map(CompletableFuture::join) - .toList(); - } + private List applyFutures(List> futures) { + return futures.stream() + .map(CompletableFuture::join) + .toList(); + } - private List getSuccessUploadImages(List> futures) { - return futures.stream() - .filter(future -> future.isDone() && !future.isCompletedExceptionally()) - .map(CompletableFuture::join) - .toList(); - } + private List getSuccessUploadImages(List> futures) { + return futures.stream() + .filter(future -> future.isDone() && !future.isCompletedExceptionally()) + .map(CompletableFuture::join) + .toList(); + } - private List getOriginalNames(List images) { - return images.stream() - .map(MultipartFile::getOriginalFilename) - .toList(); - } + private List getOriginalNames(List images) { + return images.stream() + .map(MultipartFile::getOriginalFilename) + .toList(); + } - private List getImageIds(List imageNames) { - return imageNames.stream() - .map(EventImageSaveAppResponse::id) - .toList(); - } + private List getImageIds(List imageNames) { + return imageNames.stream() + .map(EventImageSaveAppResponse::id) + .toList(); + } - public void deleteImage(String token, Long imageId) { - String imageName = eventService.deleteImage(token, imageId); - imageService.deleteImage(imageName); - } + public void deleteImage(String token, Long imageId) { + String imageName = eventService.deleteImage(token, imageId); + imageService.deleteImage(imageName); } +}