diff --git a/src/main/java/com/example/helloworldmvc/apiPayload/code/status/ErrorStatus.java b/src/main/java/com/example/helloworldmvc/apiPayload/code/status/ErrorStatus.java index 4f1ae09..f217c46 100644 --- a/src/main/java/com/example/helloworldmvc/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/com/example/helloworldmvc/apiPayload/code/status/ErrorStatus.java @@ -45,6 +45,8 @@ LANGUAGE_NOT_MATCHING(HttpStatus.NOT_FOUND, "CENTER4003", "해당 언어의 상담사가 없습니다."), //글 관련 응답 COMMUNITY_NOT_FOUND(HttpStatus.NOT_FOUND,"COMMUNITY4001","글을 찾을 수 없습니다"), + COMMUNITY_NOT_OWNER(HttpStatus.NOT_ACCEPTABLE,"COMMUNITY4002","해당 글의 소유자가 아닙니다."), + //언어 관련 응답 LANGUAGE_NOT_EXIST(HttpStatus.NOT_FOUND, "LANGUAGE4001", "해당 언어가 없습니다."); diff --git a/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java b/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java index 10558e3..8a2fc06 100644 --- a/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java +++ b/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java @@ -109,4 +109,11 @@ public static Long toCategoryId(CommunityCategory category) { } } + public static CommunityResponseDTO.DeletedPostDTO toDeletedPostDTO(Community community){ + return CommunityResponseDTO.DeletedPostDTO.builder() + .categoryName(community.getCommunityCategory().name()) + .post_id(community.getId()) + .build(); + } + } diff --git a/src/main/java/com/example/helloworldmvc/service/CommunityService.java b/src/main/java/com/example/helloworldmvc/service/CommunityService.java index e826aef..d80d94a 100644 --- a/src/main/java/com/example/helloworldmvc/service/CommunityService.java +++ b/src/main/java/com/example/helloworldmvc/service/CommunityService.java @@ -11,4 +11,6 @@ public interface CommunityService { CommunityResponseDTO.CreatedPostDTO createCommunityPost(String userId, Long categoryId, CommunityRequestDTO.CreatePostDTO request); 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); + } diff --git a/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java b/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java index ceb13a4..62924c0 100644 --- a/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java +++ b/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java @@ -24,6 +24,7 @@ import java.util.List; + @Service @RequiredArgsConstructor @Transactional @@ -66,4 +67,15 @@ public CommunityResponseDTO.PostDetailDTO getCommunityDetail(String userId, Long Page commentPage = new PageImpl<>(subList, pageRequest, commentList.size()); return CommunityConverter.toPostDetailDTO(community, commentPage); } + + @Override + public CommunityResponseDTO.DeletedPostDTO deleteCommunityPost(String userId, Long categoryId, Long communityId) { + 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); + } + communityRepository.deleteById(community.getId()); + return CommunityConverter.toDeletedPostDTO(community); + } } 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 5be1127..998e158 100644 --- a/src/main/java/com/example/helloworldmvc/web/controller/CommunityController.java +++ b/src/main/java/com/example/helloworldmvc/web/controller/CommunityController.java @@ -104,4 +104,22 @@ public ApiResponse createComment(@RequestHe } } + + @DeleteMapping(value = "/{category_id}/{community_id}/delete") + @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 = "category_id", description = "PathVariable - 게시글 카테고리 아이디"), + @Parameter(name = "community_id", description = "PathVariable - 게시글 아이디"), + }) + public ApiResponse deleteCommunity(@RequestHeader(name = "Authorization") String accessToken, + @PathVariable(name = "category_id") Long categoryId, + @PathVariable(name = "community_id") Long communityId) { + String gmail = jwtTokenProvider.getGoogleEmail(accessToken); + return ApiResponse.onSuccess(communityService.deleteCommunityPost(gmail, categoryId, communityId)); + } } 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 4002157..f6c68c1 100644 --- a/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java +++ b/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java @@ -61,4 +61,13 @@ public static class CommentDTO{ LocalDateTime created_at; String content; } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class DeletedPostDTO{ + String categoryName; + Long post_id; + } }