diff --git a/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java b/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java index 72b4e87..df233c2 100644 --- a/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java +++ b/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java @@ -116,5 +116,13 @@ public static CommunityResponseDTO.DeletedPostDTO toDeletedPostDTO(Community com .post_id(community.getId()) .build(); } - + public static CommunityResponseDTO.ModifyPostDTO toModifyPostDTO(Community community, Boolean isOwner){ + return CommunityResponseDTO.ModifyPostDTO.builder() + .title(community.getTitle()) + .content(community.getContent()) + .communityWriterEmail(community.getUser().getEmail()) + .created_at(community.getCreatedAt()) + .isOwner(isOwner) + .build(); + } } diff --git a/src/main/java/com/example/helloworldmvc/domain/Community.java b/src/main/java/com/example/helloworldmvc/domain/Community.java index 7965279..b77f75c 100644 --- a/src/main/java/com/example/helloworldmvc/domain/Community.java +++ b/src/main/java/com/example/helloworldmvc/domain/Community.java @@ -49,4 +49,11 @@ public void setUser(User user) { user.getCommunityList().add(this); } + public void setTitle(String title) { + this.title = title; + } + public void setContent(String content) { + this.content = content; + } + } diff --git a/src/main/java/com/example/helloworldmvc/service/CommunityService.java b/src/main/java/com/example/helloworldmvc/service/CommunityService.java index d80d94a..93d14c0 100644 --- a/src/main/java/com/example/helloworldmvc/service/CommunityService.java +++ b/src/main/java/com/example/helloworldmvc/service/CommunityService.java @@ -12,5 +12,6 @@ public interface CommunityService { CommunityResponseDTO.PostListDTO getCommunityList(String userId, Long categoryId, Integer page, Integer size); CommunityResponseDTO.PostDetailDTO getCommunityDetail(String userId, Long communityId, Integer page, Integer size); CommunityResponseDTO.DeletedPostDTO deleteCommunityPost(String userId, Long categoryId, Long communityId); + CommunityResponseDTO.ModifyPostDTO modifyCommunityPost(String userId, Long communityId, CommunityRequestDTO.ModifyPostDTO modifyPostDTO); } diff --git a/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java b/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java index 5eb62d1..44bfe02 100644 --- a/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java +++ b/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java @@ -82,4 +82,25 @@ public CommunityResponseDTO.DeletedPostDTO deleteCommunityPost(String userId, Lo communityRepository.deleteById(community.getId()); return CommunityConverter.toDeletedPostDTO(community); } + + @Override + public CommunityResponseDTO.ModifyPostDTO modifyCommunityPost(String userId, Long communityId, CommunityRequestDTO.ModifyPostDTO modifyPostDTO) { + User user = userRepository.findByEmail(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND)); + Community community = communityRepository.findById(communityId).orElseThrow(() -> new GeneralException(ErrorStatus.COMMUNITY_POST_NOT_FOUND)); + if(!community.getUser().getId().equals(user.getId())){ + throw new GeneralException(ErrorStatus.COMMUNITY_NOT_OWNER); + } + if(!modifyPostDTO.getTitle().isEmpty()){ + community.setTitle(modifyPostDTO.getTitle()); + } + if(!modifyPostDTO.getContent().isEmpty()){ + community.setContent(modifyPostDTO.getContent()); + } + boolean isOwner = false; + if(community.getUser().getEmail().equals(user.getEmail())) { + isOwner = true; + } + communityRepository.save(community); + return CommunityConverter.toModifyPostDTO(community, isOwner); + } } diff --git a/src/main/java/com/example/helloworldmvc/web/controller/CommunityController.java b/src/main/java/com/example/helloworldmvc/web/controller/CommunityController.java index 998e158..83ccb35 100644 --- a/src/main/java/com/example/helloworldmvc/web/controller/CommunityController.java +++ b/src/main/java/com/example/helloworldmvc/web/controller/CommunityController.java @@ -122,4 +122,22 @@ public ApiResponse deleteCommunity(@Request String gmail = jwtTokenProvider.getGoogleEmail(accessToken); return ApiResponse.onSuccess(communityService.deleteCommunityPost(gmail, categoryId, communityId)); } + + @PatchMapping(value = "/{category_id}/{community_id}/modify") + @Operation(summary = "커뮤니티 글 수정 API", description = "커뮤니티 게시판에 글을 수정하는 API입니다.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER4001", description = "사용자를 찾을수 없습니다.") + }) + @Parameters({ + @Parameter(name = "Authorization", description = "RequestHeader - 로그인한 사용자 토큰"), + @Parameter(name = "community_id", description = "PathVariable - 게시글 아이디"), + }) + public ApiResponse modifyCommunity(@RequestHeader(name = "Authorization") String accessToken, + @PathVariable(name = "community_id") Long communityId, + @RequestBody @Valid CommunityRequestDTO.ModifyPostDTO modifyPostDTO + ) { + String gmail = jwtTokenProvider.getGoogleEmail(accessToken); + return ApiResponse.onSuccess(communityService.modifyCommunityPost(gmail, communityId, modifyPostDTO)); + } } diff --git a/src/main/java/com/example/helloworldmvc/web/dto/CommunityRequestDTO.java b/src/main/java/com/example/helloworldmvc/web/dto/CommunityRequestDTO.java index 99c6a11..9759727 100644 --- a/src/main/java/com/example/helloworldmvc/web/dto/CommunityRequestDTO.java +++ b/src/main/java/com/example/helloworldmvc/web/dto/CommunityRequestDTO.java @@ -20,4 +20,14 @@ public static class CreatePostDTO { List images; } + + @Getter + @Setter + public static class ModifyPostDTO { + @NotBlank + String title; + + @NotBlank + String content; + } } diff --git a/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java b/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java index 9016524..0befe5a 100644 --- a/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java +++ b/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java @@ -71,4 +71,16 @@ public static class DeletedPostDTO{ String categoryName; Long post_id; } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class ModifyPostDTO{ + String title; + String content; + String communityWriterEmail; + LocalDateTime created_at; + Boolean isOwner; + } }