-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Main
commentService를 MockBean으로 주입하여 사용하는 테스트 코드를 작성했는데,
기존 코드는 각종 서비스 및 JPA 의존성이 복잡하게 얽혀있어 결합도를 떨어트려야 할 필요가 생겼고
응답을 제공하는데 필요없는 쿼리 또한 제거하여 서버의 트래픽 부담을 감소시키려고 합니다.
현재 비슷하게 작성된 컨트롤러, 서비스들도 추후 수정한 방침대로 리팩토링 할 예정..
기존 댓글 작성 컨트롤러
@ApiOperation(value = "comment", notes = "[댓글] 회고글에 댓글 작성")
@PostMapping("")
public ResponseEntity<Object> inputComments(HttpServletRequest request,
@RequestBody CommentDto.InputRequest inputRequest) {
Long userIdx = tokenService.getUserIdx(tokenService.getTokenFromRequest(request));
return new ResponseEntity<>(ApiDefaultResponse.res(201, ResponseMessage.COMMENT_SAVE.getResponseMessage(),
commentService.inputComments(inputRequest, userIdx)), HttpStatus.CREATED);
}수정한 댓글 작성 컨트롤러
@ApiOperation(value = "comment", notes = "[댓글] 회고글에 댓글 작성")
@PostMapping("")
// 시큐리티에서 제공하는 @AuthenticationPrincipal을 이용하여 인증된 사용자 정보를 가져오는 @CurrentUser 애노테이션
public ResponseEntity<Object> inputComments(@CurrentUser User user,
@RequestBody CommentDto.InputRequest inputRequest) {
//쿼리를 통해 post 엔티티를 가져오는 대신, RequestDto에 들어있는 postIdx로 엔티티를 생성해준다
//dtoToEntity mapstruct 활용할 것
Comment newComment = commentMapper.toEntity(inputRequest, user, Post.builder().postIdx(inputRequest.getPostIdx()).build());
//기존 return new ResponseEntity<> 대신 빌더 패턴을 사용하는게 가독성에 더 좋아보임
return ResponseEntity.status(HttpStatus.CREATED).body(
ApiDefaultResponse.res(201, ResponseMessage.COMMENT_SAVE.getResponseMessage(),
commentService.inputComments(newComment))
);
}수정한 commentService.inputComments
@Transactional
public Comment inputComments(Comment comment){
//타 서비스 의존성을 제거와 코드 가독성을 위해 Entity 외의 메서드 parameter는 최소화 할 것
return commentRepository.save(comment);
}etc.
현재 헤더가 비어있는 상태로 권한이 필요한 API로 요청하면 IllegalArgumentException으로 응답하게 되어있는데
MockBean으로 주입한 서비스도 시큐리티 필터에 걸려서ㅠㅠ
이건 따로 검사하지않고 시큐리티에서 처리하도록 넘기도록 수정할 예정입니다.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels