Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] post edit API to receive s3 url #129

Merged
merged 4 commits into from
Feb 16, 2025
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 @@ -42,6 +42,7 @@ public enum ErrorStatus implements BaseErrorCode {
MEMBER_TICKET_ALREADY_CANCELED(HttpStatus.BAD_REQUEST, "MEMBERTICKET4003", "이미 μ·¨μ†Œν•˜μ‹  ν‹°μΌ“μž…λ‹ˆλ‹€."),
//POST ERROR
POST_NOT_FOUND(HttpStatus.NOT_FOUND, "POST4000", "μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” κ²Œμ‹œκΈ€μž…λ‹ˆλ‹€."),
IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "IMAGE4000", "이미지λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),

//REPORT ERROR
UNSUPPORTED_OBJECT_TYPE(HttpStatus.BAD_REQUEST, "REPORT4000", "μ§€μ›ν•˜μ§€ μ•ŠλŠ” μ‹ κ³  νƒ€μž…μž…λ‹ˆλ‹€."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import muit.backend.domain.enums.PostType;
import muit.backend.domain.enums.ReportObjectType;
import muit.backend.domain.enums.Role;
import muit.backend.dto.postDTO.PatchPostRequestDTO;
import muit.backend.dto.postDTO.PostRequestDTO;
import muit.backend.dto.postDTO.PostResponseDTO;
import muit.backend.dto.reportDTO.ReportRequestDTO;
import muit.backend.dto.reportDTO.ReportResponseDTO;
Expand All @@ -28,8 +30,10 @@
import muit.backend.service.postService.PostService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -155,4 +159,14 @@ public ApiResponse<PostResponseDTO.MyPostResultListDTO> myPost(@RequestHeader("A
return ApiResponse.onSuccess(list);
}

@PatchMapping(value = "/{postId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "κ²Œμ‹œκΈ€ μˆ˜μ • API", description = "νŠΉμ • κ²Œμ‹œκΈ€μ„ μˆ˜μ •ν•˜λŠ” API μž…λ‹ˆλ‹€.")
public ApiResponse<PostResponseDTO.GeneralPostResponseDTO> editPost(@RequestHeader("Authorization") String accessToken,
@PathVariable("postId") Long postId,
@RequestPart("postRequestDTO") PatchPostRequestDTO postRequestDTO,
@RequestPart(name = "imageFiles", required = false)List<MultipartFile> img) {
Member member = memberService.getMemberByToken(accessToken);
return ApiResponse.onSuccess(postService.editPost(postId, postRequestDTO, img, member));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,4 @@ public ApiResponse<LostResponseDTO.GeneralLostResponseDTO> getPost(@RequestHeade
return ApiResponse.onSuccess(lostService.getLostPost(postId,member));
}


@PatchMapping(value = "/{postId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "λΆ„μ‹€ κ²Œμ‹œκΈ€ μˆ˜μ • API", description = "λΆ„μ‹€ κ²Œμ‹œκΈ€μ„ μˆ˜μ •ν•˜λŠ” API μž…λ‹ˆλ‹€.")
public ApiResponse<LostResponseDTO.GeneralLostResponseDTO> editPost(@RequestHeader("Authorization") String accessToken,
@PathVariable("postId") Long postId,
@RequestPart("lostRequestDTO") LostRequestDTO lostRequestDTO,
@RequestPart(name = "imageFiles", required = false)List<MultipartFile> img) {

Member member = memberService.getMemberByToken(accessToken);
return ApiResponse.onSuccess(lostService.editLostPost(postId, lostRequestDTO, img, member));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,4 @@ public ApiResponse<PostResponseDTO.GeneralPostResponseDTO> getPost(@RequestHeade
return ApiResponse.onSuccess(postService.getPost(postId, member));
}


@PatchMapping(value = "/{postId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "κ²Œμ‹œκΈ€ μˆ˜μ • API", description = "νŠΉμ • κ²Œμ‹œκΈ€μ„ μˆ˜μ •ν•˜λŠ” API μž…λ‹ˆλ‹€.")
public ApiResponse<PostResponseDTO.GeneralPostResponseDTO> editPost(@RequestHeader("Authorization") String accessToken,
@PathVariable("postId") Long postId,
@RequestPart("postRequestDTO") PostRequestDTO postRequestDTO,
@RequestPart(name = "imageFiles", required = false)List<MultipartFile> img) {
Member member = memberService.getMemberByToken(accessToken);
return ApiResponse.onSuccess(postService.editPost(postId, postRequestDTO, img, member));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ public ApiResponse<ReviewResponseDTO.GeneralReviewResponseDTO> getReview(@Reques
return ApiResponse.onSuccess(reviewService.getReview(postId,member));
}

@PatchMapping(value = "/{postId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "리뷰 κ²Œμ‹œκΈ€ μˆ˜μ • API", description = "νŠΉμ • 리뷰 κ²Œμ‹œκΈ€μ„ μˆ˜μ •ν•˜λŠ” API μž…λ‹ˆλ‹€.")
public ApiResponse<ReviewResponseDTO.GeneralReviewResponseDTO> editReview(@RequestHeader("Authorization") String accessToken,
@PathVariable("postId") Long postId,
@RequestPart("reviewRequestDTO") ReviewRequestDTO reviewRequestDTO,
@RequestPart(name = "imageFiles", required = false)List<MultipartFile> img) {
Member member = memberService.getMemberByToken(accessToken);

return ApiResponse.onSuccess(reviewService.editReview(postId, reviewRequestDTO, img, member));
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static ReviewResponseDTO.GeneralReviewResponseDTO toReviewResponseDTO(Pos

return ReviewResponseDTO.GeneralReviewResponseDTO.builder()
.id(review.getId())
.isMyPost(isMyPost)
.nickname(name)
.memberId(review.getMember().getId())
.musicalId(review.getMusical().getId())
Expand Down
36 changes: 15 additions & 21 deletions src/main/java/muit/backend/domain/entity/member/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import muit.backend.domain.entity.musical.Musical;
import muit.backend.domain.enums.PostType;
import muit.backend.dto.postDTO.LostRequestDTO;
import muit.backend.dto.postDTO.PatchPostRequestDTO;
import muit.backend.dto.postDTO.PostRequestDTO;
import muit.backend.dto.postDTO.ReviewRequestDTO;
import muit.backend.s3.UuidFile;
Expand Down Expand Up @@ -78,11 +79,13 @@ public void changeMusical(Musical musical){
this.musical = musical;
}

public void changeImg(List<UuidFile> imgList){
if(!this.images.isEmpty()){
this.images.clear();
public void changeImg(List<UuidFile> newImgList, List<UuidFile> oldImgList){
if(oldImgList.size()!=this.images.size()){//κΈ°μ‘΄ 사진 κ°œμˆ˜κ°€ λ‹¬λΌμ‘ŒμœΌλ©΄
this.images.retainAll(oldImgList);//dtoμ—μ„œ 보내쀀 μ΄λ―Έμ§€λ§Œ 남기고 μ‚­μ œλœ ν•­λͺ©μ€ 고아객체 λ§Œλ“€κΈ°
}
if(!newImgList.isEmpty()){
this.images.addAll(newImgList);//μƒˆλ‘œμš΄ 이미지 μΆ”κ°€λœ 건 λ”°λ‘œ μΆ”κ°€
}
this.images.addAll(imgList);
}

public void increaseMaxIndex(){
Expand All @@ -106,26 +109,17 @@ public void changeReportCount(Boolean isAdd){
}


public Post changePost(PostRequestDTO postRequestDTO) {
if(postRequestDTO.getTitle()!=null){this.title = postRequestDTO.getTitle();}
if(postRequestDTO.getContent()!=null){this.content = postRequestDTO.getContent();}
return this;

}

public Post changeLost(LostRequestDTO lostRequestDTO){
if(lostRequestDTO.getTitle()!=null){this.title = lostRequestDTO.getTitle();}
if(lostRequestDTO.getContent()!=null){this.content = lostRequestDTO.getContent();}
if(lostRequestDTO.getLocation()!=null){this.location = lostRequestDTO.getLocation();}
if(lostRequestDTO.getLostItem()!=null){this.lostItem = lostRequestDTO.getLostItem();}
if(lostRequestDTO.getLostDate()!=null){this.lostDate = lostRequestDTO.getLostDate();}
public Post changePost(PatchPostRequestDTO postRequestDTO){
if(postRequestDTO.getIsAnonymous()!=null){this.isAnonymous = postRequestDTO.getIsAnonymous();}
if(postRequestDTO.getTitle()!=null){this.title = postRequestDTO.getTitle();}
if(postRequestDTO.getContent()!=null){this.content = postRequestDTO.getContent();}
if(postRequestDTO.getLocation()!=null){this.location = postRequestDTO.getLocation();}
if(postRequestDTO.getLostItem()!=null){this.lostItem = postRequestDTO.getLostItem();}
if(postRequestDTO.getLostDate()!=null){this.lostDate = postRequestDTO.getLostDate();}
if(postRequestDTO.getRating()!=null){this.rating = postRequestDTO.getRating();}
return this;
}

public Post changeReview(ReviewRequestDTO reviewRequestDTO){
if(reviewRequestDTO.getTitle()!=null){this.title = reviewRequestDTO.getTitle();}
if(reviewRequestDTO.getContent()!=null){this.content = reviewRequestDTO.getContent();}
if(reviewRequestDTO.getRating()!=null){this.rating = reviewRequestDTO.getRating();}
return this;
}
}
25 changes: 25 additions & 0 deletions src/main/java/muit/backend/dto/postDTO/PatchPostRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package muit.backend.dto.postDTO;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class PatchPostRequestDTO {
private Boolean isAnonymous;
private String title;
private String content;
private List<String> originalImgUrls;
private LocalDate lostDate;
private String location;
private String lostItem;
private Long musicalId;
private Integer rating;

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,4 @@ public interface LostService {

//κ²Œμ‹œκΈ€ 생성
public LostResponseDTO.GeneralLostResponseDTO createLostPost(PostType postType, LostRequestDTO lostRequestDTO, List<MultipartFile> img, Member member);

//κ²Œμ‹œκΈ€ μˆ˜μ •
LostResponseDTO.GeneralLostResponseDTO editLostPost(Long postId, LostRequestDTO lostRequestDTO, List<MultipartFile> img, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,39 +119,5 @@ public LostResponseDTO.GeneralLostResponseDTO createLostPost(PostType postType,
return LostConverter.toGeneralLostResponseDTO(post,member);
}

@Override
@Transactional
public LostResponseDTO.GeneralLostResponseDTO editLostPost(Long postId, LostRequestDTO lostRequestDTO, List<MultipartFile> imgFile, Member member) {
//post μœ νš¨μ„± 검사
Post post = postRepository.findById(postId)
.orElseThrow(() -> new GeneralException(ErrorStatus.POST_NOT_FOUND));

//μž‘μ„±μžμ™€ 동일인인지 검사
if(post.getMember()!=member){
throw(new GeneralException(ErrorStatus._FORBIDDEN));
}

FilePath filePath = switch (post.getPostType()){
case LOST -> FilePath.LOST;
case FOUND -> FilePath.FOUND;
default -> throw new RuntimeException("Unsupported post type");
};

//κΈ°μ‘΄ 이미지 λ¨Όμ € μ‚­μ œ
List<UuidFile> existingImg = post.getImages();
if(!existingImg.isEmpty()){
existingImg.forEach(uuidFileService::deleteFile);
}
//μˆ˜μ •λœ 이미지 μ‚½μž…
List<UuidFile> imgArr = new ArrayList<>();
if(imgFile!=null&&!imgFile.isEmpty()){
imgArr = imgFile.stream().map(img->uuidFileService.createFile(img, FilePath.REVIEW)).collect(Collectors.toList());
}
post.changeImg(imgArr);

//λ‚˜λ¨Έμ§€ ν•„λ“œ μˆ˜μ •
Post changedPost = post.changeLost(lostRequestDTO);

return LostConverter.toGeneralLostResponseDTO(changedPost,member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import muit.backend.domain.entity.member.Member;
import muit.backend.domain.enums.PostType;
import muit.backend.dto.postDTO.PatchPostRequestDTO;
import muit.backend.dto.postDTO.PostRequestDTO;
import muit.backend.dto.postDTO.PostResponseDTO;
import muit.backend.dto.reportDTO.ReportRequestDTO;
Expand All @@ -28,7 +29,7 @@ public interface PostService {
public PostResponseDTO.DeleteResultDTO deletePost(Long id, Member member);

//κ²Œμ‹œκΈ€ μˆ˜μ •
public PostResponseDTO.GeneralPostResponseDTO editPost(Long postId, PostRequestDTO postRequestDTO, List<MultipartFile> img, Member member);
public PostResponseDTO.GeneralPostResponseDTO editPost(Long postId, PatchPostRequestDTO postRequestDTO, List<MultipartFile> img, Member member);

PostResponseDTO.likeResultDTO likePost(Long postId, Member member);

Expand Down
32 changes: 21 additions & 11 deletions src/main/java/muit/backend/service/postService/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import muit.backend.domain.entity.member.Post;
import muit.backend.domain.entity.member.PostLikes;
import muit.backend.domain.entity.member.Report;
import muit.backend.domain.entity.musical.Musical;
import muit.backend.domain.enums.PostType;
import muit.backend.domain.enums.ReportObjectType;
import muit.backend.domain.enums.Role;
import muit.backend.dto.postDTO.PatchPostRequestDTO;
import muit.backend.dto.postDTO.PostRequestDTO;
import muit.backend.dto.postDTO.PostResponseDTO;
import muit.backend.dto.reportDTO.ReportRequestDTO;
Expand All @@ -26,8 +28,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

@Service
Expand All @@ -38,6 +39,7 @@ public class PostServiceImpl implements PostService {
private final PostLikesRepository postLikesRepository;
private final UuidFileService uuidFileService;
private final ReportRepository reportRepository;
private final MusicalRepository musicalRepository;

//κ²Œμ‹œκΈ€ μž‘μ„±
@Override
Expand Down Expand Up @@ -143,7 +145,7 @@ public PostResponseDTO.DeleteResultDTO deletePost(Long id, Member member) {
//κ²Œμ‹œκΈ€ μˆ˜μ •
@Override
@Transactional
public PostResponseDTO.GeneralPostResponseDTO editPost(Long postId, PostRequestDTO requestDTO, List<MultipartFile> imgFile, Member member) {
public PostResponseDTO.GeneralPostResponseDTO editPost(Long postId, PatchPostRequestDTO requestDTO, List<MultipartFile> imgFile, Member member) {

//post μœ νš¨μ„± 검사
Post post = postRepository.findById(postId)
Expand All @@ -154,17 +156,25 @@ public PostResponseDTO.GeneralPostResponseDTO editPost(Long postId, PostRequestD
throw(new GeneralException(ErrorStatus._FORBIDDEN));
}

//κΈ°μ‘΄ 이미지 λ¨Όμ € μ‚­μ œ
List<UuidFile> existingImg = post.getImages();
if(!existingImg.isEmpty()){
existingImg.forEach(uuidFileService::deleteFile);
//λ¦¬λ·°ν˜• κ²Œμ‹œκΈ€μΌ 경우 musical λ³€κ²½ 반영
if(post.getPostType().equals(PostType.REVIEW)||post.getPostType().equals(PostType.SIGHT)){
if(requestDTO.getMusicalId()!=null){
Musical musical = musicalRepository.findById(requestDTO.getMusicalId()).orElseThrow(() -> new GeneralException(ErrorStatus.MUSICAL_NOT_FOUND));
post.changeMusical(musical);
}

}
//μˆ˜μ •λœ 이미지 μ‚½μž…
List<UuidFile> imgArr = new ArrayList<>();

//κΈ°μ‘΄ 이미지 url->UuidFileν™”
List<UuidFile> dtoImgs = requestDTO.getOriginalImgUrls().stream().map(file->
uuidFileService.getUuidFileByFileUrl(file).orElseThrow(()->new GeneralException(ErrorStatus.IMAGE_NOT_FOUND))).toList();

//μˆ˜μ •λœ 이미지 s3 생성
List<UuidFile> newImgs = new ArrayList<>();
if(imgFile!=null&&!imgFile.isEmpty()){
imgArr = imgFile.stream().map(img->uuidFileService.createFile(img, FilePath.BLIND)).collect(Collectors.toList());
newImgs = imgFile.stream().map(img->uuidFileService.createFile(img, FilePath.BLIND)).collect(Collectors.toList());
}
post.changeImg(imgArr);
post.changeImg(newImgs,dtoImgs);

//ν•„λ“œ μˆ˜μ •
Post changedPost = post.changePost(requestDTO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ public interface ReviewService {
ReviewResponseDTO.ReviewListResponseDTO getReviewList(PostType postType, Member member, Integer page, Integer size, String musicalName, String location);

ReviewResponseDTO.GeneralReviewResponseDTO getReview(Long postId,Member member);

ReviewResponseDTO.GeneralReviewResponseDTO editReview(Long postId, ReviewRequestDTO reviewRequestDTO, List<MultipartFile> img, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,50 +83,4 @@ public ReviewResponseDTO.GeneralReviewResponseDTO getReview(Long postId,Member m
return ReviewConverter.toReviewResponseDTO(review,member);
}

//리뷰 μˆ˜μ •
@Override
@Transactional
public ReviewResponseDTO.GeneralReviewResponseDTO editReview(Long postId, ReviewRequestDTO requestDTO, List<MultipartFile> imgFile,Member member) {

//post μœ νš¨μ„± 검사
Post review = postRepository.findById(postId)
.orElseThrow(() -> new GeneralException(ErrorStatus.POST_NOT_FOUND));

//μž‘μ„±μžμ™€ 동일인인지 검사
if(review.getMember()!=member){
throw(new GeneralException(ErrorStatus._FORBIDDEN));
}

//musical μœ νš¨μ„± 검사
if(requestDTO.getMusicalId()!=null){Long musicalId = requestDTO.getMusicalId();
Musical musical = musicalRepository.findById(musicalId)
.orElseThrow(() -> new GeneralException(ErrorStatus.MUSICAL_NOT_FOUND));
//musical λ¨Όμ € μˆ˜μ •
review.changeMusical(musical);}

FilePath filePath = switch (review.getPostType()){
case REVIEW -> FilePath.REVIEW;
case SIGHT -> FilePath.SIGHT;
default -> throw new RuntimeException("Unsupported post type");
};

//κΈ°μ‘΄ 이미지 λ¨Όμ € μ‚­μ œ
List<UuidFile> existingImg = review.getImages();
if(!existingImg.isEmpty()){
existingImg.forEach(uuidFileService::deleteFile);
}
//μˆ˜μ •λœ 이미지 μ‚½μž…
List<UuidFile> imgArr = new ArrayList<>();
if(imgFile!=null&&!imgFile.isEmpty()){

imgArr = imgFile.stream().map(img->uuidFileService.createFile(img, filePath)).collect(Collectors.toList());
}
review.changeImg(imgArr);


//λ‚˜λ¨Έμ§€ ν•„λ“œ μˆ˜μ •
Post changedPost = review.changeReview(requestDTO);

return ReviewConverter.toReviewResponseDTO(changedPost, member);
}
}