From 08bfcb8cf29b6c3bda8558a3bab0904b84bc337e Mon Sep 17 00:00:00 2001 From: yongckim Date: Thu, 11 Aug 2022 01:47:30 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20feat=20:=20=EB=8C=93=EA=B8=80?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EC=A0=81=EC=9A=A9=20=EB=B0=8F=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 댓글 조회시 페이지네이션 적용 (기본 사이즈 10) 댓글 검증 로직 추가 (Post 존재 여부 및 해당 유저가 스터디를 가입하고 있는지 확인) 댓글 이외의 부분의 검증로직에 대해서 책임을 넘기기 위해 Facade 패턴 적용 커스텀 PageRequest VO가 두개 존재해 합치는 작업 완료 --- .../checkmoi/domain/book/api/BookApi.java | 9 +- .../checkmoi/domain/book/dto/SimplePage.java | 42 ------- .../domain/book/service/BookQueryService.java | 5 +- .../book/service/BookQueryServiceImpl.java | 19 +-- .../domain/comment/api/CommentApi.java | 18 +-- .../domain/comment/dto/CommentResponse.java | 15 ++- .../comment/facade/CommentQueryFacade.java | 10 ++ .../facade/CommentQueryFacadeImpl.java | 28 +++++ .../repository/CustomCommentRepository.java | 5 +- .../CustomCommentRepositoryImpl.java | 24 ++-- .../comment/service/CommentQueryService.java | 4 +- .../service/CommentQueryServiceImpl.java | 13 ++- .../checkmoi/domain/study/api/StudyApi.java | 8 +- .../study/service/StudyQueryService.java | 4 +- .../{PageRequest.java => SimplePage.java} | 29 +++-- .../domain/book/dto/SimplePageTest.java | 7 +- .../service/BookQueryServiceImplTest.java | 8 +- .../domain/comment/api/CommentApiTest.java | 39 ++++--- .../facade/CommentQueryFacadeImplTest.java | 69 +++++++++++ .../repository/CommentRepositoryTest.java | 110 ++++++++++++++++++ .../service/CommentQueryServiceImplTest.java | 23 ++-- .../domain/study/api/StudyApiTest.java | 6 +- .../study/repository/StudyRepositoryTest.java | 16 +-- .../service/StudyQueryServiceImplTest.java | 6 +- .../checkmoi/util/DTOGeneratorUtil.java | 18 +++ 25 files changed, 392 insertions(+), 143 deletions(-) delete mode 100644 src/main/java/com/devcourse/checkmoi/domain/book/dto/SimplePage.java create mode 100644 src/main/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacade.java create mode 100644 src/main/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImpl.java rename src/main/java/com/devcourse/checkmoi/global/model/{PageRequest.java => SimplePage.java} (52%) create mode 100644 src/test/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImplTest.java create mode 100644 src/test/java/com/devcourse/checkmoi/domain/comment/repository/CommentRepositoryTest.java diff --git a/src/main/java/com/devcourse/checkmoi/domain/book/api/BookApi.java b/src/main/java/com/devcourse/checkmoi/domain/book/api/BookApi.java index fe1402fc..775dfe14 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/book/api/BookApi.java +++ b/src/main/java/com/devcourse/checkmoi/domain/book/api/BookApi.java @@ -4,7 +4,7 @@ import com.devcourse.checkmoi.domain.book.dto.BookRequest.CreateBook; import com.devcourse.checkmoi.domain.book.dto.BookResponse.BookSpecification; import com.devcourse.checkmoi.domain.book.dto.BookResponse.LatestAllBooks; -import com.devcourse.checkmoi.domain.book.dto.SimplePage; +import com.devcourse.checkmoi.global.model.SimplePage; import com.devcourse.checkmoi.domain.book.service.BookCommandService; import com.devcourse.checkmoi.domain.book.service.BookQueryService; import com.devcourse.checkmoi.global.model.SuccessResponse; @@ -42,8 +42,11 @@ public ResponseEntity> register( } @GetMapping - public ResponseEntity> topBooks() { - LatestAllBooks topBooks = bookQueryService.getAllTop(SimplePage.defaultPage()); + public ResponseEntity> topBooks( + SimplePage pageRequest + ) { + pageRequest.setSize(4); + LatestAllBooks topBooks = bookQueryService.getAllTop(pageRequest.of()); return ResponseEntity.ok( new SuccessResponse<>(topBooks) diff --git a/src/main/java/com/devcourse/checkmoi/domain/book/dto/SimplePage.java b/src/main/java/com/devcourse/checkmoi/domain/book/dto/SimplePage.java deleted file mode 100644 index 605605d3..00000000 --- a/src/main/java/com/devcourse/checkmoi/domain/book/dto/SimplePage.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.devcourse.checkmoi.domain.book.dto; - - -public class SimplePage { - - private static final int LOWER_LIMIT_VALID_PAGE_SIZE = 1; - - private static final int LOWER_LIMIT_VALID_PAGE = 0; - - private static final int DEFAULT_PAGE = 0; - - private static final int DEFAULT_PAGE_SIZE = 4; - - private final Integer page; - - private final Integer size; - - public SimplePage(Integer page, Integer size) { - this.page = isInvalidPage(page) ? DEFAULT_PAGE : page; - this.size = isInvalidSize(size) ? DEFAULT_PAGE_SIZE : size; - } - - public static SimplePage defaultPage() { - return new SimplePage(DEFAULT_PAGE, DEFAULT_PAGE_SIZE); - } - - public int getPage() { - return page; - } - - public int getSize() { - return size; - } - - private boolean isInvalidPage(Integer page) { - return page == null || page < LOWER_LIMIT_VALID_PAGE; - } - - private boolean isInvalidSize(Integer size) { - return size == null || size < LOWER_LIMIT_VALID_PAGE_SIZE; - } -} diff --git a/src/main/java/com/devcourse/checkmoi/domain/book/service/BookQueryService.java b/src/main/java/com/devcourse/checkmoi/domain/book/service/BookQueryService.java index dd0aa0be..8a71a94c 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/book/service/BookQueryService.java +++ b/src/main/java/com/devcourse/checkmoi/domain/book/service/BookQueryService.java @@ -2,11 +2,12 @@ import com.devcourse.checkmoi.domain.book.dto.BookResponse.BookSpecification; import com.devcourse.checkmoi.domain.book.dto.BookResponse.LatestAllBooks; -import com.devcourse.checkmoi.domain.book.dto.SimplePage; +import com.devcourse.checkmoi.global.model.SimplePage; +import org.springframework.data.domain.Pageable; public interface BookQueryService { - LatestAllBooks getAllTop(SimplePage pageRequest); + LatestAllBooks getAllTop(Pageable pageRequest); BookSpecification getById(Long bookId); diff --git a/src/main/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImpl.java b/src/main/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImpl.java index 9f80ab4c..0a1308b1 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImpl.java +++ b/src/main/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImpl.java @@ -3,10 +3,11 @@ import com.devcourse.checkmoi.domain.book.converter.BookConverter; import com.devcourse.checkmoi.domain.book.dto.BookResponse.BookSpecification; import com.devcourse.checkmoi.domain.book.dto.BookResponse.LatestAllBooks; -import com.devcourse.checkmoi.domain.book.dto.SimplePage; +import com.devcourse.checkmoi.global.model.SimplePage; import com.devcourse.checkmoi.domain.book.exception.BookNotFoundException; import com.devcourse.checkmoi.domain.book.repository.BookRepository; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.stereotype.Service; @@ -27,22 +28,12 @@ public BookQueryServiceImpl(BookRepository bookRepository, } @Override - public LatestAllBooks getAllTop(SimplePage pageRequest) { - PageRequest registerLatest = - PageRequest.of( - pageRequest.getPage(), - pageRequest.getSize(), - Sort.by(Direction.DESC, "createdAt")); - PageRequest studyLatest = - PageRequest.of( - pageRequest.getPage(), - pageRequest.getSize() - ); + public LatestAllBooks getAllTop(Pageable pageRequest) { return new LatestAllBooks( - bookRepository.findAllTop(registerLatest).stream() + bookRepository.findAllTop(pageRequest).stream() .map(bookConverter::bookToSimple) .toList(), - bookRepository.findBooksByLatestStudy(studyLatest).stream() + bookRepository.findBooksByLatestStudy(pageRequest).stream() .map(bookConverter::bookToSimple) .toList() ); diff --git a/src/main/java/com/devcourse/checkmoi/domain/comment/api/CommentApi.java b/src/main/java/com/devcourse/checkmoi/domain/comment/api/CommentApi.java index 79a526e6..084e648f 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/comment/api/CommentApi.java +++ b/src/main/java/com/devcourse/checkmoi/domain/comment/api/CommentApi.java @@ -1,16 +1,17 @@ package com.devcourse.checkmoi.domain.comment.api; import static com.devcourse.checkmoi.global.util.ApiUtil.generatedUri; +import com.devcourse.checkmoi.domain.comment.facade.CommentQueryFacade; +import com.devcourse.checkmoi.global.model.SimplePage; import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Create; import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Edit; import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; -import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.Comments; import com.devcourse.checkmoi.domain.comment.facade.CommentCommandFacade; import com.devcourse.checkmoi.domain.comment.service.CommentCommandService; import com.devcourse.checkmoi.domain.comment.service.CommentQueryService; import com.devcourse.checkmoi.global.model.SuccessResponse; import com.devcourse.checkmoi.global.security.jwt.JwtAuthentication; -import java.util.List; import javax.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -30,19 +31,22 @@ @RequiredArgsConstructor public class CommentApi { - private final CommentQueryService commentQueryService; - private final CommentCommandService commentCommandService; private final CommentCommandFacade commentCommandFacade; + private final CommentQueryFacade commentQueryFacade; + @GetMapping("/comments") - public ResponseEntity>> findAllComments( + public ResponseEntity> findAllComments( @AuthenticationPrincipal JwtAuthentication user, - Search request + @Valid Search request, + SimplePage simplePage ) { return ResponseEntity.ok() - .body(new SuccessResponse<>(commentQueryService.findAllComments(user.id(), request))); + .body(new SuccessResponse<>( + commentQueryFacade.findAllComments(user.id(), request, simplePage.of())) + ); } @PostMapping("/comments") diff --git a/src/main/java/com/devcourse/checkmoi/domain/comment/dto/CommentResponse.java b/src/main/java/com/devcourse/checkmoi/domain/comment/dto/CommentResponse.java index a89b72c2..75096af0 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/comment/dto/CommentResponse.java +++ b/src/main/java/com/devcourse/checkmoi/domain/comment/dto/CommentResponse.java @@ -1,11 +1,13 @@ package com.devcourse.checkmoi.domain.comment.dto; import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.Comments; import com.fasterxml.jackson.annotation.JsonFormat; import java.time.LocalDateTime; +import java.util.List; import lombok.Builder; -public sealed interface CommentResponse permits CommentInfo { +public sealed interface CommentResponse permits CommentInfo, Comments { record CommentInfo( Long id, @@ -22,4 +24,15 @@ record CommentInfo( public CommentInfo { } } + + record Comments( + List comments, + long totalPage + + ) implements CommentResponse { + + @Builder + public Comments { + } + } } diff --git a/src/main/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacade.java b/src/main/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacade.java new file mode 100644 index 00000000..b4c7e934 --- /dev/null +++ b/src/main/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacade.java @@ -0,0 +1,10 @@ +package com.devcourse.checkmoi.domain.comment.facade; + +import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.Comments; +import org.springframework.data.domain.Pageable; + +public interface CommentQueryFacade { + + Comments findAllComments(Long userId, Search request, Pageable pageable); +} diff --git a/src/main/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImpl.java b/src/main/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImpl.java new file mode 100644 index 00000000..9bff98a0 --- /dev/null +++ b/src/main/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImpl.java @@ -0,0 +1,28 @@ +package com.devcourse.checkmoi.domain.comment.facade; + +import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.Comments; +import com.devcourse.checkmoi.domain.comment.service.CommentQueryService; +import com.devcourse.checkmoi.domain.post.service.PostQueryService; +import com.devcourse.checkmoi.domain.study.service.StudyQueryService; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class CommentQueryFacadeImpl implements + CommentQueryFacade { + + private final CommentQueryService commentQueryService; + + private final PostQueryService postQueryService; + + @Override + public Comments findAllComments(Long userId, Search request, Pageable pageable) { + postQueryService.findByPostId(userId, request.postId()); + return commentQueryService.findAllComments(request, pageable); + } +} diff --git a/src/main/java/com/devcourse/checkmoi/domain/comment/repository/CustomCommentRepository.java b/src/main/java/com/devcourse/checkmoi/domain/comment/repository/CustomCommentRepository.java index 8c93b8e2..fcd254fc 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/comment/repository/CustomCommentRepository.java +++ b/src/main/java/com/devcourse/checkmoi/domain/comment/repository/CustomCommentRepository.java @@ -2,9 +2,10 @@ import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; -import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; public interface CustomCommentRepository { - List findAllByCondition(Long userId, Search request); + Page findAllByCondition(Search request, Pageable pageable); } diff --git a/src/main/java/com/devcourse/checkmoi/domain/comment/repository/CustomCommentRepositoryImpl.java b/src/main/java/com/devcourse/checkmoi/domain/comment/repository/CustomCommentRepositoryImpl.java index 2a403100..c4b421e3 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/comment/repository/CustomCommentRepositoryImpl.java +++ b/src/main/java/com/devcourse/checkmoi/domain/comment/repository/CustomCommentRepositoryImpl.java @@ -5,9 +5,14 @@ import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.JPQLQuery; +import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Repository; @Repository @@ -17,8 +22,8 @@ public class CustomCommentRepositoryImpl implements CustomCommentRepository { private final JPAQueryFactory jpaQueryFactory; @Override - public List findAllByCondition(Long userId, Search request) { - return jpaQueryFactory.select( + public Page findAllByCondition(Search request, Pageable pageable) { + JPQLQuery query = jpaQueryFactory.select( Projections.constructor( CommentInfo.class, comment.id, @@ -31,17 +36,14 @@ public List findAllByCondition(Long userId, Search request) { ) .from(comment) .where( - eqUserId(userId), eqPostId(request.postId()) - ) + ); + long totalCount = query.fetchCount(); + List comments = query + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) .fetch(); - } - - private BooleanExpression eqUserId(Long userId) { - if (userId == null) { - return null; - } - return comment.user.id.eq(userId); + return new PageImpl<>(comments, pageable, totalCount); } private BooleanExpression eqPostId(Long postId) { diff --git a/src/main/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryService.java b/src/main/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryService.java index ecb75208..cdc7c646 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryService.java +++ b/src/main/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryService.java @@ -2,9 +2,11 @@ import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.Comments; import java.util.List; +import org.springframework.data.domain.Pageable; public interface CommentQueryService { - List findAllComments(Long userId, Search request); + Comments findAllComments(Search request, Pageable pageable); } diff --git a/src/main/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImpl.java b/src/main/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImpl.java index 266465d6..2bda4ca8 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImpl.java +++ b/src/main/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImpl.java @@ -3,9 +3,11 @@ import com.devcourse.checkmoi.domain.comment.converter.CommentConverter; import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.Comments; import com.devcourse.checkmoi.domain.comment.repository.CommentRepository; -import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -19,7 +21,12 @@ public class CommentQueryServiceImpl implements CommentQueryService { private final CommentConverter commentConverter; @Override - public List findAllComments(Long userId, Search request) { - return commentRepository.findAllByCondition(userId, request); + public Comments findAllComments(Search request, Pageable pageable) { + Page comments = commentRepository.findAllByCondition(request, + pageable); + return Comments.builder() + .comments(comments.getContent()) + .totalPage(comments.getTotalPages()) + .build(); } } diff --git a/src/main/java/com/devcourse/checkmoi/domain/study/api/StudyApi.java b/src/main/java/com/devcourse/checkmoi/domain/study/api/StudyApi.java index 00be7fc6..8f51db68 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/study/api/StudyApi.java +++ b/src/main/java/com/devcourse/checkmoi/domain/study/api/StudyApi.java @@ -1,6 +1,7 @@ package com.devcourse.checkmoi.domain.study.api; import static com.devcourse.checkmoi.global.util.ApiUtil.generatedUri; +import com.devcourse.checkmoi.global.model.SimplePage; import com.devcourse.checkmoi.domain.study.dto.StudyRequest.Audit; import com.devcourse.checkmoi.domain.study.dto.StudyRequest.Create; import com.devcourse.checkmoi.domain.study.dto.StudyRequest.Edit; @@ -12,7 +13,6 @@ import com.devcourse.checkmoi.domain.study.facade.StudyUserFacade; import com.devcourse.checkmoi.domain.study.service.StudyCommandService; import com.devcourse.checkmoi.domain.study.service.StudyQueryService; -import com.devcourse.checkmoi.global.model.PageRequest; import com.devcourse.checkmoi.global.model.SuccessResponse; import com.devcourse.checkmoi.global.security.jwt.JwtAuthentication; import javax.validation.Valid; @@ -84,9 +84,9 @@ public ResponseEntity auditStudyParticipation( @GetMapping("/studies") public ResponseEntity> getStudies( @RequestParam Long bookId, - PageRequest pageRequest + SimplePage simplePage ) { - Pageable pageable = pageRequest.of(); + Pageable pageable = simplePage.of(); Studies response = studyQueryService.getStudies(bookId, pageable); return ResponseEntity.ok(new SuccessResponse<>(response)); } @@ -127,7 +127,7 @@ public ResponseEntity> getMyStudies( public ResponseEntity> getDetailInfo( @AuthenticationPrincipal JwtAuthentication user, @Valid Search search, - PageRequest pageable + SimplePage pageable ) { return ResponseEntity.ok() .body(new SuccessResponse<>( diff --git a/src/main/java/com/devcourse/checkmoi/domain/study/service/StudyQueryService.java b/src/main/java/com/devcourse/checkmoi/domain/study/service/StudyQueryService.java index 517422e5..437cb2fe 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/study/service/StudyQueryService.java +++ b/src/main/java/com/devcourse/checkmoi/domain/study/service/StudyQueryService.java @@ -4,8 +4,6 @@ import com.devcourse.checkmoi.domain.study.dto.StudyResponse.Studies; import com.devcourse.checkmoi.domain.study.dto.StudyResponse.StudyAppliers; import com.devcourse.checkmoi.domain.study.dto.StudyResponse.StudyDetailWithMembers; -import com.devcourse.checkmoi.domain.study.dto.StudyResponse.StudyInfo; -import java.util.List; import org.springframework.data.domain.Pageable; public interface StudyQueryService { @@ -24,7 +22,7 @@ public interface StudyQueryService { void ongoingStudy(Long studyId); - void participateUser(Long aLong, Long userId); + void participateUser(Long studyId, Long userId); Studies findAllByCondition(Long userId, Search search, Pageable pageable); } diff --git a/src/main/java/com/devcourse/checkmoi/global/model/PageRequest.java b/src/main/java/com/devcourse/checkmoi/global/model/SimplePage.java similarity index 52% rename from src/main/java/com/devcourse/checkmoi/global/model/PageRequest.java rename to src/main/java/com/devcourse/checkmoi/global/model/SimplePage.java index 4d419ae2..147fa3a9 100644 --- a/src/main/java/com/devcourse/checkmoi/global/model/PageRequest.java +++ b/src/main/java/com/devcourse/checkmoi/global/model/SimplePage.java @@ -1,18 +1,25 @@ package com.devcourse.checkmoi.global.model; + import lombok.Builder; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort.Direction; -public class PageRequest { +public class SimplePage { + + private static final int DEFAULT_PAGE = 1; + + private static final int DEFAULT_PAGE_SIZE = 10; - private int page = 1; + private int page = DEFAULT_PAGE; - private int size = 10; + private int size = DEFAULT_PAGE_SIZE; private Direction direction = Direction.DESC; @Builder - public PageRequest() { + public SimplePage() { + } public void setPage(int page) { @@ -29,8 +36,16 @@ public void setDirection(Direction direction) { this.direction = direction; } - public org.springframework.data.domain.PageRequest of() { - return org.springframework.data.domain.PageRequest.of(page - 1, size, direction, - "create_date"); + public PageRequest of() { + return PageRequest.of(page - 1, size, direction, "createdAt"); } + + public int getPage() { + return page; + } + + public int getSize() { + return size; + } + } diff --git a/src/test/java/com/devcourse/checkmoi/domain/book/dto/SimplePageTest.java b/src/test/java/com/devcourse/checkmoi/domain/book/dto/SimplePageTest.java index 973a7dca..fde8e2ee 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/book/dto/SimplePageTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/book/dto/SimplePageTest.java @@ -1,5 +1,6 @@ package com.devcourse.checkmoi.domain.book.dto; +import com.devcourse.checkmoi.global.model.SimplePage; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -10,9 +11,11 @@ class SimplePageTest { @DisplayName("유효하지 않은 페이지,사이즈 값을 전달받을 경우 페이지 정보는 전달받은 값이 아닌, 디폴트 값을 갖는다") void givenInvalidValueThenCreateDefault() { int size = 0; - int page = -1; + int page = 0; - SimplePage pageable = new SimplePage(page, size); + SimplePage pageable = new SimplePage(); + pageable.setSize(0); + pageable.setPage(0); Assertions.assertThat(pageable.getPage()) .isNotEqualTo(page); diff --git a/src/test/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImplTest.java b/src/test/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImplTest.java index 52de412b..bfe003ab 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImplTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImplTest.java @@ -3,10 +3,9 @@ import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeBook; import com.devcourse.checkmoi.domain.book.dto.BookResponse.BookSpecification; import com.devcourse.checkmoi.domain.book.dto.BookResponse.LatestAllBooks; -import com.devcourse.checkmoi.domain.book.dto.SimplePage; +import com.devcourse.checkmoi.global.model.SimplePage; import com.devcourse.checkmoi.domain.book.exception.BookNotFoundException; import com.devcourse.checkmoi.domain.book.model.Book; -import com.devcourse.checkmoi.domain.book.model.PublishedDate; import com.devcourse.checkmoi.domain.book.repository.BookRepository; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.AfterEach; @@ -46,9 +45,10 @@ class GetAllTopTest { @Test @DisplayName("최신순으로 책 목록을 가져온다") void getTopBooks() { - SimplePage simplePage = SimplePage.defaultPage(); + SimplePage simplePage = new SimplePage(); + simplePage.setSize(4); - LatestAllBooks allTopBooks = bookQueryService.getAllTop(simplePage); + LatestAllBooks allTopBooks = bookQueryService.getAllTop(simplePage.of()); Assertions.assertThat(allTopBooks.latestBooks().get(0).id()) .isEqualTo(lastCatBook.getId()); diff --git a/src/test/java/com/devcourse/checkmoi/domain/comment/api/CommentApiTest.java b/src/test/java/com/devcourse/checkmoi/domain/comment/api/CommentApiTest.java index 78b598ca..ba47ecec 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/comment/api/CommentApiTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/comment/api/CommentApiTest.java @@ -33,8 +33,10 @@ import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Edit; import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.Comments; import com.devcourse.checkmoi.domain.comment.exception.CommentNotFoundException; import com.devcourse.checkmoi.domain.comment.facade.CommentCommandFacade; +import com.devcourse.checkmoi.domain.comment.facade.CommentQueryFacade; import com.devcourse.checkmoi.domain.comment.model.Comment; import com.devcourse.checkmoi.domain.comment.service.CommentCommandService; import com.devcourse.checkmoi.domain.comment.service.CommentQueryService; @@ -54,6 +56,7 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; @@ -73,6 +76,9 @@ class CommentApiTest extends IntegrationTest { @MockBean private CommentCommandFacade commentCommandFacade; + @MockBean + private CommentQueryFacade commentQueryFacade; + @Nested @DisplayName("댓글 목록 조회 #130") class FindAllCommentsTest { @@ -84,16 +90,20 @@ void findAllComments() throws Exception { User writer = makeUserWithId(1L); Study study = makeStudyWithId(makeBook(), IN_PROGRESS, 2L); Post post = makePostWithId(GENERAL, study, writer, 1L); - List response = List.of( - makeCommentInfoWithId(writer, post, 1L), - makeCommentInfoWithId(writer, post, 2L), - makeCommentInfoWithId(writer, post, 3L) - ); + Comments response = Comments.builder() + .comments( + List.of( + makeCommentInfoWithId(writer, post, 1L), + makeCommentInfoWithId(writer, post, 2L), + makeCommentInfoWithId(writer, post, 3L))) + .totalPage(1L) + .build(); MultiValueMap params = new LinkedMultiValueMap<>(); params.add("postId", String.valueOf(1L)); - when(commentQueryService.findAllComments(anyLong(), any(Search.class))) + when(commentQueryFacade.findAllComments(anyLong(), any(Search.class), + any(Pageable.class))) .thenReturn(response); mockMvc.perform(get("/api/comments") @@ -104,6 +114,7 @@ void findAllComments() throws Exception { } private RestDocumentationResultHandler documentation() { + String commentsPath = "data.comments[]"; return MockMvcRestDocumentationWrapper.document("find-comments", ResourceSnippetParameters.builder() .tag("Comment API") @@ -118,18 +129,20 @@ private RestDocumentationResultHandler documentation() { parameterWithName("postId").description("게시글 아이디").optional() ), responseFields( - fieldWithPath("data[].id").type(JsonFieldType.NUMBER) + fieldWithPath(commentsPath + ".id").type(JsonFieldType.NUMBER) .description("댓글 아이디"), - fieldWithPath("data[].userId").type(JsonFieldType.NUMBER) + fieldWithPath(commentsPath + ".userId").type(JsonFieldType.NUMBER) .description("댓글 작성자 아이디"), - fieldWithPath("data[].postId").type(JsonFieldType.NUMBER) + fieldWithPath(commentsPath + ".postId").type(JsonFieldType.NUMBER) .description("게시글 아이디"), - fieldWithPath("data[].content").type(JsonFieldType.STRING) + fieldWithPath(commentsPath + ".content").type(JsonFieldType.STRING) .description("댓글 본문"), - fieldWithPath("data[].createdAt").type(JsonFieldType.STRING) + fieldWithPath(commentsPath + ".createdAt").type(JsonFieldType.STRING) .description("댓글 작성일자"), - fieldWithPath("data[].updatedAt").type(JsonFieldType.STRING) - .description("댓글 수정일자") + fieldWithPath(commentsPath + ".updatedAt").type(JsonFieldType.STRING) + .description("댓글 수정일자"), + fieldWithPath("data.totalPage").type(JsonFieldType.NUMBER) + .description("페이지 총 개수") ) ); } diff --git a/src/test/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImplTest.java b/src/test/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImplTest.java new file mode 100644 index 00000000..9864556e --- /dev/null +++ b/src/test/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImplTest.java @@ -0,0 +1,69 @@ +package com.devcourse.checkmoi.domain.comment.facade; + +import static com.devcourse.checkmoi.util.DTOGeneratorUtil.makeCommentInfo; +import static com.devcourse.checkmoi.util.DTOGeneratorUtil.makePostInfo; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.BDDMockito.given; +import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.Comments; +import com.devcourse.checkmoi.domain.comment.service.CommentQueryService; +import com.devcourse.checkmoi.domain.post.dto.PostResponse.PostInfo; +import com.devcourse.checkmoi.domain.post.service.PostQueryService; +import com.devcourse.checkmoi.global.model.SimplePage; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +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.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; + +@ExtendWith(MockitoExtension.class) +class CommentQueryFacadeImplTest { + + @InjectMocks + CommentQueryFacadeImpl commentQueryFacade; + + @Mock + PostQueryService postQueryService; + + @Mock + CommentQueryService commentQueryService; + + @Nested + @DisplayName("댓글 조회 페이지 적용 #178") + class FindAllCommentsTest { + + @Test + @DisplayName("페이지 적용 댓글 조회") + void findAllComments() { + Long userId = 1L; + Long postId = 1L; + Search search = new Search(postId); + PostInfo postInfo = makePostInfo(); + SimplePage simplePage = new SimplePage(); + long totalPage = 1L; + Comments want = new Comments( + List.of( + makeCommentInfo(1L, 1L, 1L), + makeCommentInfo(2L, 1L, 1L), + makeCommentInfo(3L, 1L, 1L) + ), totalPage + ); + + given(postQueryService.findByPostId(anyLong(), anyLong())) + .willReturn(postInfo); + given(commentQueryService.findAllComments(any(Search.class), any(Pageable.class))) + .willReturn(want); + + Comments got = commentQueryFacade.findAllComments(userId, search, simplePage.of()); + assertThat(got.comments()).hasSize(want.comments().size()); + assertThat(want.totalPage()).isEqualTo(want.totalPage()); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/devcourse/checkmoi/domain/comment/repository/CommentRepositoryTest.java b/src/test/java/com/devcourse/checkmoi/domain/comment/repository/CommentRepositoryTest.java new file mode 100644 index 00000000..ed20ff52 --- /dev/null +++ b/src/test/java/com/devcourse/checkmoi/domain/comment/repository/CommentRepositoryTest.java @@ -0,0 +1,110 @@ +package com.devcourse.checkmoi.domain.comment.repository; + +import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeBook; +import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeComment; +import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makePost; +import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeStudy; +import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeUser; +import static org.assertj.core.api.Assertions.assertThat; +import com.devcourse.checkmoi.global.model.SimplePage; +import com.devcourse.checkmoi.domain.book.model.Book; +import com.devcourse.checkmoi.domain.book.repository.BookRepository; +import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; +import com.devcourse.checkmoi.domain.comment.model.Comment; +import com.devcourse.checkmoi.domain.post.model.Post; +import com.devcourse.checkmoi.domain.post.model.PostCategory; +import com.devcourse.checkmoi.domain.post.repository.PostRepository; +import com.devcourse.checkmoi.domain.study.model.Study; +import com.devcourse.checkmoi.domain.study.model.StudyStatus; +import com.devcourse.checkmoi.domain.study.repository.StudyRepository; +import com.devcourse.checkmoi.domain.user.model.User; +import com.devcourse.checkmoi.domain.user.repository.UserRepository; +import com.devcourse.checkmoi.template.RepositoryTest; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + + +class CommentRepositoryTest extends RepositoryTest { + + @Autowired + BookRepository bookRepository; + + @Autowired + UserRepository userRepository; + + @Autowired + StudyRepository studyRepository; + + @Autowired + PostRepository postRepository; + + @Autowired + CommentRepository commentRepository; + + User user; + + Study study; + + Post post; + + @BeforeEach + void setUp() { + Book book = bookRepository.save(makeBook()); + user = userRepository.save(makeUser()); + study = studyRepository.save(makeStudy(book, StudyStatus.RECRUITING)); + post = postRepository.save(makePost(PostCategory.GENERAL, study, user)); + } + + @AfterEach + void tearDown() { + postRepository.deleteAllInBatch(); + studyRepository.deleteAllInBatch(); + userRepository.deleteAllInBatch(); + bookRepository.deleteAllInBatch(); + } + + @Nested + @DisplayName("댓글조회 페이지 네이션 #178") + class GetCommentsPage { + + List comments = new ArrayList<>(); + + @BeforeEach + void setComments() { + for (int i = 0; i < 5; i++) { + comments.add(commentRepository.save(makeComment(post, user))); + } + } + + @AfterEach + void tearDownComments() { + commentRepository.deleteAllInBatch(); + } + + @Test + @DisplayName("S 댓글을 조회한다.") + void getComments() { + Search search = new Search(post.getId()); + SimplePage pageRequest = new SimplePage(); + int size = 2; + pageRequest.setSize(size); + Pageable pageable = pageRequest.of(); + long totalPage = 3L; + + Page got = commentRepository.findAllByCondition(search, + pageable); + + assertThat(got.getContent()).hasSize(size); + assertThat(got.getTotalPages()).isEqualTo(totalPage); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImplTest.java b/src/test/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImplTest.java index 8ce9e230..9dcbd827 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImplTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImplTest.java @@ -12,11 +12,13 @@ import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeStudyMember; import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeUser; import static org.assertj.core.api.Assertions.assertThat; +import com.devcourse.checkmoi.global.model.SimplePage; import com.devcourse.checkmoi.domain.book.model.Book; import com.devcourse.checkmoi.domain.book.repository.BookRepository; import com.devcourse.checkmoi.domain.comment.converter.CommentConverter; import com.devcourse.checkmoi.domain.comment.dto.CommentRequest.Search; import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.Comments; import com.devcourse.checkmoi.domain.comment.repository.CommentRepository; import com.devcourse.checkmoi.domain.post.model.Post; import com.devcourse.checkmoi.domain.post.repository.PostRepository; @@ -102,26 +104,27 @@ void given() { } - // TODO: 권한체크 @Test - @DisplayName("S 해당 포스트에 작성한 글을 조회할 수 있다") + @DisplayName("S 해당 포스트에 작성한 댓글을 조회할 수 있다") void findAllComments() { - commentRepository.save(makeComment(givenPost, user3)); List commentInfos = Stream.of( commentRepository.save(makeComment(givenPost, user2)), - commentRepository.save(makeComment(givenPost, user2)) + commentRepository.save(makeComment(givenPost, user2)), + commentRepository.save(makeComment(givenPost, user3)) ).map(commentConverter::commentToInfo).toList(); - - var search = Search.builder() + Long totalPage = 1L; + Search search = Search.builder() .postId(givenPost.getId()) .build(); + SimplePage simplePage = new SimplePage(); + Comments comments = + commentQueryService.findAllComments(search, simplePage.of()); - List comments = - commentQueryService.findAllComments(user2.getId(), search); - - assertThat(comments) + assertThat(comments.comments()) .usingRecursiveFieldByFieldElementComparatorIgnoringFields("createdAt", "updatedAt") .hasSameElementsAs(commentInfos); + assertThat(comments.totalPage()) + .isEqualTo(totalPage); } } } diff --git a/src/test/java/com/devcourse/checkmoi/domain/study/api/StudyApiTest.java b/src/test/java/com/devcourse/checkmoi/domain/study/api/StudyApiTest.java index 2fbe4adb..f62b0eea 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/study/api/StudyApiTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/study/api/StudyApiTest.java @@ -42,7 +42,7 @@ import com.devcourse.checkmoi.domain.study.service.StudyCommandService; import com.devcourse.checkmoi.domain.study.service.StudyQueryService; import com.devcourse.checkmoi.domain.token.dto.TokenResponse.TokenWithUserInfo; -import com.devcourse.checkmoi.global.model.PageRequest; +import com.devcourse.checkmoi.global.model.SimplePage; import com.devcourse.checkmoi.template.IntegrationTest; import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper; import com.epages.restdocs.apispec.ResourceSnippetParameters; @@ -268,8 +268,8 @@ class GetStudiesTest { @DisplayName("현재 모집중인 특정 책에 대한 스터디 목록을 조회한다.") void getStudies() throws Exception { Long bookId = 1L; - PageRequest pageRequest = new PageRequest(); - Pageable pageable = pageRequest.of(); + SimplePage simplePage = new SimplePage(); + Pageable pageable = simplePage.of(); Studies response = new Studies( List.of( diff --git a/src/test/java/com/devcourse/checkmoi/domain/study/repository/StudyRepositoryTest.java b/src/test/java/com/devcourse/checkmoi/domain/study/repository/StudyRepositoryTest.java index 97e30b21..73cbcd14 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/study/repository/StudyRepositoryTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/study/repository/StudyRepositoryTest.java @@ -13,6 +13,7 @@ import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeUser; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; +import com.devcourse.checkmoi.global.model.SimplePage; import com.devcourse.checkmoi.domain.book.model.Book; import com.devcourse.checkmoi.domain.book.repository.BookRepository; import com.devcourse.checkmoi.domain.study.dto.StudyRequest.Search; @@ -25,7 +26,6 @@ import com.devcourse.checkmoi.domain.study.model.StudyMemberStatus; import com.devcourse.checkmoi.domain.user.model.User; import com.devcourse.checkmoi.domain.user.repository.UserRepository; -import com.devcourse.checkmoi.global.model.PageRequest; import com.devcourse.checkmoi.template.RepositoryTest; import java.util.ArrayList; import java.util.List; @@ -124,11 +124,11 @@ void init() { @Test @DisplayName("책 아이디를 기준으로 모집중인 스터디 정보를 조회한다.") void findRecruitingStudyByBookId() { - PageRequest pageRequest = new PageRequest(); - pageRequest.setSize(4); + SimplePage simplePage = new SimplePage(); + simplePage.setSize(4); int totalPage = 1; Page result = - studyRepository.findRecruitingStudyByBookId(givenBook.getId(), pageRequest.of()); + studyRepository.findRecruitingStudyByBookId(givenBook.getId(), simplePage.of()); assertThat(result.getContent()).hasSize(4); assertThat(result.getTotalPages()).isEqualTo(totalPage); @@ -423,7 +423,7 @@ void searchStudies() { Search search = Search.builder() .userId(givenUser.getId()) .build(); - PageRequest page = PageRequest.builder().build(); + SimplePage page = SimplePage.builder().build(); Page result = studyRepository.findAllByCondition(givenUser.getId(), search, page.of()); @@ -441,7 +441,7 @@ void isMemberStudies() { .studyStatus(IN_PROGRESS.toString()) .memberStatus(ACCEPTED.toString()) .build(); - PageRequest page = PageRequest.builder().build(); + SimplePage page = SimplePage.builder().build(); Page result = studyRepository.findAllByCondition(givenUser.getId(), search, page.of()); @@ -457,7 +457,7 @@ void isNotMemberStudies() { .userId(givenUser.getId()) .isMember(false) .build(); - PageRequest page = PageRequest.builder().build(); + SimplePage page = SimplePage.builder().build(); Page result = studyRepository.findAllByCondition(givenUser.getId(), search, page.of()); @@ -474,7 +474,7 @@ void acceptedAndInProgressStudies() { .studyStatus("recruiting") .memberStatus("owned") .build(); - PageRequest page = PageRequest.builder().build(); + SimplePage page = SimplePage.builder().build(); Page result = studyRepository.findAllByCondition(givenUser.getId(), search, page.of()); diff --git a/src/test/java/com/devcourse/checkmoi/domain/study/service/StudyQueryServiceImplTest.java b/src/test/java/com/devcourse/checkmoi/domain/study/service/StudyQueryServiceImplTest.java index 62c36223..0b70a22d 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/study/service/StudyQueryServiceImplTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/study/service/StudyQueryServiceImplTest.java @@ -29,7 +29,7 @@ import com.devcourse.checkmoi.domain.study.repository.StudyRepository; import com.devcourse.checkmoi.domain.study.service.validator.StudyServiceValidator; import com.devcourse.checkmoi.domain.user.model.User; -import com.devcourse.checkmoi.global.model.PageRequest; +import com.devcourse.checkmoi.global.model.SimplePage; import java.util.List; import java.util.Optional; import org.assertj.core.api.Assertions; @@ -72,8 +72,8 @@ class GetStudiesTest { void getStudies() { Long bookId = 1L; long totalPage = 1L; - PageRequest pageRequest = new PageRequest(); - Pageable pageable = pageRequest.of(); + SimplePage simplePage = new SimplePage(); + Pageable pageable = simplePage.of(); Page studies = new PageImpl<>(List.of( makeStudyInfo(makeStudyWithId(makeBookWithId(1L), StudyStatus.RECRUITING, 1L)), makeStudyInfo(makeStudyWithId(makeBookWithId(1L), StudyStatus.RECRUITING, 3L)) diff --git a/src/test/java/com/devcourse/checkmoi/util/DTOGeneratorUtil.java b/src/test/java/com/devcourse/checkmoi/util/DTOGeneratorUtil.java index 5e135ca2..46b1e8a2 100644 --- a/src/test/java/com/devcourse/checkmoi/util/DTOGeneratorUtil.java +++ b/src/test/java/com/devcourse/checkmoi/util/DTOGeneratorUtil.java @@ -3,6 +3,7 @@ import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeBook; import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeStudyWithId; import static com.devcourse.checkmoi.util.EntityGeneratorUtil.makeUserWithId; +import com.devcourse.checkmoi.domain.comment.dto.CommentResponse.CommentInfo; import com.devcourse.checkmoi.domain.post.dto.PostResponse.PostInfo; import com.devcourse.checkmoi.domain.post.model.PostCategory; import com.devcourse.checkmoi.domain.study.dto.StudyResponse.Studies; @@ -11,10 +12,12 @@ import com.devcourse.checkmoi.domain.study.model.StudyStatus; import com.devcourse.checkmoi.domain.user.dto.UserResponse.UserInfo; import com.devcourse.checkmoi.domain.user.model.User; +import java.time.LocalDateTime; import java.util.List; public abstract class DTOGeneratorUtil { + // study public static StudyInfo makeStudyInfo(Study study) { return StudyInfo.builder() .id(study.getId()) @@ -32,6 +35,7 @@ public static StudyInfo makeStudyInfo(Study study) { } + // post public static PostInfo makePostInfo() { return PostInfo.builder() .id(1L) @@ -45,6 +49,7 @@ public static PostInfo makePostInfo() { .build(); } + // user public static UserInfo makeUserInfo() { User user = makeUserWithId(1L); return UserInfo.builder() @@ -56,6 +61,7 @@ public static UserInfo makeUserInfo() { .build(); } + // studies public static List makeMyStudies() { return List.of( new Studies( @@ -69,4 +75,16 @@ public static List makeMyStudies() { ) ); } + + // comment + public static CommentInfo makeCommentInfo(Long commentId, Long postId, Long userId) { + return CommentInfo.builder() + .id(commentId) + .content("댓글 테스트") + .postId(postId) + .userId(userId) + .createdAt(LocalDateTime.now()) + .updatedAt(LocalDateTime.now()) + .build(); + } } From 3f5212f39ddc58c16a1b9fac621848e32782db1a Mon Sep 17 00:00:00 2001 From: yongckim Date: Thu, 11 Aug 2022 01:53:01 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=94=A7=20chore=20:=20=EC=96=B4?= =?UTF-8?q?=ED=94=8C=EB=A6=AC=EC=BC=80=EC=9D=B4=EC=85=98=20=EB=B2=84?= =?UTF-8?q?=EC=A0=84=20=EC=97=85=20=EB=AA=85=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 81e15460..5362dfa8 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ plugins { } group = 'com.devcourse' -version = '2.0.0' +version = '3.0.0' sourceCompatibility = '17' configurations { From 45a8b0bf2fcd5029c842932043f6305b4068a184 Mon Sep 17 00:00:00 2001 From: yongckim Date: Thu, 11 Aug 2022 14:24:34 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor=20:=20SimpleP?= =?UTF-8?q?age=20setter=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20validation=20?= =?UTF-8?q?=EC=B2=B4=ED=81=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../checkmoi/domain/book/api/BookApi.java | 11 ++--- .../domain/comment/api/CommentApi.java | 2 +- .../checkmoi/domain/study/api/StudyApi.java | 4 +- .../checkmoi/global/model/SimplePage.java | 45 +++++++++++-------- .../domain/book/dto/SimplePageTest.java | 7 +-- .../service/BookQueryServiceImplTest.java | 8 ++-- .../facade/CommentQueryFacadeImplTest.java | 7 ++- .../repository/CommentRepositoryTest.java | 10 +++-- .../service/CommentQueryServiceImplTest.java | 7 ++- .../domain/study/api/StudyApiTest.java | 10 +++-- .../study/repository/StudyRepositoryTest.java | 16 ++++--- .../service/StudyQueryServiceImplTest.java | 7 ++- 12 files changed, 81 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/devcourse/checkmoi/domain/book/api/BookApi.java b/src/main/java/com/devcourse/checkmoi/domain/book/api/BookApi.java index 775dfe14..6a108890 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/book/api/BookApi.java +++ b/src/main/java/com/devcourse/checkmoi/domain/book/api/BookApi.java @@ -42,11 +42,12 @@ public ResponseEntity> register( } @GetMapping - public ResponseEntity> topBooks( - SimplePage pageRequest - ) { - pageRequest.setSize(4); - LatestAllBooks topBooks = bookQueryService.getAllTop(pageRequest.of()); + public ResponseEntity> topBooks() { + SimplePage pageRequest = SimplePage.builder() + .page(1) + .size(4) + .build(); + LatestAllBooks topBooks = bookQueryService.getAllTop(pageRequest.pageRequest()); return ResponseEntity.ok( new SuccessResponse<>(topBooks) diff --git a/src/main/java/com/devcourse/checkmoi/domain/comment/api/CommentApi.java b/src/main/java/com/devcourse/checkmoi/domain/comment/api/CommentApi.java index 084e648f..566c3e25 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/comment/api/CommentApi.java +++ b/src/main/java/com/devcourse/checkmoi/domain/comment/api/CommentApi.java @@ -45,7 +45,7 @@ public ResponseEntity> findAllComments( ) { return ResponseEntity.ok() .body(new SuccessResponse<>( - commentQueryFacade.findAllComments(user.id(), request, simplePage.of())) + commentQueryFacade.findAllComments(user.id(), request, simplePage.pageRequest())) ); } diff --git a/src/main/java/com/devcourse/checkmoi/domain/study/api/StudyApi.java b/src/main/java/com/devcourse/checkmoi/domain/study/api/StudyApi.java index 8f51db68..dd151152 100644 --- a/src/main/java/com/devcourse/checkmoi/domain/study/api/StudyApi.java +++ b/src/main/java/com/devcourse/checkmoi/domain/study/api/StudyApi.java @@ -86,7 +86,7 @@ public ResponseEntity> getStudies( @RequestParam Long bookId, SimplePage simplePage ) { - Pageable pageable = simplePage.of(); + Pageable pageable = simplePage.pageRequest(); Studies response = studyQueryService.getStudies(bookId, pageable); return ResponseEntity.ok(new SuccessResponse<>(response)); } @@ -131,7 +131,7 @@ public ResponseEntity> getDetailInfo( ) { return ResponseEntity.ok() .body(new SuccessResponse<>( - studyQueryService.findAllByCondition(user.id(), search, pageable.of()))); + studyQueryService.findAllByCondition(user.id(), search, pageable.pageRequest()))); } } diff --git a/src/main/java/com/devcourse/checkmoi/global/model/SimplePage.java b/src/main/java/com/devcourse/checkmoi/global/model/SimplePage.java index 147fa3a9..dd05aa1a 100644 --- a/src/main/java/com/devcourse/checkmoi/global/model/SimplePage.java +++ b/src/main/java/com/devcourse/checkmoi/global/model/SimplePage.java @@ -7,45 +7,52 @@ public class SimplePage { + private static final int LOWER_LIMIT_VALID_PAGE_SIZE = 1; + + private static final int LOWER_LIMIT_VALID_PAGE = 1; + private static final int DEFAULT_PAGE = 1; private static final int DEFAULT_PAGE_SIZE = 10; - private int page = DEFAULT_PAGE; + private Integer page; - private int size = DEFAULT_PAGE_SIZE; + private Integer size; - private Direction direction = Direction.DESC; + private Direction direction; @Builder - public SimplePage() { - + public SimplePage(Integer page, Integer size, Direction direction) { + this.page = isInvalidPage(page) ? DEFAULT_PAGE : page; + this.size = isInvalidSize(size) ? DEFAULT_PAGE_SIZE : size; + this.direction = isInvalidDirection(direction) ? Direction.ASC : direction; } - public void setPage(int page) { - this.page = page <= 0 ? 1 : page; + public PageRequest pageRequest() { + return PageRequest.of(page - 1, size, Direction.ASC, "createdAt"); } - public void setSize(int size) { - int defaultSize = 10; - int maxSize = 50; - this.size = size > maxSize ? defaultSize : size; + public int getPage() { + return page; } - public void setDirection(Direction direction) { - this.direction = direction; + public int getSize() { + return size; } - public PageRequest of() { - return PageRequest.of(page - 1, size, direction, "createdAt"); + public Direction getDirection() { + return direction; } - public int getPage() { - return page; + private boolean isInvalidPage(Integer page) { + return page == null || page < LOWER_LIMIT_VALID_PAGE; } - public int getSize() { - return size; + private boolean isInvalidSize(Integer size) { + return size == null || size < LOWER_LIMIT_VALID_PAGE_SIZE; } + public boolean isInvalidDirection(Direction direction) { + return direction == null; + } } diff --git a/src/test/java/com/devcourse/checkmoi/domain/book/dto/SimplePageTest.java b/src/test/java/com/devcourse/checkmoi/domain/book/dto/SimplePageTest.java index fde8e2ee..4b2fb81b 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/book/dto/SimplePageTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/book/dto/SimplePageTest.java @@ -13,9 +13,10 @@ void givenInvalidValueThenCreateDefault() { int size = 0; int page = 0; - SimplePage pageable = new SimplePage(); - pageable.setSize(0); - pageable.setPage(0); + SimplePage pageable = SimplePage.builder() + .size(0) + .page(0) + .build(); Assertions.assertThat(pageable.getPage()) .isNotEqualTo(page); diff --git a/src/test/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImplTest.java b/src/test/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImplTest.java index bfe003ab..bd7688e6 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImplTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/book/service/BookQueryServiceImplTest.java @@ -45,10 +45,12 @@ class GetAllTopTest { @Test @DisplayName("최신순으로 책 목록을 가져온다") void getTopBooks() { - SimplePage simplePage = new SimplePage(); - simplePage.setSize(4); + SimplePage simplePage = SimplePage.builder() + .page(1) + .size(4) + .build(); - LatestAllBooks allTopBooks = bookQueryService.getAllTop(simplePage.of()); + LatestAllBooks allTopBooks = bookQueryService.getAllTop(simplePage.pageRequest()); Assertions.assertThat(allTopBooks.latestBooks().get(0).id()) .isEqualTo(lastCatBook.getId()); diff --git a/src/test/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImplTest.java b/src/test/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImplTest.java index 9864556e..c2fb2fd1 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImplTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/comment/facade/CommentQueryFacadeImplTest.java @@ -46,7 +46,10 @@ void findAllComments() { Long postId = 1L; Search search = new Search(postId); PostInfo postInfo = makePostInfo(); - SimplePage simplePage = new SimplePage(); + SimplePage simplePage = SimplePage.builder() + .page(1) + .size(3) + .build(); long totalPage = 1L; Comments want = new Comments( List.of( @@ -61,7 +64,7 @@ void findAllComments() { given(commentQueryService.findAllComments(any(Search.class), any(Pageable.class))) .willReturn(want); - Comments got = commentQueryFacade.findAllComments(userId, search, simplePage.of()); + Comments got = commentQueryFacade.findAllComments(userId, search, simplePage.pageRequest()); assertThat(got.comments()).hasSize(want.comments().size()); assertThat(want.totalPage()).isEqualTo(want.totalPage()); } diff --git a/src/test/java/com/devcourse/checkmoi/domain/comment/repository/CommentRepositoryTest.java b/src/test/java/com/devcourse/checkmoi/domain/comment/repository/CommentRepositoryTest.java index ed20ff52..c6791aee 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/comment/repository/CommentRepositoryTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/comment/repository/CommentRepositoryTest.java @@ -93,11 +93,13 @@ void tearDownComments() { @Test @DisplayName("S 댓글을 조회한다.") void getComments() { - Search search = new Search(post.getId()); - SimplePage pageRequest = new SimplePage(); int size = 2; - pageRequest.setSize(size); - Pageable pageable = pageRequest.of(); + Search search = new Search(post.getId()); + SimplePage pageRequest = SimplePage.builder() + .page(1) + .size(size) + .build(); + Pageable pageable = pageRequest.pageRequest(); long totalPage = 3L; Page got = commentRepository.findAllByCondition(search, diff --git a/src/test/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImplTest.java b/src/test/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImplTest.java index 9dcbd827..e0e9b271 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImplTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/comment/service/CommentQueryServiceImplTest.java @@ -116,9 +116,12 @@ void findAllComments() { Search search = Search.builder() .postId(givenPost.getId()) .build(); - SimplePage simplePage = new SimplePage(); + SimplePage simplePage = SimplePage.builder() + .size(3) + .page(1) + .build(); Comments comments = - commentQueryService.findAllComments(search, simplePage.of()); + commentQueryService.findAllComments(search, simplePage.pageRequest()); assertThat(comments.comments()) .usingRecursiveFieldByFieldElementComparatorIgnoringFields("createdAt", "updatedAt") diff --git a/src/test/java/com/devcourse/checkmoi/domain/study/api/StudyApiTest.java b/src/test/java/com/devcourse/checkmoi/domain/study/api/StudyApiTest.java index f62b0eea..cb214446 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/study/api/StudyApiTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/study/api/StudyApiTest.java @@ -268,8 +268,12 @@ class GetStudiesTest { @DisplayName("현재 모집중인 특정 책에 대한 스터디 목록을 조회한다.") void getStudies() throws Exception { Long bookId = 1L; - SimplePage simplePage = new SimplePage(); - Pageable pageable = simplePage.of(); + SimplePage simplePage = SimplePage + .builder() + .page(1) + .size(2) + .build(); + Pageable pageable = simplePage.pageRequest(); Studies response = new Studies( List.of( @@ -278,7 +282,7 @@ void getStudies() throws Exception { ).stream() .map(studyConverter::studyToStudyInfo) .toList(), - 0 + 1 ); given(studyQueryService.getStudies(anyLong(), any(Pageable.class))) diff --git a/src/test/java/com/devcourse/checkmoi/domain/study/repository/StudyRepositoryTest.java b/src/test/java/com/devcourse/checkmoi/domain/study/repository/StudyRepositoryTest.java index 73cbcd14..85d678f6 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/study/repository/StudyRepositoryTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/study/repository/StudyRepositoryTest.java @@ -124,11 +124,13 @@ void init() { @Test @DisplayName("책 아이디를 기준으로 모집중인 스터디 정보를 조회한다.") void findRecruitingStudyByBookId() { - SimplePage simplePage = new SimplePage(); - simplePage.setSize(4); int totalPage = 1; + SimplePage simplePage = SimplePage.builder() + .page(totalPage) + .size(4) + .build(); Page result = - studyRepository.findRecruitingStudyByBookId(givenBook.getId(), simplePage.of()); + studyRepository.findRecruitingStudyByBookId(givenBook.getId(), simplePage.pageRequest()); assertThat(result.getContent()).hasSize(4); assertThat(result.getTotalPages()).isEqualTo(totalPage); @@ -426,7 +428,7 @@ void searchStudies() { SimplePage page = SimplePage.builder().build(); Page result = - studyRepository.findAllByCondition(givenUser.getId(), search, page.of()); + studyRepository.findAllByCondition(givenUser.getId(), search, page.pageRequest()); assertThat(result.getContent()).hasSize(3); } @@ -444,7 +446,7 @@ void isMemberStudies() { SimplePage page = SimplePage.builder().build(); Page result = - studyRepository.findAllByCondition(givenUser.getId(), search, page.of()); + studyRepository.findAllByCondition(givenUser.getId(), search, page.pageRequest()); assertThat(result.getContent()).hasSize(2); } @@ -460,7 +462,7 @@ void isNotMemberStudies() { SimplePage page = SimplePage.builder().build(); Page result = - studyRepository.findAllByCondition(givenUser.getId(), search, page.of()); + studyRepository.findAllByCondition(givenUser.getId(), search, page.pageRequest()); assertThat(result.getContent()).hasSize(2); } @@ -477,7 +479,7 @@ void acceptedAndInProgressStudies() { SimplePage page = SimplePage.builder().build(); Page result = studyRepository.findAllByCondition(givenUser.getId(), - search, page.of()); + search, page.pageRequest()); assertThat(result.getContent()).hasSize(1); } } diff --git a/src/test/java/com/devcourse/checkmoi/domain/study/service/StudyQueryServiceImplTest.java b/src/test/java/com/devcourse/checkmoi/domain/study/service/StudyQueryServiceImplTest.java index 0b70a22d..78b945c3 100644 --- a/src/test/java/com/devcourse/checkmoi/domain/study/service/StudyQueryServiceImplTest.java +++ b/src/test/java/com/devcourse/checkmoi/domain/study/service/StudyQueryServiceImplTest.java @@ -72,8 +72,11 @@ class GetStudiesTest { void getStudies() { Long bookId = 1L; long totalPage = 1L; - SimplePage simplePage = new SimplePage(); - Pageable pageable = simplePage.of(); + SimplePage simplePage = SimplePage.builder() + .page(1) + .size(2) + .build(); + Pageable pageable = simplePage.pageRequest(); Page studies = new PageImpl<>(List.of( makeStudyInfo(makeStudyWithId(makeBookWithId(1L), StudyStatus.RECRUITING, 1L)), makeStudyInfo(makeStudyWithId(makeBookWithId(1L), StudyStatus.RECRUITING, 3L))