-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: Board controller, service 계층 리팩토링/테스트 코드 추가 #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| import com.Alchive.backend.config.error.exception.board.NotFoundBoardException; | ||||||||||||||||||||||||||||||||||||||||||||||
| import com.Alchive.backend.config.error.exception.problem.NotFoundProblemException; | ||||||||||||||||||||||||||||||||||||||||||||||
| import com.Alchive.backend.config.result.ResultCode; | ||||||||||||||||||||||||||||||||||||||||||||||
| import com.Alchive.backend.domain.algorithm.Algorithm; | ||||||||||||||||||||||||||||||||||||||||||||||
| import com.Alchive.backend.domain.algorithmProblem.AlgorithmProblem; | ||||||||||||||||||||||||||||||||||||||||||||||
| import com.Alchive.backend.domain.board.Board; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -37,62 +38,44 @@ public class BoardService { | |||||||||||||||||||||||||||||||||||||||||||||
| private final SolutionRepository solutionRepository; | ||||||||||||||||||||||||||||||||||||||||||||||
| private final UserService userService; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| private BoardDetailResponseDTO toBoardDetailResponseDTO(Board board) { | ||||||||||||||||||||||||||||||||||||||||||||||
| BoardResponseDTO boardResponseDTO = new BoardResponseDTO(board); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // 문제 정보 | ||||||||||||||||||||||||||||||||||||||||||||||
| Long problemId = board.getProblem().getId(); | ||||||||||||||||||||||||||||||||||||||||||||||
| Problem problem = problemRepository.findById(problemId) | ||||||||||||||||||||||||||||||||||||||||||||||
| .orElseThrow(NotFoundProblemException::new); | ||||||||||||||||||||||||||||||||||||||||||||||
| ProblemResponseDTO problemResponseDTO = new ProblemResponseDTO(problem, getProblemAlgorithms(problemId)); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // 풀이 정보 | ||||||||||||||||||||||||||||||||||||||||||||||
| List<SolutionResponseDTO> solutions = getSolutions(board.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // DTO로 묶어서 반환 | ||||||||||||||||||||||||||||||||||||||||||||||
| return new BoardDetailResponseDTO(boardResponseDTO, problemResponseDTO, solutions); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Board 저장 여부 구현 | ||||||||||||||||||||||||||||||||||||||||||||||
| public BoardDetailResponseDTO isBoardSaved(User user, ProblemNumberRequest problemNumberRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||
| Optional<Board> board = boardRepository.findByProblem_PlatformAndProblem_NumberAndUser_Id(problemNumberRequest.getPlatform(), problemNumberRequest.getProblemNumber(), user.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||
| return board.map(this::toBoardDetailResponseDTO).orElse(null); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public ResultCode boardSavedStatus(BoardDetailResponseDTO board) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (board != null) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return ResultCode.BOARD_ALREADY_EXSIST; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return ResultCode.BOARD_NOT_EXIST; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public Page<List<BoardDetailResponseDTO>> getBoardList(int offset, int limit) { | ||||||||||||||||||||||||||||||||||||||||||||||
| Pageable pageable = PageRequest.of(offset, limit); | ||||||||||||||||||||||||||||||||||||||||||||||
| Page<Board> boardPage = boardRepository.findAll(pageable); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Board를 BoardDetailResponseDTO로 변환 | ||||||||||||||||||||||||||||||||||||||||||||||
| List<BoardDetailResponseDTO> boardList = boardPage.getContent().stream() | ||||||||||||||||||||||||||||||||||||||||||||||
| .map(this::toBoardDetailResponseDTO) | ||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // 변환된 리스트를 새로운 Page 객체로 감싸서 반환 | ||||||||||||||||||||||||||||||||||||||||||||||
| return new PageImpl<>(List.of(boardList), pageable, boardPage.getTotalElements()); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
54
to
63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion
현재 구현은 아래와 같이 제너릭 타입을 단일 DTO 로 변경하고 불필요한 - public Page<List<BoardDetailResponseDTO>> getBoardList(int offset, int limit) {
+ public Page<BoardDetailResponseDTO> getBoardList(int offset, int limit) {
...
- return new PageImpl<>(List.of(boardList), pageable, boardPage.getTotalElements());
+ return new PageImpl<>(boardList, pageable, boardPage.getTotalElements());
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // 게시물 메서드 | ||||||||||||||||||||||||||||||||||||||||||||||
| public BoardDetailResponseDTO getBoardDetail(Long boardId) { | ||||||||||||||||||||||||||||||||||||||||||||||
| Board board = boardRepository.findById(boardId) | ||||||||||||||||||||||||||||||||||||||||||||||
| .orElseThrow(NotFoundBoardException::new); | ||||||||||||||||||||||||||||||||||||||||||||||
| return toBoardDetailResponseDTO(board); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||
| public BoardResponseDTO createBoard(User user, BoardCreateRequest boardCreateRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||
| ProblemCreateRequest problemCreateRequest = boardCreateRequest.getProblemCreateRequest(); | ||||||||||||||||||||||||||||||||||||||||||||||
| // 문제 정보 저장 여부 확인 | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!problemRepository.existsByNumberAndPlatform(problemCreateRequest.getNumber(), problemCreateRequest.getPlatform())) { | ||||||||||||||||||||||||||||||||||||||||||||||
| // 문제가 저장되지 않은 경우 | ||||||||||||||||||||||||||||||||||||||||||||||
| createProblem(problemCreateRequest); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| createProblem(problemCreateRequest); | ||||||||||||||||||||||||||||||||||||||||||||||
| Problem problem = problemRepository.findByNumberAndPlatform(problemCreateRequest.getNumber(), problemCreateRequest.getPlatform()); | ||||||||||||||||||||||||||||||||||||||||||||||
| Board board = Board.of(problem, user, boardCreateRequest); | ||||||||||||||||||||||||||||||||||||||||||||||
| return new BoardResponseDTO(boardRepository.save(board)); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public BoardDetailResponseDTO getBoardDetail(Long boardId) { | ||||||||||||||||||||||||||||||||||||||||||||||
| // 게시물 정보 | ||||||||||||||||||||||||||||||||||||||||||||||
| Board board = boardRepository.findById(boardId) | ||||||||||||||||||||||||||||||||||||||||||||||
| .orElseThrow(NotFoundBoardException::new); | ||||||||||||||||||||||||||||||||||||||||||||||
| return toBoardDetailResponseDTO(board); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||
| public BoardResponseDTO updateBoard(User user, Long boardId, BoardUpdateRequest updateRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||
| Board board = boardRepository.findById(boardId) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -102,55 +85,75 @@ public BoardResponseDTO updateBoard(User user, Long boardId, BoardUpdateRequest | |||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||
| public void deleteBoard(User user, Long boardId) { | ||||||||||||||||||||||||||||||||||||||||||||||
| public BoardResponseDTO updateBoardMemo(User user, Long boardId, BoardMemoUpdateRequest updateRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||
| Board board = boardRepository.findById(boardId) | ||||||||||||||||||||||||||||||||||||||||||||||
| .orElseThrow(NotFoundBoardException::new); | ||||||||||||||||||||||||||||||||||||||||||||||
| userService.validateUser(user.getId(), board.getUser().getId()); | ||||||||||||||||||||||||||||||||||||||||||||||
| boardRepository.delete(board); | ||||||||||||||||||||||||||||||||||||||||||||||
| return new BoardResponseDTO(board.updateMemo(updateRequest.getMemo())); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||
| public BoardResponseDTO updateBoardMemo(User user, Long boardId, BoardMemoUpdateRequest updateRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||
| public void deleteBoard(User user, Long boardId) { | ||||||||||||||||||||||||||||||||||||||||||||||
| Board board = boardRepository.findById(boardId) | ||||||||||||||||||||||||||||||||||||||||||||||
| .orElseThrow(NotFoundBoardException::new); | ||||||||||||||||||||||||||||||||||||||||||||||
| userService.validateUser(user.getId(), board.getUser().getId()); | ||||||||||||||||||||||||||||||||||||||||||||||
| return new BoardResponseDTO(board.updateMemo(updateRequest.getMemo())); | ||||||||||||||||||||||||||||||||||||||||||||||
| boardRepository.delete(board); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| private BoardDetailResponseDTO toBoardDetailResponseDTO(Board board) { | ||||||||||||||||||||||||||||||||||||||||||||||
| BoardResponseDTO boardResponseDTO = new BoardResponseDTO(board); | ||||||||||||||||||||||||||||||||||||||||||||||
| ProblemResponseDTO problemResponseDTO = getBoardProblem(board); | ||||||||||||||||||||||||||||||||||||||||||||||
| List<SolutionResponseDTO> solutions = getBoardSolutions(board.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return new BoardDetailResponseDTO(boardResponseDTO, problemResponseDTO, solutions); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| private ProblemResponseDTO getBoardProblem(Board board) { | ||||||||||||||||||||||||||||||||||||||||||||||
| Long problemId = board.getProblem().getId(); | ||||||||||||||||||||||||||||||||||||||||||||||
| Problem problem = problemRepository.findById(problemId) | ||||||||||||||||||||||||||||||||||||||||||||||
| .orElseThrow(NotFoundProblemException::new); | ||||||||||||||||||||||||||||||||||||||||||||||
| return new ProblemResponseDTO(problem, getProblemAlgorithms(problemId)); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public List<SolutionResponseDTO> getBoardSolutions(Long boardId) { | ||||||||||||||||||||||||||||||||||||||||||||||
| List<Solution> solutions = solutionRepository.findAllByBoard_Id(boardId); | ||||||||||||||||||||||||||||||||||||||||||||||
| return solutions.stream() | ||||||||||||||||||||||||||||||||||||||||||||||
| .map(SolutionResponseDTO::new) | ||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public List<String> getProblemAlgorithms(Long problemId) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return algorithmProblemRepository.findAlgorithmNamesByProblemId(problemId); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // 문제 메서드 | ||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||
| public void createProblem(ProblemCreateRequest problemCreateRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (problemRepository.existsByNumberAndPlatform(problemCreateRequest.getNumber(), problemCreateRequest.getPlatform())) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| Problem problem = problemRepository.save(Problem.of(problemCreateRequest)); | ||||||||||||||||||||||||||||||||||||||||||||||
| List<String> algorithms = problemCreateRequest.getAlgorithms(); | ||||||||||||||||||||||||||||||||||||||||||||||
| connectProblemAlgorithm(problem, problemCreateRequest.getAlgorithms()); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| private void connectProblemAlgorithm(Problem problem, List<String> algorithms) { | ||||||||||||||||||||||||||||||||||||||||||||||
| for (String algorithmName : algorithms) { | ||||||||||||||||||||||||||||||||||||||||||||||
| // 알고리즘 저장 여부 확인 | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!algorithmRepository.existsByName(algorithmName)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| // 알고리즘이 저장되지 않은 경우 | ||||||||||||||||||||||||||||||||||||||||||||||
| createAlgorithm(algorithmName); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| Algorithm algorithm = algorithmRepository.findByName(algorithmName); | ||||||||||||||||||||||||||||||||||||||||||||||
| // Algorithm - Problem 연결 | ||||||||||||||||||||||||||||||||||||||||||||||
| AlgorithmProblem algorithmProblem = AlgorithmProblem.of(algorithm, problem); | ||||||||||||||||||||||||||||||||||||||||||||||
| algorithmProblemRepository.save(algorithmProblem); | ||||||||||||||||||||||||||||||||||||||||||||||
| createAlgorithm(algorithmName); | ||||||||||||||||||||||||||||||||||||||||||||||
| createAlgorithmProblem(algorithmName, problem); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public List<String> getProblemAlgorithms(Long problemId) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return algorithmProblemRepository.findAlgorithmNamesByProblemId(problemId); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // 알고리즘 메서드 | ||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||
| public void createAlgorithm(String name) { | ||||||||||||||||||||||||||||||||||||||||||||||
| Algorithm newAlgorithm = Algorithm.of(name); | ||||||||||||||||||||||||||||||||||||||||||||||
| public void createAlgorithm(String algorithmName) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (algorithmRepository.existsByName(algorithmName)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| Algorithm newAlgorithm = Algorithm.of(algorithmName); | ||||||||||||||||||||||||||||||||||||||||||||||
| algorithmRepository.save(newAlgorithm); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| public void createAlgorithm(String algorithmName) { | |
| if (algorithmRepository.existsByName(algorithmName)) { | |
| return; | |
| } | |
| Algorithm newAlgorithm = Algorithm.of(algorithmName); | |
| algorithmRepository.save(newAlgorithm); | |
| public void createAlgorithm(String algorithmName) { | |
| try { | |
| algorithmRepository.save(Algorithm.of(algorithmName)); | |
| } catch (DataIntegrityViolationException e) { | |
| // UNIQUE 제약 위반 → 이미 존재하므로 무시 | |
| } | |
| } |
🤖 Prompt for AI Agents
In src/main/java/com/Alchive/backend/service/BoardService.java around lines 146
to 151, the createAlgorithm method risks duplicate inserts due to a race
condition between existsByName check and save. To fix this, add a UNIQUE
constraint on the algorithm name column at the database level, then remove the
existsByName check and instead attempt to save directly. Catch any exceptions
caused by unique constraint violations and ignore them to prevent duplicates and
reduce I/O operations.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package com.Alchive.backend.controller; | ||
|
|
||
| import com.Alchive.backend.config.result.ResultResponse; | ||
| import com.Alchive.backend.domain.board.Board; | ||
| import com.Alchive.backend.domain.board.BoardStatus; | ||
| import com.Alchive.backend.domain.problem.Problem; | ||
| import com.Alchive.backend.domain.problem.ProblemDifficulty; | ||
| import com.Alchive.backend.domain.problem.ProblemPlatform; | ||
| import com.Alchive.backend.domain.user.User; | ||
| import com.Alchive.backend.dto.request.ProblemNumberRequest; | ||
| import com.Alchive.backend.dto.response.BoardDetailResponseDTO; | ||
| import com.Alchive.backend.dto.response.BoardResponseDTO; | ||
| import com.Alchive.backend.dto.response.ProblemResponseDTO; | ||
| import com.Alchive.backend.service.BoardService; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.http.ResponseEntity; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static com.Alchive.backend.config.result.ResultCode.*; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.nullable; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| public class BoardControllerTest { | ||
| @InjectMocks | ||
| private BoardController sut; | ||
| @Mock | ||
| private BoardService boardService; | ||
| private Board board; | ||
| private Problem problem; | ||
| private User user; | ||
| private List<String> algorithms = Arrays.asList("BFS", "DFS"); | ||
|
|
||
| private long problemId = 1L; | ||
| private int problemNumber = 1; | ||
| private String problemTitle = "testProblem"; | ||
| private String problemContent = "this is test problem's content"; | ||
| private String problemUrl = "https://test/problem/1"; | ||
| private ProblemDifficulty problemDifficulty = ProblemDifficulty.LEVEL0; | ||
| private ProblemPlatform problemPlatform = ProblemPlatform.BAEKJOON; | ||
| private long userId = 1L; | ||
| private String userEmail = "[email protected]"; | ||
| private String userName = "testUsername"; | ||
| private long boardId = 1L; | ||
| private String boardMemo = "test problem's memo"; | ||
| private BoardStatus boardStatus = BoardStatus.CORRECT; | ||
| private String boardDescription = "test problem's description"; | ||
| @BeforeEach | ||
|
|
||
| public void setUp() { | ||
| problem = new Problem(problemId, problemNumber, problemTitle, problemContent, problemUrl, problemDifficulty, problemPlatform); | ||
| user = new User(userId, userEmail, userName); | ||
| board = new Board(boardId, problem, user, boardMemo, boardStatus, boardDescription); | ||
| } | ||
|
|
||
| @DisplayName("게시물 저장 여부 조회 - 이미 존재하는 문제") | ||
| @Test | ||
| public void existBoard() { | ||
| BoardDetailResponseDTO response = new BoardDetailResponseDTO(new BoardResponseDTO(board), new ProblemResponseDTO(problem, algorithms), new ArrayList<>()); | ||
| when(boardService.isBoardSaved(any(User.class), any(ProblemNumberRequest.class))).thenReturn(response); | ||
| when(boardService.boardSavedStatus(response)).thenReturn(BOARD_ALREADY_EXSIST); | ||
|
|
||
| ResponseEntity<ResultResponse> result = sut.isBoardSaved(user, new ProblemNumberRequest(problemPlatform, problemNumber)); | ||
|
|
||
| Assertions.assertEquals(BOARD_ALREADY_EXSIST.getMessage(), result.getBody().getMessage()); | ||
| } | ||
|
|
||
| @DisplayName("게시물 저장 여부 조회 - 존재하지 않는 문제") | ||
| @Test | ||
| public void newBoard() { | ||
| when(boardService.isBoardSaved(any(User.class), any(ProblemNumberRequest.class))).thenReturn(null); | ||
| when(boardService.boardSavedStatus(nullable(BoardDetailResponseDTO.class))).thenReturn(BOARD_NOT_EXIST); | ||
|
|
||
| ResponseEntity<ResultResponse> result = sut.isBoardSaved(user, new ProblemNumberRequest(problemPlatform, problemNumber)); | ||
|
|
||
| Assertions.assertEquals(BOARD_NOT_EXIST.getMessage(), result.getBody().getMessage()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
boardSavedStatus역할 중복 및 오타 가능성isBoardSaved➡null/BoardDetailResponseDTO로 존재 여부를 판단하고 있는데,boardSavedStatus가 추가로 한 번 더 판단하도록 설계돼 중복 로직이 발생했습니다. 두 메서드 중 하나만으로도 충분하므로 응답 DTO 또는ResultCode둘 중 하나로 통일하는 편이 깔끔합니다.ResultCode.BOARD_ALREADY_EXSIST→ “EXIST” 철자 오타 가능성이 있습니다. 실제 enum 정의를 한 번 더 확인해 주세요(테스트 코드에서 찾기 어려움).다음 스크립트를 실행하여
isBoardSaved메서드 로직 중복 여부와ResultCodeenum 상수 철자(BOARD_ALREADY_EXSIST vs BOARD_ALREADY_EXIST)를 확인해 주세요.🏁 Script executed:
Length of output: 1291
boardSavedStatus 메서드 로직 중복 제거 및 상수 철자 수정 필요
isBoardSaved()와boardSavedStatus()가 게시물 존재 여부를 각각 판단하면서 중복 로직이 발생하고,ResultCodeenum에BOARD_ALREADY_EXSIST오타가 있어 아래와 같이 리팩토링을 권장드립니다.대상 파일 및 수정 포인트:
•
BOARD_ALREADY_EXSIST→BOARD_ALREADY_EXIST로 상수명 수정•
isBoardSaved+boardSavedStatus통합 → 게시물 조회 후 곧바로ResultCode반환• 테스트 내
BOARD_ALREADY_EXSIST→BOARD_ALREADY_EXIST로 수정예시 diff:
컨트롤러 호출부 예시:
테스트에서도 동일하게 상수명 변경을 적용해 주세요.
📝 Committable suggestion
🤖 Prompt for AI Agents