Skip to content

Commit

Permalink
Merge pull request #71 from KNU-HAEDAL-Website/feat-delete-post-issue-69
Browse files Browse the repository at this point in the history
Fix: 순환Service 순환 참조 문제
  • Loading branch information
tfer2442 authored Aug 9, 2024
2 parents 0676ae9 + b3bfe89 commit 84ccad2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/haedal/haedalweb/constants/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/haedal/haedalweb/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,4 +69,18 @@ public ResponseEntity<SuccessResponse> 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<SuccessResponse> deletePost(@PathVariable Long boardId, @PathVariable Long postId) {
postService.deletePost(boardId, postId);

return ResponseUtil.buildSuccessResponseEntity(SuccessCode.DELETE_POST_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post, Long> {
boolean existsByBoardId(Long boardId);

Optional<Post> findByBoardIdAndId(Long boardId, Long postId);
}
23 changes: 21 additions & 2 deletions src/main/java/com/haedal/haedalweb/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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, 해구르르, 게시글 작성자만 삭제 가능

}
}

0 comments on commit 84ccad2

Please sign in to comment.