Skip to content

Commit

Permalink
Merge pull request #153 from Me1tingPot/feature/#152
Browse files Browse the repository at this point in the history
[fix] ๊ฒŒ์‹œ๊ธ€, ๋Œ“๊ธ€ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ๋กœ์ง ์ˆ˜์ •
  • Loading branch information
moonyaeyoon authored Nov 10, 2024
2 parents df98a98 + 9b79c92 commit 6971a07
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import meltingpot.server.util.CurrentUser;
import meltingpot.server.util.ResponseCode;
import meltingpot.server.util.ResponseData;
import meltingpot.server.util.r2.FileService;
import meltingpot.server.util.r2.FileUploadResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -19,7 +21,14 @@
@RequestMapping("/api/v1/comments")
public class CommentController {
private final CommentService commentService;
private final FileService fileService;

@Operation(summary = "์ด๋ฏธ์ง€ ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•œ Pre-signed URL ์ƒ์„ฑ")
@PostMapping("/upload-url")
public ResponseEntity<FileUploadResponse> getUploadUrl(@RequestParam String prefix) {
FileUploadResponse response = fileService.getPreSignedUrl(prefix);
return ResponseEntity.ok(response);
}
@Operation(summary = "๋Œ“๊ธ€ ์ž‘์„ฑ, ์ด๋ฏธ์ง€๊ฐ€ ์—†์œผ๋ฉด null๋กœ ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ")
@PostMapping("/{postId}")
public ResponseEntity<ResponseData> createComment(@RequestBody CommentCreateRequest commentCreateRequest, @CurrentUser Account account, @PathVariable Long postId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import meltingpot.server.post.dto.PostDetailResponse;
import meltingpot.server.post.dto.PostsListResponse;
import meltingpot.server.util.r2.FileService;
import meltingpot.server.util.r2.FileUploadResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import meltingpot.server.util.*;
Expand All @@ -23,7 +25,14 @@
@RequestMapping("/api/v1/posts")
public class PostController {
private final PostService postService;
private final FileService fileService;

@Operation(summary = "์ด๋ฏธ์ง€ ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•œ Pre-signed URL ์ƒ์„ฑ")
@PostMapping("/upload-url")
public ResponseEntity<FileUploadResponse> getUploadUrl(@RequestParam String prefix) {
FileUploadResponse response = fileService.getPreSignedUrl(prefix);
return ResponseEntity.ok(response);
}
@Operation(summary = "๊ฒŒ์‹œ๋ฌผ ์ž‘์„ฑ", description="requestParam์œผ๋กœ ์ž„์‹œ์ €์žฅ ์—ฌ๋ถ€๋ฅผ ์•Œ๋ ค์ฃผ์„ธ์š”." +
"์ด๋ฏธ์ง€๊ฐ€ ์—†์„ ๋•Œ๋Š” ๋นˆ ๊ฐ’์œผ๋กœ ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.")
@PostMapping("")
Expand Down
48 changes: 20 additions & 28 deletions src/main/java/meltingpot/server/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ResponseCode updatePost(PostCreateRequest updateRequest,Long postId, Acco
Post post = findPostById(postId);
isAuthenticated ( post, account);
updatePostContent(post, account, updateRequest);
setPostImages(post,account,updateRequest);
setPostImages(post, updateRequest.getImageKeys(), account);

postRepository.save(post);

Expand Down Expand Up @@ -89,7 +89,7 @@ public PostsListResponse getPostsList(Account account, PostType postType, Long
/*post ์‚ญ์ œํ•˜๊ธฐ*/
public ResponseCode deletePost(Long postId, Account account){
Post post = findPostById(postId);
isAuthenticated ( post, account);
isAuthenticated (post, account);

// ๊ฒŒ์‹œ๋ฌผ์— ์—ฐ๊ด€๋œ ์ด๋ฏธ์ง€ ์‚ญ์ œ
if (!post.getPostImages().isEmpty()) {
Expand Down Expand Up @@ -131,46 +131,38 @@ private void isAuthenticated (Post post, Account account) {
}
}

private List<String> getCdnUrls(List<String> imageKeys) {
return imageKeys.stream()
.map(imageKey -> {
String prefix = "post"; // ์ ์ ˆํ•œ prefix ๊ฐ’์„ ์„ค์ •
return fileService.getCdnUrl(prefix, imageKey);
})
.collect(Collectors.toList());
}

private void updatePostContent(Post post, Account account, PostCreateRequest updateRequest) {
private void updatePostContent(Post post, PostCreateRequest updateRequest) {
post.setTitle(updateRequest.getTitle());
post.setContent(updateRequest.getContent());
}

private void setPostImages(Post post, List<String> imageKeys, Account account) {
clearExistingImages(post);
List<PostImage> postImages = createPostImages(imageKeys, post, account);
post.setPostImages(postImages);
}

// ๊ธฐ์กด์˜ ๋ชจ๋“  PostImage ์‚ญ์ œ
private void clearExistingImages(Post post) {
if (post.getPostImages() != null && !post.getPostImages().isEmpty()) {
postImageRepository.deleteAll(post.getPostImages());
post.getPostImages().clear();
}
}

private List<PostImage> createPostImages(List<String> imageKeys, Post post, Account account) {
List<String> postImgUrls = getCdnUrls(imageKeys);
return postImgUrls.stream()
.map(imageUrl -> PostImage.builder()
.imageUrl(imageUrl)
.post(post)
.account(account)
.build())
return imageKeys.stream()
.map(imageKey -> {
String imageUrl = fileService.getCdnUrl("post", imageKey); // prefix๋Š” "post"๋กœ ์„ค์ •
return PostImage.builder()
.imageUrl(imageUrl)
.post(post)
.account(account)
.build();
})
.collect(Collectors.toList());
}

private Optional<Post> getDraftPost(Account account) {
return postRepository.findByAccountAndIsDraftTrue(account);
}

private void setPostImages(Post post, Account account,PostCreateRequest postRequest) {
List<PostImage> postImages = createPostImages(postRequest.getImageKeys(), post, account);
post.setPostImages(postImages);
}


}

0 comments on commit 6971a07

Please sign in to comment.