Skip to content

Commit

Permalink
MATE-149 : [!HOTFIX] 굿즈거래 판매글 이미지 수정 로직 오류 해결 (#133)
Browse files Browse the repository at this point in the history
* MATE-149 : [!HOTFIX] 굿즈거래 판매글 이미지 수정 로직 오류 해결

- form-data 에 이미지("files")가 요청되지 않는 경우, 이미지를 수정하지 않도록 로직 변경

* MATE-149 : [STYLE] GoodsPostService 코드 스타일 수정
  • Loading branch information
hongjeZZ authored Jan 9, 2025
1 parent cf05da5 commit c956165
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ResponseEntity<ApiResponse<GoodsPostResponse>> updateGoodsPost(
@AuthenticationPrincipal AuthMember member,
@Parameter(description = "판매글 ID", required = true) @PathVariable Long goodsPostId,
@Parameter(description = "수정할 판매글 데이터", required = true) @Validated @RequestPart("data") GoodsPostRequest request,
@Parameter(description = "수정할 첨부 파일 리스트", required = true) @RequestPart("files") List<MultipartFile> files
@Parameter(description = "수정할 첨부 파일 리스트") @RequestPart(value = "files", required = false) List<MultipartFile> files
) {
GoodsPostResponse response = goodsPostService.updateGoodsPost(member.getMemberId(), goodsPostId, request, files);
return ResponseEntity.ok(ApiResponse.success(response));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import com.example.mate.domain.member.entity.ActivityType;
import com.example.mate.domain.member.entity.Member;
import com.example.mate.domain.member.repository.MemberRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -54,24 +55,28 @@ public GoodsPostResponse registerGoodsPost(Long memberId, GoodsPostRequest reque
return GoodsPostResponse.of(savedPost);
}

public GoodsPostResponse updateGoodsPost(Long memberId, Long goodsPostId, GoodsPostRequest request,
List<MultipartFile> files) {
public GoodsPostResponse updateGoodsPost(Long memberId, Long goodsPostId, GoodsPostRequest request, List<MultipartFile> files) {
Member seller = findMemberById(memberId);
GoodsPost goodsPost = findGoodsPostById(goodsPostId);

validateTeamInfo(request.getTeamId());
validateOwnership(seller, goodsPost);
FileValidator.validateGoodsPostImages(files);

GoodsPost updateTarget = GoodsPostRequest.toEntity(seller, request);
goodsPost.updatePostDetails(updateTarget);

deleteExistingImageFiles(goodsPostId);
attachImagesToGoodsPost(goodsPost, files);
updateGoodsPostImages(goodsPostId, goodsPost, files);

return GoodsPostResponse.of(goodsPost);
}

private void updateGoodsPostImages(Long goodsPostId, GoodsPost goodsPost, List<MultipartFile> files) {
if (files != null && !files.isEmpty()) {
FileValidator.validateGoodsPostImages(files);
deleteExistingImageFiles(goodsPostId);
attachImagesToGoodsPost(goodsPost, files);
}
}

public void deleteGoodsPost(Long memberId, Long goodsPostId) {
Member seller = findMemberById(memberId);
GoodsPost goodsPost = findGoodsPostById(goodsPostId);
Expand All @@ -92,18 +97,15 @@ public GoodsPostResponse getGoodsPost(Long goodsPostId) {
@Transactional(readOnly = true)
public List<GoodsPostSummaryResponse> getMainGoodsPosts(Long teamId) {
validateTeamInfo(teamId);
return mapToGoodsPostSummaryResponses(
goodsPostRepository.findMainGoodsPosts(teamId, Status.OPEN, PageRequest.of(0, 4))
);
return mapToGoodsPostSummaryResponses(goodsPostRepository.findMainGoodsPosts(teamId, Status.OPEN, PageRequest.of(0, 4)));
}

@Transactional(readOnly = true)
public PageResponse<GoodsPostSummaryResponse> getPageGoodsPosts(Long teamId, String categoryVal,
Pageable pageable) {
public PageResponse<GoodsPostSummaryResponse> getPageGoodsPosts(Long teamId, String categoryVal, Pageable pageable) {
validateTeamInfo(teamId);
Category category = Category.from(categoryVal);
Page<GoodsPost> pageGoodsPosts = goodsPostRepository.findPageGoodsPosts(teamId, Status.OPEN, category,
pageable);
Page<GoodsPost> pageGoodsPosts = goodsPostRepository.findPageGoodsPosts(teamId, Status.OPEN, category, pageable);

return PageResponse.from(pageGoodsPosts, mapToGoodsPostSummaryResponses(pageGoodsPosts.getContent()));
}

Expand All @@ -125,32 +127,13 @@ private void attachImagesToGoodsPost(GoodsPost goodsPost, List<MultipartFile> fi
}

private List<GoodsPostImage> uploadImageFiles(List<MultipartFile> files, GoodsPost savedPost) {
List<GoodsPostImage> images = new ArrayList<>();

// 첫 번째 파일은 썸네일 생성과 함께 업로드
images.add(handleImageUpload(files.get(0), savedPost, true));

// 나머지 파일들은 일반 업로드
for (int i = 1; i < files.size(); i++) {
images.add(handleImageUpload(files.get(i), savedPost, false));
}

return images;
}

private GoodsPostImage handleImageUpload(MultipartFile file, GoodsPost savedPost, boolean isMainImage) {
String uploadUrl;

if (isMainImage) {
uploadUrl = fileService.uploadImageWithThumbnail(file); // 썸네일 업로드
} else {
uploadUrl = fileService.uploadFile(file); // 일반 업로드
}

return GoodsPostImage.builder()
.imageUrl(uploadUrl)
.post(savedPost)
.build();
return IntStream.range(0, files.size())
.mapToObj(i -> GoodsPostImage.builder()
// 첫번째 사진일 경우 썸네일과 함께 저장
.imageUrl(i == 0 ? fileService.uploadImageWithThumbnail(files.get(i)) : fileService.uploadFile(files.get(i)))
.post(savedPost)
.build())
.collect(Collectors.toList());
}

private void deleteExistingImageFiles(Long goodsPostId) {
Expand Down

0 comments on commit c956165

Please sign in to comment.