Skip to content

Commit

Permalink
Merge pull request #73 from Team-Shaka/refactor/72
Browse files Browse the repository at this point in the history
Refactor/72 - 이미지 업로드 방식의 변경
  • Loading branch information
koojun99 authored Mar 24, 2024
2 parents a51feeb + d0dadaf commit 42ac615
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 37 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 @@ -73,13 +65,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
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@
public class ProfileController {
private final ProfileService profileService;

@PostMapping(value = "/trees/owner/register", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PostMapping("/trees/owner/register")
@Operation(summary = "트리하우스 설립자 프로필 등록", description = "트리하우스 오너 프로필을 등록합니다.")
public ApiResponse registerTreeOwner(
@RequestPart ProfileRequestDTO.ownerProfile request,
@RequestPart("profileImage") final MultipartFile profileImage
@RequestBody ProfileRequestDTO.ownerProfile request
) throws Exception {
return ApiResponse.onSuccess(profileService.ownerProfile(request, profileImage));
return ApiResponse.onSuccess(profileService.ownerProfile(request));
}

@PostMapping(value = "/trees/members/register", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PostMapping("/trees/members/register")
@Operation(summary = "트리하우스 멤버 프로필 등록", description = "트리하우스 멤버 프로필을 등록합니다.")
public ApiResponse registerTreeMember(
@RequestPart ProfileRequestDTO.createProfile request,
@RequestPart("profileImage") final MultipartFile profileImage
@RequestBody ProfileRequestDTO.createProfile request
) throws Exception {
return ApiResponse.onSuccess(profileService.createProfile(request, profileImage));
return ApiResponse.onSuccess(profileService.createProfile(request));
}

@GetMapping("/trees/{treeId}/members/{profileId}") //프로필 조회
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class createProfile {
private Long treeId;
private String userId;
private String memberName;
private MultipartFile profileImage;
private String profileImage;
private String bio;

}
Expand All @@ -22,7 +22,7 @@ public static class ownerProfile {
private Long treeId;
private String userId;
private String memberName;
private MultipartFile profileImage;
private String profileImage;
private String bio;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ public class ProfileService {
private final ProfileConverter profileConverter;
private final TreeQueryService treeQueryService;
private final MemberQueryService memberQueryService;
private final S3UploadService s3UploadService;
private final BranchService branchService;
private final InvitationQueryService invitationQueryService;

@Transactional
public ProfileResponseDTO.createProfile createProfile(ProfileRequestDTO.createProfile request, MultipartFile profileImage) throws Exception {
public ProfileResponseDTO.createProfile createProfile(ProfileRequestDTO.createProfile request) throws Exception {
Tree tree = treeQueryService.findById(request.getTreeId());
Member member = memberQueryService.findById(request.getUserId());
boolean isNewUser = profileQueryService.isNewUser(member);
Expand All @@ -43,7 +42,7 @@ public ProfileResponseDTO.createProfile createProfile(ProfileRequestDTO.createPr
currentProfile.inactivate();
profileCommandService.updateProfile(currentProfile);
}
String profileImageUrl = !profileImage.isEmpty() ? s3UploadService.uploadImage(profileImage) : DEFAULT_PROFILE_IMAGE;
String profileImageUrl = request.getProfileImage() == null ? DEFAULT_PROFILE_IMAGE : request.getProfileImage();
Profile newProfile = profileConverter.toProfile(tree, member, request.getMemberName(), request.getBio(), profileImageUrl);
profileCommandService.createProfile(newProfile);
tree.increaseTreeSize();
Expand All @@ -54,7 +53,7 @@ public ProfileResponseDTO.createProfile createProfile(ProfileRequestDTO.createPr
}

@Transactional
public ProfileResponseDTO.createProfile ownerProfile(ProfileRequestDTO.ownerProfile request, MultipartFile profileImage) throws Exception {
public ProfileResponseDTO.createProfile ownerProfile(ProfileRequestDTO.ownerProfile request) throws Exception {
Tree tree = treeQueryService.findById(request.getTreeId());
Member member = memberQueryService.findById(request.getUserId());
boolean isNewUser = profileQueryService.isNewUser(member);
Expand All @@ -63,7 +62,7 @@ public ProfileResponseDTO.createProfile ownerProfile(ProfileRequestDTO.ownerProf
currentProfile.inactivate();
profileCommandService.updateProfile(currentProfile);
}
String profileImageUrl = !profileImage.isEmpty() ? s3UploadService.uploadImage(profileImage) : DEFAULT_PROFILE_IMAGE;
String profileImageUrl = request.getProfileImage() == null ? DEFAULT_PROFILE_IMAGE : request.getProfileImage();
Profile newProfile = profileConverter.toProfile(tree, member, request.getMemberName(), request.getBio(), profileImageUrl);
profileCommandService.createProfile(newProfile);
tree.increaseTreeSize();
Expand Down

0 comments on commit 42ac615

Please sign in to comment.