Skip to content
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

✨ feat : 댓글 조회 페이지네이션 적용 및 검증 로직 추가 #183

Merged
merged 3 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ plugins {
}

group = 'com.devcourse'
version = '2.0.0'
version = '3.0.0'
sourceCompatibility = '17'

configurations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +43,11 @@ public ResponseEntity<SuccessResponse<Long>> register(

@GetMapping
public ResponseEntity<SuccessResponse<LatestAllBooks>> topBooks() {
LatestAllBooks topBooks = bookQueryService.getAllTop(SimplePage.defaultPage());
SimplePage pageRequest = SimplePage.builder()
.page(1)
.size(4)
.build();
LatestAllBooks topBooks = bookQueryService.getAllTop(pageRequest.pageRequest());

return ResponseEntity.ok(
new SuccessResponse<>(topBooks)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<SuccessResponse<List<CommentInfo>>> findAllComments(
public ResponseEntity<SuccessResponse<Comments>> 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.pageRequest()))
);
}

@PostMapping("/comments")
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -22,4 +24,15 @@ record CommentInfo(
public CommentInfo {
}
}

record Comments(
List<CommentInfo> comments,
long totalPage

) implements CommentResponse {

@Builder
public Comments {
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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());
zxcv9203 marked this conversation as resolved.
Show resolved Hide resolved
return commentQueryService.findAllComments(request, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommentInfo> findAllByCondition(Long userId, Search request);
zxcv9203 marked this conversation as resolved.
Show resolved Hide resolved
Page<CommentInfo> findAllByCondition(Search request, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,8 +22,8 @@ public class CustomCommentRepositoryImpl implements CustomCommentRepository {
private final JPAQueryFactory jpaQueryFactory;

@Override
public List<CommentInfo> findAllByCondition(Long userId, Search request) {
return jpaQueryFactory.select(
public Page<CommentInfo> findAllByCondition(Search request, Pageable pageable) {
JPQLQuery<CommentInfo> query = jpaQueryFactory.select(
Projections.constructor(
CommentInfo.class,
comment.id,
Expand All @@ -31,17 +36,14 @@ public List<CommentInfo> findAllByCondition(Long userId, Search request) {
)
.from(comment)
.where(
eqUserId(userId),
eqPostId(request.postId())
)
);
long totalCount = query.fetchCount();
List<CommentInfo> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommentInfo> findAllComments(Long userId, Search request);
Comments findAllComments(Search request, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,7 +21,12 @@ public class CommentQueryServiceImpl implements CommentQueryService {
private final CommentConverter commentConverter;

@Override
public List<CommentInfo> findAllComments(Long userId, Search request) {
return commentRepository.findAllByCondition(userId, request);
public Comments findAllComments(Search request, Pageable pageable) {
Page<CommentInfo> comments = commentRepository.findAllByCondition(request,
pageable);
return Comments.builder()
.comments(comments.getContent())
.totalPage(comments.getTotalPages())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -84,9 +84,9 @@ public ResponseEntity<Void> auditStudyParticipation(
@GetMapping("/studies")
public ResponseEntity<SuccessResponse<Studies>> getStudies(
@RequestParam Long bookId,
PageRequest pageRequest
SimplePage simplePage
) {
Pageable pageable = pageRequest.of();
Pageable pageable = simplePage.pageRequest();
Studies response = studyQueryService.getStudies(bookId, pageable);
return ResponseEntity.ok(new SuccessResponse<>(response));
}
Expand Down Expand Up @@ -127,11 +127,11 @@ public ResponseEntity<SuccessResponse<MyStudies>> getMyStudies(
public ResponseEntity<SuccessResponse<Studies>> getDetailInfo(
@AuthenticationPrincipal JwtAuthentication user,
@Valid Search search,
PageRequest pageable
SimplePage pageable
) {
return ResponseEntity.ok()
.body(new SuccessResponse<>(
studyQueryService.findAllByCondition(user.id(), search, pageable.of())));
studyQueryService.findAllByCondition(user.id(), search, pageable.pageRequest())));
}

}
Loading