Skip to content

Commit

Permalink
Merge pull request #14 from Domitory-CheckMate/feature/13-post
Browse files Browse the repository at this point in the history
[feat] 키워드 게시물 조회 api 구현
  • Loading branch information
RyuKwanKon authored Jan 3, 2024
2 parents e284a18 + 05fbbe8 commit d19b8fb
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
import org.gachon.checkmate.global.config.auth.UserId;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RequestMapping("/api/post")
Expand All @@ -24,7 +19,15 @@ public class PostController {
public ResponseEntity<SuccessResponse<?>> searchTextPost(@UserId final Long userId,
@RequestParam final String text,
final Pageable pageable) {
final List<PostSearchResponseDto> responseDto = postService.searchTextPost(userId, text, pageable);
final PostSearchResponseDto responseDto = postService.searchTextPost(userId, text, pageable);
return SuccessResponse.ok(responseDto);
}

@GetMapping("/search/{key}")
public ResponseEntity<SuccessResponse<?>> searchKeyWordPost(@UserId final Long userId,
@PathVariable final String key,
final Pageable pageable) {
final PostSearchResponseDto responseDto = postService.searchKeyWordPost(userId, key, pageable);
return SuccessResponse.ok(responseDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.gachon.checkmate.domain.post.dto.response;

import lombok.Builder;
import org.gachon.checkmate.domain.post.dto.support.PostSearchDto;

@Builder
public record PostSearchElementResponseDto(
String title,
String content,
String importantKey,
String similarityKey,
int scrapCount,
int remainDate,
int accuracy
) {
public static PostSearchElementResponseDto of(PostSearchDto postSearchDto,
int remainDate,
int accuracy) {
return PostSearchElementResponseDto.builder()
.title(postSearchDto.title())
.content(postSearchDto.content())
.importantKey(postSearchDto.importantKey().getDesc())
.similarityKey(postSearchDto.similarityKey().getDesc())
.scrapCount(postSearchDto.scrapCount())
.remainDate(remainDate)
.accuracy(accuracy)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package org.gachon.checkmate.domain.post.dto.response;

import lombok.Builder;
import org.gachon.checkmate.domain.post.dto.support.PostSearchDto;

import java.util.List;

@Builder
public record PostSearchResponseDto(
String title,
String content,
String importantKey,
String similarityKey,
int scrapCount,
int remainDate,
int accuracy
) {
public static PostSearchResponseDto of(PostSearchDto postSearchDto,
int remainDate,
int accuracy) {
List<PostSearchElementResponseDto> results,
int totalPages,
long totalElements) {
public static PostSearchResponseDto of(List<PostSearchElementResponseDto> results, int totalPages, long totalElements) {
return PostSearchResponseDto.builder()
.title(postSearchDto.title())
.content(postSearchDto.content())
.importantKey(postSearchDto.importantKey().getDesc())
.similarityKey(postSearchDto.similarityKey().getDesc())
.scrapCount(postSearchDto.scrapCount())
.remainDate(remainDate)
.accuracy(accuracy)
.results(results)
.totalPages(totalPages)
.totalElements(totalElements)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.RequiredArgsConstructor;
import org.gachon.checkmate.domain.post.dto.support.PostSearchDto;
import org.gachon.checkmate.domain.post.dto.support.QPostSearchDto;
import org.gachon.checkmate.domain.post.entity.ImportantKeyType;
import org.gachon.checkmate.domain.post.entity.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -23,6 +24,32 @@
public class PostQuerydslRepository {
private final JPAQueryFactory queryFactory;

public Page<PostSearchDto> searchKeyPost(ImportantKeyType importantKeyType, 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(
containKeyWordCondition(importantKeyType),
validatePostDate()
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

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

public Page<PostSearchDto> searchTextPost(String text, Pageable pageable) {
List<PostSearchDto> content = queryFactory
.select(new QPostSearchDto(
Expand All @@ -37,8 +64,8 @@ public Page<PostSearchDto> searchTextPost(String text, Pageable pageable) {
.from(post)
.leftJoin(post.postCheckList, postCheckList)
.where(
post.title.contains(text),
post.endDate.before(LocalDate.now())
containTextCondition(text),
validatePostDate()
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand All @@ -48,4 +75,16 @@ public Page<PostSearchDto> searchTextPost(String text, Pageable pageable) {
.selectFrom(post);
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchCount);
}

private BooleanExpression containKeyWordCondition(ImportantKeyType importantKeyType) {
return post.importantKeyType.eq(importantKeyType);
}

private BooleanExpression containTextCondition(String text) {
return post.title.contains(text);
}

private BooleanExpression validatePostDate() {
return post.endDate.before(LocalDate.now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import org.gachon.checkmate.domain.checkList.entity.CheckList;
import org.gachon.checkmate.domain.checkList.entity.PostCheckList;
import org.gachon.checkmate.domain.checkList.repository.CheckListRepository;
import org.gachon.checkmate.domain.post.dto.response.PostSearchElementResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostSearchResponseDto;
import org.gachon.checkmate.domain.post.dto.support.PostSearchDto;
import org.gachon.checkmate.domain.post.entity.ImportantKeyType;
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;
Expand All @@ -28,20 +31,28 @@ public class PostService {
private final CheckListRepository checkListRepository;
private final PostQuerydslRepository postQuerydslRepository;

public List<PostSearchResponseDto> searchTextPost(Long userId, String text, Pageable pageable) {
public PostSearchResponseDto searchKeyWordPost(Long userId, String key, Pageable pageable) {
CheckList checkList = getCheckList(userId);
Page<PostSearchDto> postSearchDtoList = getPostSearchDto(text, pageable);
return createPostSearchResponseDto(postSearchDtoList, checkList);
ImportantKeyType importantKeyType = EnumValueUtils.toEntityCode(ImportantKeyType.class, key);
Page<PostSearchDto> postSearchList = getKeySearchResults(importantKeyType, pageable);
List<PostSearchElementResponseDto> searchResults = createPostSearchResponseDto(postSearchList, checkList);
return PostSearchResponseDto.of(searchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements());
}

private List<PostSearchResponseDto> createPostSearchResponseDto(Page<PostSearchDto> postSearchDtoList, CheckList checkList) {
public PostSearchResponseDto searchTextPost(Long userId, String text, Pageable pageable) {
CheckList checkList = getCheckList(userId);
Page<PostSearchDto> postSearchList = getTextSearchResults(text, pageable);
List<PostSearchElementResponseDto> searchResults = createPostSearchResponseDto(postSearchList, checkList);
return PostSearchResponseDto.of(searchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements());
}

private List<PostSearchElementResponseDto> createPostSearchResponseDto(Page<PostSearchDto> postSearchDtoList, CheckList checkList) {
return postSearchDtoList.stream()
.map(postSearchDto ->
PostSearchResponseDto.of(
PostSearchElementResponseDto.of(
postSearchDto,
getRemainDate(postSearchDto.endDate()),
getAccuracy(postSearchDto.postCheckList(), checkList)
))
getAccuracy(postSearchDto.postCheckList(), checkList)))
.collect(Collectors.toList());
}

Expand All @@ -64,7 +75,11 @@ private int getRemainDate(LocalDate endDate) {
return (int) endDate.until(LocalDate.now(), ChronoUnit.DAYS);
}

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

private Page<PostSearchDto> getTextSearchResults(String text, Pageable pageable) {
return postQuerydslRepository.searchTextPost(text, pageable);
}

Expand Down

0 comments on commit d19b8fb

Please sign in to comment.