Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}


}

Loading