diff --git a/src/main/java/com/haedal/haedalweb/constants/ErrorCode.java b/src/main/java/com/haedal/haedalweb/constants/ErrorCode.java index f68e87a..c5588e3 100644 --- a/src/main/java/com/haedal/haedalweb/constants/ErrorCode.java +++ b/src/main/java/com/haedal/haedalweb/constants/ErrorCode.java @@ -26,8 +26,9 @@ public enum ErrorCode implements ResponseCode{ EXIST_BOARD(HttpStatus.CONFLICT, "017", "해당 활동에 게시판이 존재하는 경우 삭제할 수 없습니다."), NOT_FOUND_BOARD_ID(HttpStatus.NOT_FOUND, "018", "해당 게시판을 찾을 수 없습니다."), FORBIDDEN_UPDATE(HttpStatus.FORBIDDEN, "019", "삭제 권한이 없습니다."), - NOT_FOUND_POST_TYPE(HttpStatus.NOT_FOUND, "018", "해당 게시글 타입이 존재하지 않습니다."), - EXIST_POST(HttpStatus.CONFLICT, "020", "해당 게시판에 게시글이 존재하는 경우 삭제할 수 없습니다."); + NOT_FOUND_POST_TYPE(HttpStatus.NOT_FOUND, "020", "해당 게시글 타입이 존재하지 않습니다."), + EXIST_POST(HttpStatus.CONFLICT, "021", "해당 게시판에 게시글이 존재하는 경우 삭제할 수 없습니다."), + NOT_FOUND_POST_ID(HttpStatus.NOT_FOUND, "022", "해당 게시글을 찾을 수 없습니다."); private final HttpStatus httpStatus; private final String code; diff --git a/src/main/java/com/haedal/haedalweb/constants/SuccessCode.java b/src/main/java/com/haedal/haedalweb/constants/SuccessCode.java index f627d0c..62ecda3 100644 --- a/src/main/java/com/haedal/haedalweb/constants/SuccessCode.java +++ b/src/main/java/com/haedal/haedalweb/constants/SuccessCode.java @@ -27,7 +27,8 @@ public enum SuccessCode implements ResponseCode{ DELETE_BOARD_SUCCESS(HttpStatus.OK, true, "게시판을 삭제했습니다."), UPDATE_BOARD_SUCCESS(HttpStatus.OK, true, "게시판을 수정했습니다."), ADD_POST_SUCCESS(HttpStatus.CREATED, true, "게시글을 생성했습니다."), - OK(HttpStatus.OK, true, "요청을 성공적으로 수행했습니다."); + OK(HttpStatus.OK, true, "요청을 성공적으로 수행했습니다."), + DELETE_POST_SUCCESS(HttpStatus.OK, true, "게시글을 삭제했습니다."); private final HttpStatus httpStatus; diff --git a/src/main/java/com/haedal/haedalweb/controller/PostController.java b/src/main/java/com/haedal/haedalweb/controller/PostController.java index 6ecaeed..a954e60 100644 --- a/src/main/java/com/haedal/haedalweb/controller/PostController.java +++ b/src/main/java/com/haedal/haedalweb/controller/PostController.java @@ -7,15 +7,18 @@ import com.haedal.haedalweb.dto.response.common.SuccessResponse; import com.haedal.haedalweb.service.PostService; import com.haedal.haedalweb.service.S3Service; +import com.haedal.haedalweb.swagger.ApiErrorCodeExample; import com.haedal.haedalweb.swagger.ApiErrorCodeExamples; import com.haedal.haedalweb.swagger.ApiSuccessCodeExample; import com.haedal.haedalweb.util.ResponseUtil; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -66,4 +69,18 @@ public ResponseEntity addEventPost(@RequestBody @Valid CreatePo postService.createPost(createPostDTO); return ResponseUtil.buildSuccessResponseEntity(SuccessCode.ADD_POST_SUCCESS); } + + @Operation(summary = "활동 게시글 삭제") + @ApiSuccessCodeExample(SuccessCode.DELETE_POST_SUCCESS) + @ApiErrorCodeExamples({}) + @Parameters({ + @Parameter(name = "boardId", description = "게시글 삭제할 활동 게시판 ID"), + @Parameter(name = "postId", description = "해당 게시글 ID") + }) + @DeleteMapping("/boards/{boardId}/posts/{postId}") + public ResponseEntity deletePost(@PathVariable Long boardId, @PathVariable Long postId) { + postService.deletePost(boardId, postId); + + return ResponseUtil.buildSuccessResponseEntity(SuccessCode.DELETE_POST_SUCCESS); + } } diff --git a/src/main/java/com/haedal/haedalweb/repository/PostRepository.java b/src/main/java/com/haedal/haedalweb/repository/PostRepository.java index cd0c6fd..9fca2ee 100644 --- a/src/main/java/com/haedal/haedalweb/repository/PostRepository.java +++ b/src/main/java/com/haedal/haedalweb/repository/PostRepository.java @@ -4,7 +4,11 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface PostRepository extends JpaRepository { boolean existsByBoardId(Long boardId); + + Optional findByBoardIdAndId(Long boardId, Long postId); } diff --git a/src/main/java/com/haedal/haedalweb/service/PostService.java b/src/main/java/com/haedal/haedalweb/service/PostService.java index 1e7c4cf..39edcf0 100644 --- a/src/main/java/com/haedal/haedalweb/service/PostService.java +++ b/src/main/java/com/haedal/haedalweb/service/PostService.java @@ -7,6 +7,7 @@ import com.haedal.haedalweb.domain.User; import com.haedal.haedalweb.dto.request.CreatePostDTO; import com.haedal.haedalweb.exception.BusinessException; +import com.haedal.haedalweb.repository.BoardRepository; import com.haedal.haedalweb.repository.PostRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -19,7 +20,7 @@ @Service public class PostService { private final PostRepository postRepository; - private final BoardService boardService; + private final BoardRepository boardRepository; private final UserService userService; public boolean isBoardPresent(Long boardId) { @@ -28,7 +29,9 @@ public boolean isBoardPresent(Long boardId) { @Transactional public void createPost(Long boardId, CreatePostDTO createPostDTO) { // createPost 리팩토링 해야함. - Board board = boardService.findBoardById(boardId); + // Board board = boardService.findBoardById(boardId); + Board board = boardRepository.findById(boardId) + .orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND_BOARD_ID)); PostType postType; try { @@ -86,4 +89,20 @@ public void createPost(CreatePostDTO createPostDTO) { postRepository.save(post); } + + @Transactional + public void deletePost(Long boardId, Long postId) { // 활동 게시글 삭제 method + Post post = postRepository.findByBoardIdAndId(boardId, postId) + .orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND_POST_ID)); + //Board board = boardService.findBoardById(boardId); + + User loggedInUser = userService.getLoggedInUser(); + User postCreator = post.getUser(); + // User boardCreator = board.getUser(); + + //validateAuthorityOfPostManagement + + // 게시판 생성한 것은 팀장이므로, 게시판 생성자와 WEB_MASTER, 해구르르, 게시글 작성자만 삭제 가능 + + } }