From d655a99c4b19a54ffb6756020a8f9cbc279042e6 Mon Sep 17 00:00:00 2001 From: JH Park Date: Sun, 3 Nov 2024 18:06:11 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Refactor:=20=EB=8B=A4=EC=9D=B4=EC=96=B4?= =?UTF-8?q?=EB=A6=AC=20=EC=83=9D=EC=84=B1=20API=EC=97=90=EC=84=9C=20=20nul?= =?UTF-8?q?l=EA=B0=92=EC=9C=BC=EB=A1=9C=20=EC=9E=85=EB=A0=A5=20=EB=93=A4?= =?UTF-8?q?=EC=96=B4=EC=98=A4=EB=8D=98=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostController.java | 26 +++++++-- .../domain/post/converter/PostConverter.java | 16 +++--- .../post/dto/request/PostRequestDTO.java | 41 +++++++++----- .../post/dto/response/PostResponseDTO.java | 54 ++++++++++++------- .../backend/domain/post/entity/Post.java | 4 +- .../post/service/PostCommandService.java | 6 +-- 6 files changed, 99 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/codiary/backend/domain/post/controller/PostController.java b/src/main/java/com/codiary/backend/domain/post/controller/PostController.java index 6abd01ca..cd3b5e64 100644 --- a/src/main/java/com/codiary/backend/domain/post/controller/PostController.java +++ b/src/main/java/com/codiary/backend/domain/post/controller/PostController.java @@ -48,11 +48,27 @@ public ApiResponse createPost(@ModelAttribu return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toCreateResultDTO(newPost)); } - // 멤버의 게시글 수정하기 - - - // 게시글 삭제하기 - +// // 멤버의 게시글 수정하기 +// @PatchMapping(path = "/{postId}", consumes = "multipart/form-data") +// @Operation(summary = "다이어리 수정 API", description = "다이어리를 수정합니다.") +// public ApiResponse updatePost(@ModelAttribute PostRequestDTO.UpdatePostDTO request, @PathVariable Long postId){ +// Member member = memberCommandService.getRequester(); +// jwtTokenProvider.isValidToken(member.getMemberId()); +// +// return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toUpdatePostResultDTO(postCommandService.updatePost(postId, request))); +// } + + +// // 게시글 삭제하기 +// @DeleteMapping("/{postId}") +// @Operation(summary = "다이어리 삭제 API", description = "다이어리를 삭제합니다.") +// public ApiResponse deletePost(@PathVariable Long postId){ +// Member member = memberCommandService.getRequester(); +// jwtTokenProvider.isValidToken(member.getMemberId()); +// +// postCommandService.deletePost(postId); +// return ApiResponse.onSuccess(SuccessStatus.POST_OK, null); +// } diff --git a/src/main/java/com/codiary/backend/domain/post/converter/PostConverter.java b/src/main/java/com/codiary/backend/domain/post/converter/PostConverter.java index dabe44b1..33d69309 100644 --- a/src/main/java/com/codiary/backend/domain/post/converter/PostConverter.java +++ b/src/main/java/com/codiary/backend/domain/post/converter/PostConverter.java @@ -33,19 +33,19 @@ private static PostResponseDTO.SimplePostResponseDTO toSimplePostResponseDto(Pos public static Post toPost(PostRequestDTO.CreatePostRequestDTO request, TeamRepository teamRepository, ProjectRepository projectRepository) { Team team = null; Project project = null; - if (request.teamId() != null) { - team = teamRepository.findById(request.teamId()).orElseThrow(() -> new IllegalArgumentException("Team not found with id: " + request.teamId())); + if (request.getTeamId() != null) { + team = teamRepository.findById(request.getTeamId()).orElseThrow(() -> new IllegalArgumentException("Team not found with id: " + request.getTeamId())); } - if (request.projectId() != null) { - project = projectRepository.findById(request.projectId()).orElseThrow(() -> new IllegalArgumentException("Project not found with id: " + request.projectId())); + if (request.getProjectId() != null) { + project = projectRepository.findById(request.getProjectId()).orElseThrow(() -> new IllegalArgumentException("Project not found with id: " + request.getProjectId())); } return Post.builder() - .postTitle(request.postTitle()) - .postBody(request.postBody()) + .postTitle(request.getPostTitle()) + .postBody(request.getPostBody()) .team(team) .project(project) - .postStatus(request.postStatus() != null ? request.postStatus() : true) // 기본값 설정 - .postAccess(request.postAccess() != null ? request.postAccess() : PostAccess.MEMBER) // 기본값 설정 + .postStatus(request.getPostStatus() != null ? request.getPostStatus() : true) // 기본값 설정 + .postAccess(request.getPostAccess() != null ? request.getPostAccess() : PostAccess.MEMBER) // 기본값 설정 .build(); } diff --git a/src/main/java/com/codiary/backend/domain/post/dto/request/PostRequestDTO.java b/src/main/java/com/codiary/backend/domain/post/dto/request/PostRequestDTO.java index 3ef2b8e7..cea1cdf0 100644 --- a/src/main/java/com/codiary/backend/domain/post/dto/request/PostRequestDTO.java +++ b/src/main/java/com/codiary/backend/domain/post/dto/request/PostRequestDTO.java @@ -12,19 +12,36 @@ public class PostRequestDTO { +// @Builder +// @JsonInclude(JsonInclude.Include.NON_NULL) +// @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +// public record CreatePostRequestDTO ( +// Long teamId, +// Long projectId, +// String postTitle, +// String postBody, +// Boolean postStatus, +// PostAccess postAccess, +// String thumbnailImageName, +// List postFiles +// ) { +// } + + @Getter + //@Setter @Builder - @JsonInclude(JsonInclude.Include.NON_NULL) - @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) - public record CreatePostRequestDTO ( - Long teamId, - Long projectId, - String postTitle, - String postBody, - Boolean postStatus, - PostAccess postAccess, - String thumbnailImageName, - List postFiles - ) { + @AllArgsConstructor + //@NoArgsConstructor + public static class CreatePostRequestDTO { + private Long teamId; + private Long projectId; + private String postTitle; + private String postBody; + private Boolean postStatus; + //private Set postCategory; + private PostAccess postAccess; + private String thumbnailImageName; + private List postFiles; } } diff --git a/src/main/java/com/codiary/backend/domain/post/dto/response/PostResponseDTO.java b/src/main/java/com/codiary/backend/domain/post/dto/response/PostResponseDTO.java index 094e956a..937a6f46 100644 --- a/src/main/java/com/codiary/backend/domain/post/dto/response/PostResponseDTO.java +++ b/src/main/java/com/codiary/backend/domain/post/dto/response/PostResponseDTO.java @@ -25,23 +25,41 @@ public record SimplePostResponseDTO( } - @Builder - @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) - @JsonInclude(JsonInclude.Include.NON_NULL) - public record CreatePostResultDTO( - Long postId, - Long memberId, - Long teamId, - Long projectId, - String postTitle, - String postBody, - String thumbnailImageUrl, - Boolean postStatus, - String postCategory, - Set coauthorIds, - PostAccess postAccess, - PostFileResponseDTO.PostFileListDTO postFileList - ){ +// @Builder +// @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +// @JsonInclude(JsonInclude.Include.NON_NULL) +// public record CreatePostResultDTO( +// Long postId, +// Long memberId, +// Long teamId, +// Long projectId, +// String postTitle, +// String postBody, +// String thumbnailImageUrl, +// Boolean postStatus, +// String postCategory, +// Set coauthorIds, +// PostAccess postAccess, +// PostFileResponseDTO.PostFileListDTO postFileList +// ){ +// } +@Getter +@Builder +@AllArgsConstructor +//@NoArgsConstructor +public static class CreatePostResultDTO { + Long postId; + Long memberId; + Long teamId; + Long projectId; + String postTitle; + String postBody; + String thumbnailImageUrl; + Boolean postStatus; + String postCategory; + Set coauthorIds; + PostAccess postAccess; + PostFileResponseDTO.PostFileListDTO postFileList; +} - } } diff --git a/src/main/java/com/codiary/backend/domain/post/entity/Post.java b/src/main/java/com/codiary/backend/domain/post/entity/Post.java index b78c98c6..e3f62a7b 100644 --- a/src/main/java/com/codiary/backend/domain/post/entity/Post.java +++ b/src/main/java/com/codiary/backend/domain/post/entity/Post.java @@ -38,10 +38,10 @@ public class Post extends BaseEntity { @JoinColumn(name = "project_id") private Project project; - @Column(name = "post_title", columnDefinition = "varchar(500)") + @Column(name = "post_title", nullable = false, columnDefinition = "varchar(500)") private String postTitle; - @Column(name = "post_body", columnDefinition = "varchar(3000)") + @Column(name = "post_body", nullable = false, columnDefinition = "varchar(3000)") private String postBody; @OneToOne diff --git a/src/main/java/com/codiary/backend/domain/post/service/PostCommandService.java b/src/main/java/com/codiary/backend/domain/post/service/PostCommandService.java index 0262c500..e98636ca 100644 --- a/src/main/java/com/codiary/backend/domain/post/service/PostCommandService.java +++ b/src/main/java/com/codiary/backend/domain/post/service/PostCommandService.java @@ -49,8 +49,8 @@ public Post createPost(PostRequestDTO.CreatePostRequestDTO request) { Post tempPost = postRepository.save(newPost); tempPost.setPostFileList(new ArrayList<>()); - if (request.postFiles() != null) { - for (MultipartFile file : request.postFiles()) { + if (request.getPostFiles() != null) { + for (MultipartFile file : request.getPostFiles()) { if (file.isEmpty()) { continue; } @@ -66,7 +66,7 @@ public Post createPost(PostRequestDTO.CreatePostRequestDTO request) { } // 대표 사진 설정 - String thumbnailImageName = request.thumbnailImageName(); + String thumbnailImageName = request.getThumbnailImageName(); for (PostFile postFile : tempPost.getPostFileList()) { if (postFile.getFileName().equals(thumbnailImageName)) { tempPost.setThumbnailImage(postFile); From 2cd9f9af9fe129e7a34330c91706563a684fd9c6 Mon Sep 17 00:00:00 2001 From: JH Park Date: Sun, 3 Nov 2024 18:23:58 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Feat:=20=EB=8B=A4=EC=9D=B4=EC=96=B4?= =?UTF-8?q?=EB=A6=AC=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=20API=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostController.java | 42 +++++------ .../domain/post/converter/PostConverter.java | 21 ++++++ .../post/dto/request/PostRequestDTO.java | 17 +++++ .../post/dto/response/PostResponseDTO.java | 72 +++++++++---------- .../backend/domain/post/entity/Post.java | 8 +++ .../post/service/PostCommandService.java | 48 +++++++++++++ 6 files changed, 151 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/codiary/backend/domain/post/controller/PostController.java b/src/main/java/com/codiary/backend/domain/post/controller/PostController.java index cd3b5e64..7949d50d 100644 --- a/src/main/java/com/codiary/backend/domain/post/controller/PostController.java +++ b/src/main/java/com/codiary/backend/domain/post/controller/PostController.java @@ -48,27 +48,27 @@ public ApiResponse createPost(@ModelAttribu return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toCreateResultDTO(newPost)); } -// // 멤버의 게시글 수정하기 -// @PatchMapping(path = "/{postId}", consumes = "multipart/form-data") -// @Operation(summary = "다이어리 수정 API", description = "다이어리를 수정합니다.") -// public ApiResponse updatePost(@ModelAttribute PostRequestDTO.UpdatePostDTO request, @PathVariable Long postId){ -// Member member = memberCommandService.getRequester(); -// jwtTokenProvider.isValidToken(member.getMemberId()); -// -// return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toUpdatePostResultDTO(postCommandService.updatePost(postId, request))); -// } - - -// // 게시글 삭제하기 -// @DeleteMapping("/{postId}") -// @Operation(summary = "다이어리 삭제 API", description = "다이어리를 삭제합니다.") -// public ApiResponse deletePost(@PathVariable Long postId){ -// Member member = memberCommandService.getRequester(); -// jwtTokenProvider.isValidToken(member.getMemberId()); -// -// postCommandService.deletePost(postId); -// return ApiResponse.onSuccess(SuccessStatus.POST_OK, null); -// } + // 멤버의 게시글 수정하기 + @PatchMapping(path = "/{postId}", consumes = "multipart/form-data") + @Operation(summary = "다이어리 수정 API", description = "다이어리를 수정합니다.") + public ApiResponse updatePost(@ModelAttribute PostRequestDTO.UpdatePostDTO request, @PathVariable Long postId){ + Member member = memberCommandService.getRequester(); + jwtTokenProvider.isValidToken(member.getMemberId()); + + return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toUpdatePostResultDTO(postCommandService.updatePost(postId, request))); + } + + + // 게시글 삭제하기 + @DeleteMapping("/{postId}") + @Operation(summary = "다이어리 삭제 API", description = "다이어리를 삭제합니다.") + public ApiResponse deletePost(@PathVariable Long postId){ + Member member = memberCommandService.getRequester(); + jwtTokenProvider.isValidToken(member.getMemberId()); + + postCommandService.deletePost(postId); + return ApiResponse.onSuccess(SuccessStatus.POST_OK, null); + } diff --git a/src/main/java/com/codiary/backend/domain/post/converter/PostConverter.java b/src/main/java/com/codiary/backend/domain/post/converter/PostConverter.java index 33d69309..85fb73f8 100644 --- a/src/main/java/com/codiary/backend/domain/post/converter/PostConverter.java +++ b/src/main/java/com/codiary/backend/domain/post/converter/PostConverter.java @@ -72,6 +72,27 @@ public static PostResponseDTO.CreatePostResultDTO toCreateResultDTO(Post post) { } + public static PostResponseDTO.UpdatePostResultDTO toUpdatePostResultDTO(Post post) { + return PostResponseDTO.UpdatePostResultDTO.builder() + .postId(post.getPostId()) + .memberId(post.getMember().getMemberId()) + .teamId(post.getTeam() != null ? post.getTeam().getTeamId() : null) + .projectId(post.getProject() != null ? post.getProject().getProjectId() : null) + .postTitle(post.getPostTitle()) + .postBody(post.getPostBody()) + .postStatus(post.getPostStatus()) + .coauthorIds(post.getAuthorsList().stream() + .map(author -> author.getMember().getMemberId()) + .collect(Collectors.toSet())) + .postAccess(post.getPostAccess()) + .thumbnailImageUrl((post.getThumbnailImage() != null) + ? post.getThumbnailImage().getFileUrl() + : "") + .postFileList(PostFileConverter.toPostFileListDTO(post.getPostFileList())) + .build(); + } + + } diff --git a/src/main/java/com/codiary/backend/domain/post/dto/request/PostRequestDTO.java b/src/main/java/com/codiary/backend/domain/post/dto/request/PostRequestDTO.java index cea1cdf0..9a701211 100644 --- a/src/main/java/com/codiary/backend/domain/post/dto/request/PostRequestDTO.java +++ b/src/main/java/com/codiary/backend/domain/post/dto/request/PostRequestDTO.java @@ -44,4 +44,21 @@ public static class CreatePostRequestDTO { private List postFiles; } + @Getter + //@Setter + @Builder + @AllArgsConstructor + //@NoArgsConstructor + public static class UpdatePostDTO { + private String postTitle; + private String postBody; + private Boolean postStatus; + //private Set postCategory; + private PostAccess postAccess; + private String thumbnailImageName; + private List addedPostFiles; + } + + + } diff --git a/src/main/java/com/codiary/backend/domain/post/dto/response/PostResponseDTO.java b/src/main/java/com/codiary/backend/domain/post/dto/response/PostResponseDTO.java index 937a6f46..954f0155 100644 --- a/src/main/java/com/codiary/backend/domain/post/dto/response/PostResponseDTO.java +++ b/src/main/java/com/codiary/backend/domain/post/dto/response/PostResponseDTO.java @@ -24,42 +24,42 @@ public record SimplePostResponseDTO( ) { } + @Getter + @Builder + @AllArgsConstructor + //@NoArgsConstructor + public static class CreatePostResultDTO { + Long postId; + Long memberId; + Long teamId; + Long projectId; + String postTitle; + String postBody; + String thumbnailImageUrl; + Boolean postStatus; + String postCategory; + Set coauthorIds; + PostAccess postAccess; + PostFileResponseDTO.PostFileListDTO postFileList; + } -// @Builder -// @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) -// @JsonInclude(JsonInclude.Include.NON_NULL) -// public record CreatePostResultDTO( -// Long postId, -// Long memberId, -// Long teamId, -// Long projectId, -// String postTitle, -// String postBody, -// String thumbnailImageUrl, -// Boolean postStatus, -// String postCategory, -// Set coauthorIds, -// PostAccess postAccess, -// PostFileResponseDTO.PostFileListDTO postFileList -// ){ -// } -@Getter -@Builder -@AllArgsConstructor -//@NoArgsConstructor -public static class CreatePostResultDTO { - Long postId; - Long memberId; - Long teamId; - Long projectId; - String postTitle; - String postBody; - String thumbnailImageUrl; - Boolean postStatus; - String postCategory; - Set coauthorIds; - PostAccess postAccess; - PostFileResponseDTO.PostFileListDTO postFileList; -} + @Getter + @Builder + //@NoArgsConstructor + @AllArgsConstructor + public static class UpdatePostResultDTO { + Long postId; + Long memberId; + Long teamId; + Long projectId; + String postTitle; + String postBody; + String thumbnailImageUrl; + Boolean postStatus; + String postCategory; + Set coauthorIds; + PostAccess postAccess; + PostFileResponseDTO.PostFileListDTO postFileList; + } } diff --git a/src/main/java/com/codiary/backend/domain/post/entity/Post.java b/src/main/java/com/codiary/backend/domain/post/entity/Post.java index e3f62a7b..a7086e9f 100644 --- a/src/main/java/com/codiary/backend/domain/post/entity/Post.java +++ b/src/main/java/com/codiary/backend/domain/post/entity/Post.java @@ -2,6 +2,7 @@ import com.codiary.backend.domain.comment.entity.Comment; import com.codiary.backend.domain.member.entity.Member; +import com.codiary.backend.domain.post.dto.request.PostRequestDTO; import com.codiary.backend.global.common.BaseEntity; import com.codiary.backend.domain.project.entity.Project; import com.codiary.backend.domain.team.entity.Team; @@ -86,6 +87,13 @@ public class Post extends BaseEntity { public void setProject(Project project) { this.project = project;} + public void update(PostRequestDTO.UpdatePostDTO request) { + this.postTitle = request.getPostTitle(); + this.postBody = request.getPostBody(); + this.postAccess = request.getPostAccess(); + this.postStatus = request.getPostStatus(); + } + public void setCategories(List categories) { this.categoriesList.clear(); this.categoriesList.addAll(categories); diff --git a/src/main/java/com/codiary/backend/domain/post/service/PostCommandService.java b/src/main/java/com/codiary/backend/domain/post/service/PostCommandService.java index e98636ca..0dd5cc9e 100644 --- a/src/main/java/com/codiary/backend/domain/post/service/PostCommandService.java +++ b/src/main/java/com/codiary/backend/domain/post/service/PostCommandService.java @@ -79,4 +79,52 @@ public Post createPost(PostRequestDTO.CreatePostRequestDTO request) { Post savedPost = postRepository.save(tempPost); return savedPost; } + + + public Post updatePost(Long postId, PostRequestDTO.UpdatePostDTO request) { + Member getMember = memberCommandService.getRequester(); + Post updatePost = postRepository.findById(postId).get(); + updatePost.update(request); + + // 새로운 이미지 추가 + if (request.getAddedPostFiles() != null) { + for (MultipartFile file : request.getAddedPostFiles()) { + if (file.isEmpty()) { + continue; + } + String uuid = UUID.randomUUID().toString(); + Uuid savedUuid = uuidRepository.save(Uuid.builder().uuid(uuid).build()); + String fileUrl = s3Manager.uploadFile(s3Manager.generatePostName(savedUuid), file); + + PostFile newPostFile = PostFileConverter.toPostFile(fileUrl, updatePost, file.getOriginalFilename()); + postFileRepository.save(newPostFile); + + updatePost.getPostFileList().add(newPostFile); + } + } + + // 대표 사진 설정 + String thumbnailImageName = request.getThumbnailImageName(); + for (PostFile postFile : updatePost.getPostFileList()) { + if (postFile.getFileName() == thumbnailImageName) { + updatePost.setThumbnailImage(postFile); + } + } + if (updatePost.getPostFileList().size() != 0 && updatePost.getThumbnailImage() == null) { + updatePost.setThumbnailImage(updatePost.getPostFileList().get(0)); + } + + return postRepository.save(updatePost); + } + + + public void deletePost(Long postId) { + Member getMember = memberCommandService.getRequester(); + + Post deletePost = postRepository.findById(postId).get(); + postRepository.delete(deletePost); + } + + + }