From 4f22d0935f9b2e99aa8f4e90274c5ae5fb0ebfad Mon Sep 17 00:00:00 2001 From: bora Date: Sat, 8 Apr 2023 01:01:52 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Feat:=20PostController=20=EB=B0=8F=20PostPo?= =?UTF-8?q?stReq=20DTO=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todaysgym/post/PostController.java | 36 +++++++++++++++++++ .../todaysgym/post/dto/PostPostReq.java | 4 +++ 2 files changed, 40 insertions(+) create mode 100644 src/main/java/com/todaysgym/todaysgym/post/PostController.java create mode 100644 src/main/java/com/todaysgym/todaysgym/post/dto/PostPostReq.java diff --git a/src/main/java/com/todaysgym/todaysgym/post/PostController.java b/src/main/java/com/todaysgym/todaysgym/post/PostController.java new file mode 100644 index 0000000..05e0c8c --- /dev/null +++ b/src/main/java/com/todaysgym/todaysgym/post/PostController.java @@ -0,0 +1,36 @@ +package com.todaysgym.todaysgym.post; + +import com.todaysgym.todaysgym.config.response.BaseResponse; +import com.todaysgym.todaysgym.member.Member; +import com.todaysgym.todaysgym.post.dto.PostPostReq; +import com.todaysgym.todaysgym.record.dto.RecordGetReq; +import com.todaysgym.todaysgym.report.dto.ReportReq; +import com.todaysgym.todaysgym.utils.UtilService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.validation.Valid; +import java.util.List; + +@RestController +@RequiredArgsConstructor +public class PostController { + + private final UtilService utilService; + private final PostService postService; + + /** 게시글 생성하기 + * [POST] /post + */ + @PostMapping("/post") + public BaseResponse createPost(@RequestPart(value = "image", required = false) List multipartFiles, + @RequestPart(value = "postPostReq") @Valid PostPostReq postPostReq){ + Member writer = utilService.findByMemberIdWithValidation(1L); + return new BaseResponse<>(postService.createPost(writer, postPostReq, multipartFiles)); + } + +} diff --git a/src/main/java/com/todaysgym/todaysgym/post/dto/PostPostReq.java b/src/main/java/com/todaysgym/todaysgym/post/dto/PostPostReq.java new file mode 100644 index 0000000..5ab193d --- /dev/null +++ b/src/main/java/com/todaysgym/todaysgym/post/dto/PostPostReq.java @@ -0,0 +1,4 @@ +package com.todaysgym.todaysgym.post.dto; + +public class PostPostReq { +} From 8786743475f59f3f69f0e7544ffd0f272f12c7ea Mon Sep 17 00:00:00 2001 From: bora Date: Sat, 8 Apr 2023 02:39:53 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Feat:=20createPost,=20PostPhoto=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/todaysgym/todaysgym/post/Post.java | 22 +++++++ .../todaysgym/todaysgym/post/PostService.java | 35 ++++++++++ .../todaysgym/post/dto/PostPostReq.java | 19 ++++++ .../todaysgym/post/photo/PostPhoto.java | 8 +++ .../post/photo/PostPhotoRepository.java | 15 +++++ .../post/photo/PostPhotoService.java | 66 +++++++++++++++++++ 6 files changed, 165 insertions(+) create mode 100644 src/main/java/com/todaysgym/todaysgym/post/photo/PostPhotoService.java diff --git a/src/main/java/com/todaysgym/todaysgym/post/Post.java b/src/main/java/com/todaysgym/todaysgym/post/Post.java index 5bd82b4..ec2a420 100644 --- a/src/main/java/com/todaysgym/todaysgym/post/Post.java +++ b/src/main/java/com/todaysgym/todaysgym/post/Post.java @@ -5,6 +5,7 @@ import com.todaysgym.todaysgym.post.comment.Comment; import com.todaysgym.todaysgym.post.photo.PostPhoto; import com.todaysgym.todaysgym.record.Record; +import com.todaysgym.todaysgym.record.photo.RecordPhoto; import com.todaysgym.todaysgym.utils.BaseTimeEntity; import java.util.ArrayList; import java.util.List; @@ -63,4 +64,25 @@ public void deleteRecord(){ public void addReportCount() { this.report++; } + + public static Post createPost(Category category, Member writer, String title, String content, Record record){ + Post post = new Post(); + post.setCategory(category); + post.setRecord(record); + post.createTitle(title); + post.createContent(content); + post.createMember(writer); + return post; + } + + public void setCategory(Category category) {this.category = category;} + public void setRecord(Record record) {this.record = record;} + public void createMember(Member member){this.member = member;} + public void createContent(String content){this.content = content;} + public void createTitle(String title){this.title = title;} + + public void createPhotoList(PostPhoto postPhoto){ + photoList.add(postPhoto); + postPhoto.createPost(this); + } } \ No newline at end of file diff --git a/src/main/java/com/todaysgym/todaysgym/post/PostService.java b/src/main/java/com/todaysgym/todaysgym/post/PostService.java index 9bb7d23..1a2b20d 100644 --- a/src/main/java/com/todaysgym/todaysgym/post/PostService.java +++ b/src/main/java/com/todaysgym/todaysgym/post/PostService.java @@ -1,12 +1,24 @@ package com.todaysgym.todaysgym.post; import com.todaysgym.todaysgym.config.exception.BaseException; +import com.todaysgym.todaysgym.member.Member; import com.todaysgym.todaysgym.post.PostRepository; +import com.todaysgym.todaysgym.post.dto.PostPostReq; +import com.todaysgym.todaysgym.post.photo.PostPhotoRepository; +import com.todaysgym.todaysgym.post.photo.PostPhotoService; +import com.todaysgym.todaysgym.record.Record; +import com.todaysgym.todaysgym.record.dto.RecordGetReq; import com.todaysgym.todaysgym.record.photo.RecordPhotoRepository; import com.todaysgym.todaysgym.record.photo.RecordPhotoService; +import com.todaysgym.todaysgym.utils.S3Service; +import com.todaysgym.todaysgym.utils.UtilService; +import com.todaysgym.todaysgym.utils.dto.getS3Res; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.util.ArrayList; import java.util.List; import static com.todaysgym.todaysgym.config.exception.errorCode.RecordErrorCode.EMPTY_RECORD; @@ -15,6 +27,9 @@ @RequiredArgsConstructor public class PostService { private final PostRepository postRepository; + private final PostPhotoService postPhotoService; + private final S3Service s3Service; + private final UtilService utilService; /** * 기록과 관련된 게시글 조회 후 기록 null로 변경 @@ -22,4 +37,24 @@ public class PostService { public List deleteRecord(Long recordId){ return postRepository.findPostByRecord(recordId); } + + @Transactional + public String createPost(Member writer, PostPostReq postPostReq, List multipartFiles) throws BaseException { + // 1. 기록 첨부 여부 조회 -> 기록 없으면 NULL로 저장 + Record record = null; + if(postPostReq.getRecordId() != null) { + record = utilService.findByRecordIdWithValidation(postPostReq.getRecordId()); + } + // 2. 게시글 생성 + Post post = Post.createPost(postPostReq.getCategory(), writer, postPostReq.getTitle(), postPostReq.getContent(), record); + postRepository.save(post); + + // 3. 이미지 첨부했다면 저장 + if(multipartFiles != null) { + List imgUrls = s3Service.uploadFile(multipartFiles); + postPhotoService.saveAllPostPhotoByPost(imgUrls, post); + } + + return "postId: " + post.getPostId() + "인 게시글을 생성했습니다."; + } } diff --git a/src/main/java/com/todaysgym/todaysgym/post/dto/PostPostReq.java b/src/main/java/com/todaysgym/todaysgym/post/dto/PostPostReq.java index 5ab193d..7ab9246 100644 --- a/src/main/java/com/todaysgym/todaysgym/post/dto/PostPostReq.java +++ b/src/main/java/com/todaysgym/todaysgym/post/dto/PostPostReq.java @@ -1,4 +1,23 @@ package com.todaysgym.todaysgym.post.dto; +import com.todaysgym.todaysgym.category.Category; +import com.todaysgym.todaysgym.tag.Tag; +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.util.ArrayList; +import java.util.List; + +@Data public class PostPostReq { + @NotNull + private Category category; + @NotBlank + private String title; + @NotBlank + private String content; + + private Long recordId; // null 일 수도 있음 + } diff --git a/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhoto.java b/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhoto.java index 99ad201..07e4da3 100644 --- a/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhoto.java +++ b/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhoto.java @@ -9,12 +9,16 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; + +import com.todaysgym.todaysgym.record.Record; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @Entity @Getter +@Builder @AllArgsConstructor @NoArgsConstructor @Table(name = "post_photo") @@ -30,4 +34,8 @@ public class PostPhoto { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post_id") private Post post; + + public void createPost(Post post){ + this.post = post; + } } \ No newline at end of file diff --git a/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhotoRepository.java b/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhotoRepository.java index 0c3e165..3947578 100644 --- a/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhotoRepository.java +++ b/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhotoRepository.java @@ -1,7 +1,22 @@ package com.todaysgym.todaysgym.post.photo; +import com.todaysgym.todaysgym.record.photo.RecordPhoto; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; public interface PostPhotoRepository extends JpaRepository { + @Query("SELECT pp FROM PostPhoto pp WHERE pp.post.postId = :postId") + List findAllByPost(@Param("postId") Long postId); + + @Query("SELECT pp.id FROM PostPhoto pp WHERE pp.post.postId = :postId") + List findAllId(@Param("postId") Long postId); + + @Modifying + @Query("DELETE FROM PostPhoto pp WHERE pp.id IN :ids") + Integer deleteAllByPost(@Param("ids") List ids); } diff --git a/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhotoService.java b/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhotoService.java new file mode 100644 index 0000000..4f4fbde --- /dev/null +++ b/src/main/java/com/todaysgym/todaysgym/post/photo/PostPhotoService.java @@ -0,0 +1,66 @@ +package com.todaysgym.todaysgym.post.photo; + +import com.todaysgym.todaysgym.post.Post; +import com.todaysgym.todaysgym.record.photo.RecordPhoto; +import com.todaysgym.todaysgym.utils.S3Service; +import com.todaysgym.todaysgym.utils.dto.getS3Res; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Service +@AllArgsConstructor +public class PostPhotoService { + + private final PostPhotoRepository postPhotoRepository; + private final S3Service s3Service; + + @Transactional + public void savePostPhoto(List postPhotos){ + postPhotoRepository.saveAll(postPhotos); + } + + public List findByPostId(Long postId){ return postPhotoRepository.findAllByPost(postId); } + + /** + * 여러개의 postPhoto 저장 + */ + @Transactional + public void saveAllPostPhotoByPost(List getS3ResList, Post post) { + List postPhotos = new ArrayList<>(); + for(getS3Res getS3Res : getS3ResList){ + PostPhoto postPhoto = PostPhoto.builder() + .imgUrl(getS3Res.getImgUrl()) + .fileName(getS3Res.getFileName()) + .build(); + postPhotos.add(postPhoto); + post.createPhotoList(postPhoto); + } + savePostPhoto(postPhotos); + } + + /** + * 게시글과 연관된 모든 postPhoto 삭제 + */ + @Transactional + public void deleteAllPostPhotos(List postPhotos){ + for (PostPhoto postPhoto : postPhotos) { + s3Service.deleteFile(postPhoto.getFileName()); + } + } + + @Transactional + public void deleteAllPostPhotoByPost(List ids){ + postPhotoRepository.deleteAllByPost(ids); + } + + /** + * post와 연관된 모든 id 조회 + */ + public List findAllId(Long postId){ + return postPhotoRepository.findAllId(postId); + } +}