Skip to content

Commit

Permalink
Merge pull request #70 from KNU-HAEDAL-Website/feat-delete-board-issu…
Browse files Browse the repository at this point in the history
…e-61

Feat: 게시글 존재 시 게시판 삭제 불가
  • Loading branch information
tfer2442 authored Aug 8, 2024
2 parents 1695edc + ec4507e commit 0676ae9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/haedal/haedalweb/constants/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ 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", "해당 게시글 타입이 존재하지 않습니다.");
NOT_FOUND_POST_TYPE(HttpStatus.NOT_FOUND, "018", "해당 게시글 타입이 존재하지 않습니다."),
EXIST_POST(HttpStatus.CONFLICT, "020", "해당 게시판에 게시글이 존재하는 경우 삭제할 수 없습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
boolean existsByBoardId(Long boardId);
}
9 changes: 8 additions & 1 deletion src/main/java/com/haedal/haedalweb/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
public class BoardService {
private final BoardRepository boardRepository;
private final ActivityService activityService;
private final PostService postService;
private final UserService userService;
private final S3Service s3Service;

Expand Down Expand Up @@ -86,8 +87,8 @@ public void deleteBoard(Long activityId, Long boardId) {
User creator = board.getUser();

validateAuthorityOfBoardManagement(loggedInUser, creator);
validateDeleteBoardRequest(boardId);

// 게시글 존재 시 삭제 불가 로직 추가 예정
s3Service.deleteObject(board.getImageUrl());
boardRepository.delete(board);
}
Expand Down Expand Up @@ -183,4 +184,10 @@ private void validateAuthorityOfBoardManagement(User loggedInUser, User creator)
throw new BusinessException(ErrorCode.FORBIDDEN_UPDATE);
}
}

private void validateDeleteBoardRequest(Long boardId) {
if (postService.isBoardPresent(boardId)) {
throw new BusinessException(ErrorCode.EXIST_POST);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/haedal/haedalweb/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class PostService {
private final BoardService boardService;
private final UserService userService;

public boolean isBoardPresent(Long boardId) {
return postRepository.existsByBoardId(boardId);
}

@Transactional
public void createPost(Long boardId, CreatePostDTO createPostDTO) { // createPost 리팩토링 해야함.
Board board = boardService.findBoardById(boardId);
Expand Down

0 comments on commit 0676ae9

Please sign in to comment.