Skip to content

Commit

Permalink
[REFACTOR]/#72-게시글 작성 시, 이미지 등록 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
koojun99 committed Mar 16, 2024
1 parent 681e103 commit d0dadaf
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
public class PostController {
private final PostService postService;

@PostMapping(value = "/trees/{treeId}/feed/posts", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PostMapping("/trees/{treeId}/feed/posts")
public ApiResponse<PostResponseDTO.createPost> createPost(
@PathVariable final Long treeId,
@RequestHeader("Authorization") final String header,
@RequestPart(value="request") final PostRequestDTO.createPost request,
@RequestPart(value="images") final List<MultipartFile> images
@RequestBody final PostRequestDTO.createPost request
) throws Exception {
String token = header.replace("Bearer ", "");
return ApiResponse.onSuccess(postService.createPost(treeId, request, images, token));
return ApiResponse.onSuccess(postService.createPost(treeId, request, token));
}

@GetMapping("/trees/{treeId}/feed")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package org.example.tree.domain.post.converter;

import lombok.RequiredArgsConstructor;
import org.example.tree.domain.post.dto.PostRequestDTO;
import org.example.tree.domain.post.dto.PostResponseDTO;
import org.example.tree.domain.post.entity.Post;
import org.example.tree.domain.post.entity.PostImage;
import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.tree.entity.Tree;
import org.example.tree.global.common.amazons3.S3UploadService;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class PostConverter {
private final S3UploadService s3UploadService;

public Post toPost(String content, Profile profile) {
return Post.builder()
Expand All @@ -39,9 +34,6 @@ public PostResponseDTO.createPost toCreatePost(Post savedPost) {
.collect(Collectors.toList());
return PostResponseDTO.createPost.builder()
.postId(savedPost.getId())
.postImageUrls(imageUrls)
.authorId(savedPost.getProfile().getMember().getId())
.treeId(savedPost.getTree().getId())
.build();
}

Expand Down Expand Up @@ -72,13 +64,12 @@ public PostResponseDTO.getPost toGetPost(Post post, int branchDegree, List<React
.build();
}

public List<PostImage> toPostImages(List<MultipartFile> images) throws Exception {
public List<PostImage> toPostImages(List<String> images) throws Exception {
List<PostImage> postImages = new ArrayList<>();
for(MultipartFile image :images) {
if (!image.isEmpty()) {
String postImageUrl = s3UploadService.uploadImage(image); // 이미지 업로드 후 URL 획득
for(String imageUrl :images) {
if (imageUrl != null) {// 이미지 업로드 후 URL 획득
PostImage postImage = PostImage.builder()
.imageUrl(postImageUrl) // PostImage 엔티티에는 imageUrl 필드가 있어야 합니다.
.imageUrl(imageUrl) // PostImage 엔티티에는 imageUrl 필드가 있어야 합니다.
.build();
postImages.add(postImage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;


@NoArgsConstructor(access = AccessLevel.PRIVATE)

Expand All @@ -12,6 +14,7 @@ public class PostRequestDTO {
@Getter
public static class createPost {
private String content;
private List<String> images;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public class PostResponseDTO {
@AllArgsConstructor
public static class createPost {
private Long postId;
private List<String> postImageUrls;
private String authorId;
private Long treeId;
}

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class PostService {
private final BranchService branchService;

@Transactional
public PostResponseDTO.createPost createPost(Long treeId, PostRequestDTO.createPost request, List<MultipartFile> images, String token) throws Exception {
public PostResponseDTO.createPost createPost(Long treeId, PostRequestDTO.createPost request, String token) throws Exception {
Profile profile = profileService.getTreeProfile(token, treeId);
List<PostImage> postImages = postConverter.toPostImages(images);
List<PostImage> postImages = postConverter.toPostImages(request.getImages());
Post post = postConverter.toPost(request.getContent(), profile);
Post savedPost = postCommandService.createPost(post);
for (PostImage postImage : postImages) {
Expand Down

0 comments on commit d0dadaf

Please sign in to comment.