Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shinheekim committed Jul 30, 2024
1 parent 15df63e commit 13a7c57
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -69,7 +82,9 @@ public PostInfoResDto postFindById(Long postId) {

// ๊ธ€ ์œ„์น˜๋ณ„ ์กฐํšŒ
public PostListResDto postFindByLocationId(Long locationId) {
Optional<Post> post = postRepository.findById(locationId);
Optional<Post> post = Optional.ofNullable(postRepository.findById(locationId).orElseThrow(
() -> new IllegalArgumentException("ํ•ด๋‹น ์œ„์น˜์˜ ๊ธ€์„ ์กฐํšŒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. locationId = " + locationId)
));

List<PostInfoResDto> postInfoResDtoList = post.stream()
.map(PostInfoResDto::from)
Expand All @@ -80,7 +95,9 @@ public PostListResDto postFindByLocationId(Long locationId) {

// ๊ธ€ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ์กฐํšŒ
public PostListResDto postFindByCategoryId(Long categoryId) {
Optional<Post> posts = postRepository.findById(categoryId);
Optional<Post> posts = Optional.ofNullable(postRepository.findById(categoryId).orElseThrow(
() -> new IllegalArgumentException("ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. categoryId = " + categoryId)
));

List<PostInfoResDto> postInfoResDtoList = posts.stream()
.map(PostInfoResDto::from)
Expand All @@ -89,9 +106,11 @@ public PostListResDto postFindByCategoryId(Long categoryId) {
return PostListResDto.from(postInfoResDtoList);
}

// ๊ธ€ ์ž‘์„ฑ์ž๋ณ„ ์กฐํšŒ(๋‚ด ๊ธ€ ์กฐํšŒ)
public PostListResDto postFindByUserId(Long userId) {
Optional<Post> posts = postRepository.findById(userId);
// ๊ธ€ ๋ถ„์œ„๊ธฐ๋ณ„ ์กฐํšŒ
public PostListResDto postFindByMoodId(Long moodId) {
Optional<Post> posts = Optional.ofNullable(postRepository.findById(moodId).orElseThrow(
() -> new IllegalArgumentException("ํ•ด๋‹น ๋ถ„์œ„๊ธฐ์˜ ๊ธ€์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. moodId = " + moodId)
));

List<PostInfoResDto> postInfoResDtoList = posts.stream()
.map(PostInfoResDto::from)
Expand All @@ -100,38 +119,64 @@ public PostListResDto postFindByUserId(Long userId) {
return PostListResDto.from(postInfoResDtoList);
}

// ๊ธ€ ๊ฐ์ •๋ณ„ ์กฐํšŒ
// ๊ธ€ ์ž‘์„ฑ์ž๋ณ„ ์กฐํšŒ(๋‚ด ๊ธ€ ์กฐํšŒ)
public PostListResDto postFindByUserId(Long userId) {
Optional<Post> posts = Optional.ofNullable(postRepository.findById(userId).orElseThrow(
() -> new IllegalArgumentException("ํ•ด๋‹น ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. userId = " + userId)
));

// ๊ธ€ ์ƒ‰์ƒ๋ณ„ ์กฐํšŒ
List<PostInfoResDto> 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<String> 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);
}
);
}

}
7 changes: 7 additions & 0 deletions src/main/java/net/skhu/likelion12thteam03be/s3/S3Service.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -68,4 +69,10 @@ public Optional<File> 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));
}
}

0 comments on commit 13a7c57

Please sign in to comment.