Conversation
| public ResponseEntity<ArticleResponse> crateArticle( | ||
| @RequestBody ArticleCreateRequest request | ||
| @Valid @RequestBody ArticleCreateRequest request | ||
| ) { | ||
| if (boardService.getBoards().stream() | ||
| .noneMatch(board -> board.id().equals(request.boardId())) || | ||
| memberService.getAll().stream() | ||
| .noneMatch(member -> member.id().equals(request.authorId()))) | ||
| throw new ExceptionGenerator(StatusEnum.CREATE_NOT_PRESENT_BOARD); | ||
|
|
There was a problem hiding this comment.
예외 처리를 모두 controller에서 하고 있네요!
위와 같은 로직을 service에서 한다면 코드를 재활용 할 수 있지 않을까요?
ArticleController의 crateArticle()과 updateArticle() 에서도 중복되는 부분이 보입니다.
| out/ | ||
| !**/src/main/**/out/ | ||
| !**/src/test/**/out/ | ||
| *.yml |
There was a problem hiding this comment.
적용 중이던 .gitignore에 추가로 작성하면 적용이 안됩니다! 방법을 찾아보세요..!
| @GetMapping("/articles") | ||
| public ResponseEntity<List<ArticleResponse>> getArticles( | ||
| @RequestParam Long boardId | ||
| @RequestParam Long boardId | ||
| ) { | ||
| List<ArticleResponse> response = articleService.getByBoardId(boardId); | ||
|
|
||
| articleValidate.validateResponseIsEmpty(response); | ||
|
|
||
| return ResponseEntity.ok(response); | ||
| } |
There was a problem hiding this comment.
controller에 articleValidate.validateResponseIsEmpty(response);와 같이 검증 메서드를 사용하는 로직이 있네요!
검증해야 할 게 바뀌거나 추가될 때마다 controller에서 수정을 해야 하는 문제가 생길 것 같아요
There was a problem hiding this comment.
그러면 컨트롤러에서는 validateResponseIsEmpty 같은 메소드를 사용하는 것이 아니라 validateCreateArticle, validateUpdateArticle과 같은 메소드를 호출하게 만들고 그 메소드 속에서 validateResponseIsEmpty를 호출하게 만들면 나중에 검증 로직에 수정사항이 있을 때 컨트롤러에서 수정할 일이 없을 것 같습니다! 위와 같은 방식으로 수정해두겠습니다.
| @@ -24,9 +24,9 @@ public class ArticleService { | |||
| private final BoardRepository boardRepository; | |||
|
|
|||
| public ArticleService( | |||
There was a problem hiding this comment.
제가 잘못 이해한걸수도 있지만 예외 처리는 src/main/java/com/example/demo/exception/ExceptHandler.java 에서 처리하도록 만들었습니다.
| return modifiedAt; | ||
| } | ||
|
|
||
| public void setBoardId(Long boardId) { |
8주차 실습 과제 제출합니다.