Skip to content

Commit

Permalink
feat: 레시피 삭제 시 자동으로 S3에서 이미지 삭제되도록 수정 - #102
Browse files Browse the repository at this point in the history
  • Loading branch information
jher235 authored Aug 5, 2024
2 parents 0cc2e56 + 89dd885 commit 97365ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.hackathonteam1.refreshrator.repository;

import com.hackathonteam1.refreshrator.entity.Image;
import com.hackathonteam1.refreshrator.entity.Recipe;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
import java.util.UUID;

public interface ImageRepository extends JpaRepository<Image, UUID> {
Optional<Image> findByRecipe(Recipe recipe);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@


import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -159,6 +156,10 @@ public void modifyContent(ModifyRecipeDto modifyRecipeDto, User user, UUID recip
public void delete(UUID recipeId, User user) {
Recipe recipe = findRecipeByRecipeId(recipeId);
checkAuth(recipe.getUser(), user);
if(recipe.isContainingImage()){
Image image = findImageByRecipe(recipe);
s3Uploader.removeS3File(image.getUrl().split("/")[3]);
}
recipeRepository.delete(recipe);
}

Expand Down Expand Up @@ -397,4 +398,8 @@ private <T> void checkValidPage(Page<T> pages, int page){
throw new NotFoundException(ErrorCode.PAGE_NOT_FOUND);
}
}

private Image findImageByRecipe(Recipe recipe){
return imageRepository.findByRecipe(recipe).orElseThrow(()->new NotFoundException(ErrorCode.IMAGE_NOT_FOUND));
}
}

0 comments on commit 97365ee

Please sign in to comment.