From 13a7c57cc1302a0497c194707662237fa5ca86ff Mon Sep 17 00:00:00 2001 From: shinheekim Date: Tue, 30 Jul 2024 18:44:13 +0900 Subject: [PATCH] merge --- .../post/application/PostService.java | 87 ++++++++++++++----- .../likelion12thteam03be/s3/S3Service.java | 7 ++ 2 files changed, 73 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java index b819ff9..1592538 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java @@ -5,15 +5,21 @@ import net.skhu.likelion12thteam03be.category.domain.repository.CategoryRepository; import net.skhu.likelion12thteam03be.location.domain.Location; import net.skhu.likelion12thteam03be.location.domain.repository.LocationRepository; +import net.skhu.likelion12thteam03be.mood.domain.Mood; +import net.skhu.likelion12thteam03be.mood.domain.repository.MoodRepository; import net.skhu.likelion12thteam03be.post.api.dto.request.PostSaveReqDto; import net.skhu.likelion12thteam03be.post.api.dto.request.PostUpdateReqDto; import net.skhu.likelion12thteam03be.post.api.dto.response.PostInfoResDto; import net.skhu.likelion12thteam03be.post.api.dto.response.PostListResDto; import net.skhu.likelion12thteam03be.post.domain.Post; import net.skhu.likelion12thteam03be.post.domain.repository.PostRepository; +import net.skhu.likelion12thteam03be.s3.S3Service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; +import java.security.Principal; import java.util.List; import java.util.Optional; @@ -24,25 +30,32 @@ public class PostService { private final PostRepository postRepository; private final CategoryRepository categoryRepository; private final LocationRepository locationRepository; + private final S3Service s3Service; + private final MoodRepository moodRepository; @Transactional - public void postSave(PostSaveReqDto postSaveReqDto) { - Category category = categoryRepository.findById(postSaveReqDto.categoryId()) - .orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postSaveReqDto.categoryId())); + public void postSave(PostSaveReqDto postSaveReqDto, MultipartFile multipartFile, Principal principal) throws IOException { + String imgUrl = s3Service.upload(multipartFile, "post"); + Long id = Long.parseLong(principal.getName()); Location location = locationRepository.findById(postSaveReqDto.locationId()) .orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postSaveReqDto.locationId())); + Category category = categoryRepository.findById(postSaveReqDto.categoryId()) + .orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postSaveReqDto.categoryId())); + + Mood mood = moodRepository.findById(postSaveReqDto.moodId()) + .orElseThrow(() -> new IllegalArgumentException("해당 분위기가 존재하지 않습니다. moodId = " + postSaveReqDto.moodId())); + Post post = Post.builder() .title(postSaveReqDto.title()) .content(postSaveReqDto.content()) - .imgUrl(postSaveReqDto.imgUrl()) .location(location) .time(postSaveReqDto.time()) .price(postSaveReqDto.price()) .category(category) - .emotionId(postSaveReqDto.emotionId()) - .colorId(postSaveReqDto.colorId()) + .mood(mood) + .imgUrl(imgUrl) .build(); postRepository.save(post); @@ -69,7 +82,9 @@ public PostInfoResDto postFindById(Long postId) { // 글 위치별 조회 public PostListResDto postFindByLocationId(Long locationId) { - Optional post = postRepository.findById(locationId); + Optional post = Optional.ofNullable(postRepository.findById(locationId).orElseThrow( + () -> new IllegalArgumentException("해당 위치의 글을 조회할 수 없습니다. locationId = " + locationId) + )); List postInfoResDtoList = post.stream() .map(PostInfoResDto::from) @@ -80,7 +95,9 @@ public PostListResDto postFindByLocationId(Long locationId) { // 글 카테고리별 조회 public PostListResDto postFindByCategoryId(Long categoryId) { - Optional posts = postRepository.findById(categoryId); + Optional posts = Optional.ofNullable(postRepository.findById(categoryId).orElseThrow( + () -> new IllegalArgumentException("해당 카테고리의 글을 찾을 수 없습니다. categoryId = " + categoryId) + )); List postInfoResDtoList = posts.stream() .map(PostInfoResDto::from) @@ -89,9 +106,11 @@ public PostListResDto postFindByCategoryId(Long categoryId) { return PostListResDto.from(postInfoResDtoList); } - // 글 작성자별 조회(내 글 조회) - public PostListResDto postFindByUserId(Long userId) { - Optional posts = postRepository.findById(userId); + // 글 분위기별 조회 + public PostListResDto postFindByMoodId(Long moodId) { + Optional posts = Optional.ofNullable(postRepository.findById(moodId).orElseThrow( + () -> new IllegalArgumentException("해당 분위기의 글을 찾을 수 없습니다. moodId = " + moodId) + )); List postInfoResDtoList = posts.stream() .map(PostInfoResDto::from) @@ -100,38 +119,64 @@ public PostListResDto postFindByUserId(Long userId) { return PostListResDto.from(postInfoResDtoList); } - // 글 감정별 조회 + // 글 작성자별 조회(내 글 조회) + public PostListResDto postFindByUserId(Long userId) { + Optional posts = Optional.ofNullable(postRepository.findById(userId).orElseThrow( + () -> new IllegalArgumentException("해당 사용자가 작성한 글을 찾을 수 없습니다. userId = " + userId) + )); - // 글 색상별 조회 + List postInfoResDtoList = posts.stream() + .map(PostInfoResDto::from) + .toList(); - // 글 검색 조회 + return PostListResDto.from(postInfoResDtoList); + } + // 글 검색 조회 // 글 수정 @Transactional - public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto) { + public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto, MultipartFile multipartFile) throws IOException { Post post = postRepository.findById(postId).orElseThrow( () -> new IllegalArgumentException("해당 글을 수정할 수 없습니다. postId = " + postId) ); + Location location = locationRepository.findById(postUpdateReqDto.locationId()) + .orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postUpdateReqDto.locationId())); + Category category = categoryRepository.findById(postUpdateReqDto.categoryId()) .orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postUpdateReqDto.categoryId())); - Location location = locationRepository.findById(postUpdateReqDto.locationId()) - .orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postUpdateReqDto.locationId())); + Mood mood = moodRepository.findById(postUpdateReqDto.moodId()) + .orElseThrow(() -> new IllegalArgumentException("해당 분위기가 존재하지 않습니다. moodId = " + postUpdateReqDto.moodId())); + + String imgUrl = s3Service.upload(multipartFile, "post"); - post.update(location, category, postUpdateReqDto); + post.update(location, category, postUpdateReqDto, mood, imgUrl); PostInfoResDto.from(post); } // 글 삭제 @Transactional - public void postDelete(Long postId) { + public void postDelete(Long postId) throws IOException { Post post = postRepository.findById(postId).orElseThrow( () -> new IllegalArgumentException("해당 글을 삭제할 수 없습니다. postId = " + postId) ); - postRepository.delete(post); + Optional imgUrl = Optional.ofNullable(post.getImgUrl()); + + imgUrl.ifPresentOrElse( + url -> { + try { + s3Service.delete(url, "post"); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("이미지 삭제 중 오류 발생", e); + } + postRepository.delete(post); + }, + () -> { + throw new IllegalArgumentException("이미지 URL이 존재하지 않습니다. postId = " + postId); + } + ); } - } diff --git a/src/main/java/net/skhu/likelion12thteam03be/s3/S3Service.java b/src/main/java/net/skhu/likelion12thteam03be/s3/S3Service.java index 8f80583..ee047a9 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/s3/S3Service.java +++ b/src/main/java/net/skhu/likelion12thteam03be/s3/S3Service.java @@ -1,6 +1,7 @@ package net.skhu.likelion12thteam03be.s3; import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.DeleteObjectRequest; import com.amazonaws.services.s3.model.PutObjectRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -68,4 +69,10 @@ public Optional convert(MultipartFile multipartFile) throws IOException { return Optional.empty(); } + public void delete(String imgUrl, String dirName) { + String imgName = imgUrl.substring(imgUrl.lastIndexOf("/") + 1); + String fileName = dirName + "/" + imgName; + + amazonS3.deleteObject(new DeleteObjectRequest(bucket, fileName)); + } }