오늘 공부한 것
* 기술 면접 대비 롤플레잉
* 어제 Merge한 최종 코드 Postman으로 Test
* 댓글 대댓글 기능에 Pagination 기능 추가
* 마이페이지에 들어갈 댓글 및 대댓글 기능 Pagination 기능 추가
오늘은 기술 면접 대비 롤플레잉을 했다
원래는 하루에 2개씩 1주일에 총 10개의 기술을 공부해서 페어를 이루어 서로 질문과 답변을 하는 것인데
오늘은 시작주여서 6개의 기술을 공부해서 롤플레잉을 진행하였다
현재 팀원분과 페어를 이루었는데 롤플레잉이라고 생각하니 더욱 긴장이 되었다
공부 한것 말고도 꼬리 질문을 많이 받았는데 예상해서 찾아둔 것도 있는 반면
예상하지 못한 꼬리 질문도 있었다
앞으로 공부할 때는 꼬리질문에 대한 내용도 함께 잘 정리해야 겠다
이후에는 어제 Merge한 최종 코드를 Postman으로 Test 했다
처음에 오류가 떠서 왜그러지? 라고 생각했는데
기존에 저장되어 있던 DB와 바뀐 코드간의 충돌로 인한 것이였다
DB를 싹 비우고 다시 실행하니 다행히 문제없이 잘 돌아갔다
그리고는 1차 구현목표를 다 해결했기 때문에 2차 구현 목표를 팀원들과 나누었다
나는 댓글과 대댓글에 들어가는 Pagination 기능을 맡았고 구현을 완료했다
기본적으로 한페이지에 5개의 댓글과 대댓글을 보이도록 만들어 두었고, CreateaAt 기준으로 내침차순 정렬하였다
1. 댓글
1) Controller
아래 코드와 같이 변경하였다
// 댓글 조회
@GetMapping("/posts/{postId}/comments")
public ResponseEntity<Page<CommentsResponseDto>> commentsList(@PathVariable("postId") Long postId,
@RequestParam("page") int page) {
return ResponseEntity.ok(commentsService.commentsList(postId, page-1));
}
// 마이페이지에서 내가 쓴 댓글 조회
@GetMapping("/posts/{postId}/commentsme")
public ResponseEntity<Page<CommentsMeResponseDto>> commentsMeList(@PathVariable("postId") Long postId,
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestParam("page") int page) {
return ResponseEntity.ok(commentsService.commentsMeList(postId, userDetails.getUsers(), page -1));
}
2) Repository
// 추가
Page<Comments> findByPosts_Id(Long postId, Pageable pageable);
Page<Comments> findByPosts_IdAndEmail(Long postId, String email, Pageable pageable);
// 삭제
List<Comments> findByPosts_IdOrderByCreatedAtDesc(Long postId);
3) Service
아래 코드와 같이 변경하였다
// 댓글 조회
public Page<CommentsResponseDto> commentsList(Long postId,
int page) {
int size = 5;
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<Comments> commentsPageList = commentsRepository.findByPosts_Id(postId, pageable);
if (commentsPageList.isEmpty()) {
throw new CustomException(ErrorCode.POST_NOT_EXIST); // 존재하지 않는 게시글입니다
}
Page<CommentsResponseDto> commentsResponseDtoPage = commentsPageList.map(comments -> new CommentsResponseDto(comments, comments.getNickname()));
return commentsResponseDtoPage;
}
// 마이페이지에서 내가 쓴 댓글 조회
public Page<CommentsMeResponseDto> commentsMeList (Long postId,
Users users,
int page) {
int size = 5;
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<Comments> commentsMePageList = commentsRepository.findByPosts_IdAndEmail(postId, users.getEmail(), pageable);
if (commentsMePageList.isEmpty()) {
throw new CustomException(ErrorCode.POST_NOT_EXIST); // 존재하지 않는 게시글입니다
}
Page<CommentsMeResponseDto> CommentsMeResponseDtoPage = commentsMePageList.map(comments -> new CommentsMeResponseDto(comments, comments.getPosts().getTitle(), comments.getNickname()));
return CommentsMeResponseDtoPage;
}
2. 대댓글
1) Controller
아래 코드와 같이 변경하였다
// 마이페이지에서 내가 쓴 대댓글 조회
@GetMapping("/comments/{commentId}/repliesme")
public ResponseEntity<Page<RepliesMeResponseDto>> repliesMeList(@PathVariable("commentId") Long commentId,
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestParam("page") int page) {
return ResponseEntity.ok(repliesService.repliesMeList(commentId, userDetails.getUsers(), page-1));
}
// 대댓글 수정
@PutMapping("replies/{repliesId}")
public ResponseEntity<MessageResponseDto> repliesUpdate( @PathVariable("repliesId") Long repliesId,
@RequestBody RepliesRequestDto request,
@AuthenticationPrincipal UserDetailsImpl userDetails) {
return ResponseEntity.ok(repliesService.repliesUpdate(repliesId, request, userDetails.getUsers()));
}
2) Repository
// 추가
Page<Replies> findByComments_Id(Long postId, Pageable pageable);
Page<Replies> findByComments_IdAndEmail(Long commentId, String email, Pageable pageable);
// 삭제
List<Replies> findByComments_IdOrderByCreatedAtDesc(Long commentId);
3) Service
// 대댓글 조회
public Page<RepliesResponseDto> repliesList(Long commentId,
int page) {
int size = 5;
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<Replies> repliesPageList = repliesRepository.findByComments_Id(commentId, pageable);
if (repliesPageList.isEmpty()) {
throw new CustomException(ErrorCode.COMMENTS_NOT_EXIST); // 존재하지 않는 댓글입니다
}
Page<RepliesResponseDto> repliesResponseDtoPage = repliesPageList.map(replies -> new RepliesResponseDto(replies, replies.getNickname()));
return repliesResponseDtoPage;
}
// 마이페이지에서 내가 쓴 대댓글 조회
public Page<RepliesMeResponseDto> repliesMeList(Long commentId,
Users users,
int page) {
int size = 5;
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<Replies> repliesMePageList = repliesRepository.findByComments_IdAndEmail(commentId, users.getEmail(), pageable);
if (repliesMePageList.isEmpty()) {
throw new CustomException(ErrorCode.COMMENTS_NOT_EXIST); // 존재하지 않는 댓글입니다
}
Page<RepliesMeResponseDto> RepliesMeResponseDtoPage = repliesMePageList.map(replies -> new RepliesMeResponseDto(replies, replies.getComments().getPosts().getTitle(), replies.getNickname()));
return RepliesMeResponseDtoPage;
}
'항해99' 카테고리의 다른 글
23.10.14 항해 99 16기 실전 프로젝트 10일차 (1) | 2023.10.14 |
---|---|
23.10.13 항해 99 16기 실전 프로젝트 9일차 (1) | 2023.10.14 |
23.10.11 항해 99 16기 실전 프로젝트 7일차 (0) | 2023.10.11 |
23.10.10 항해 99 16기 실전 프로젝트 6일차 (0) | 2023.10.10 |
23.10.09 항해 99 16기 실전 프로젝트 5일차 (0) | 2023.10.09 |