Skip to content

Commit

Permalink
Merge pull request #18 from Domitory-CheckMate/feature/17-post
Browse files Browse the repository at this point in the history
[feat] 게시물 전체 조회 api 구현
  • Loading branch information
RyuKwanKon authored Jan 3, 2024
2 parents 7acb591 + f0206e6 commit a037274
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
public class PostController {
private final PostService postService;

@GetMapping("/search")
public ResponseEntity<SuccessResponse<?>> searchTextPost(@UserId final Long userId,
@RequestParam final String text,
final Pageable pageable) {
final PostSearchResponseDto responseDto = postService.searchTextPost(userId, text, pageable);
@GetMapping
public ResponseEntity<SuccessResponse<?>> getAllPosts(@UserId final Long userId,
@RequestParam final String type,
final Pageable pageable){
final PostSearchResponseDto responseDto = postService.getAllPosts(userId, type, pageable);
return SuccessResponse.ok(responseDto);
}

Expand All @@ -31,4 +31,12 @@ public ResponseEntity<SuccessResponse<?>> searchKeyWordPost(@UserId final Long u
final PostSearchResponseDto responseDto = postService.searchKeyWordPost(userId, key, type, pageable);
return SuccessResponse.ok(responseDto);
}

@GetMapping("/search")
public ResponseEntity<SuccessResponse<?>> searchTextPost(@UserId final Long userId,
@RequestParam final String text,
final Pageable pageable) {
final PostSearchResponseDto responseDto = postService.searchTextPost(userId, text, pageable);
return SuccessResponse.ok(responseDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@
public class PostQuerydslRepository {
private final JPAQueryFactory queryFactory;

public Page<PostSearchDto> findAllPosts(Pageable pageable) {
List<PostSearchDto> content = queryFactory
.select(new QPostSearchDto(
post.title,
post.content,
post.importantKeyType,
post.similarityKeyType,
post.endDate,
post.scrapList.size(),
postCheckList
))
.from(post)
.leftJoin(post.postCheckList, postCheckList)
.where(
validatePostDate()
)
.fetch();

JPAQuery<Post> countQuery = queryFactory
.selectFrom(post);
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchCount);
}

public Page<PostSearchDto> searchKeyPost(ImportantKeyType importantKeyType, Pageable pageable) {
List<PostSearchDto> content = queryFactory
.select(new QPostSearchDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
import org.gachon.checkmate.domain.post.entity.SortType;
import org.gachon.checkmate.domain.post.repository.PostQuerydslRepository;
import org.gachon.checkmate.global.error.exception.EntityNotFoundException;
import org.gachon.checkmate.global.utils.EnumValueUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
Expand All @@ -37,6 +35,17 @@ public class PostService {
private final CheckListRepository checkListRepository;
private final PostQuerydslRepository postQuerydslRepository;

public PostSearchResponseDto getAllPosts(Long userId, String type, Pageable pageable) {
CheckList checkList = getCheckList(userId);
SortType sortType = toEntityCode(SortType.class, type);
Page<PostSearchDto> postSearchList = getAllPostsResults(pageable);
List<PostSearchElementResponseDto> searchResults = createPostSearchResponseDto(postSearchList, checkList);
sortByTypeForSearchResults(searchResults, Objects.requireNonNull(sortType));
List<PostSearchElementResponseDto> pagingSearchResults
= convertPaging(searchResults, pageable.getOffset(), pageable.getPageSize());
return PostSearchResponseDto.of(pagingSearchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements());
}

public PostSearchResponseDto searchKeyWordPost(Long userId, String key, String type, Pageable pageable) {
CheckList checkList = getCheckList(userId);
SortType sortType = toEntityCode(SortType.class, type);
Expand Down Expand Up @@ -100,6 +109,10 @@ private int getRemainDate(LocalDate endDate) {
return (int) endDate.until(LocalDate.now(), ChronoUnit.DAYS);
}

private Page<PostSearchDto> getAllPostsResults(Pageable pageable) {
return postQuerydslRepository.findAllPosts(pageable);
}

private Page<PostSearchDto> getKeySearchResults(ImportantKeyType importantKeyType, Pageable pageable) {
return postQuerydslRepository.searchKeyPost(importantKeyType, pageable);
}
Expand Down

0 comments on commit a037274

Please sign in to comment.