From ec709fbc87a6958aa1948a67480920e8f1957bae Mon Sep 17 00:00:00 2001 From: RyuKwanKon Date: Tue, 2 Jan 2024 21:34:43 +0900 Subject: [PATCH 1/5] =?UTF-8?q?[feat]=20#13=20=ED=82=A4=EC=9B=8C=EB=93=9C?= =?UTF-8?q?=20=EA=B8=B0=EC=A4=80=20search=20api=20controller=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/post/controller/PostController.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/gachon/checkmate/domain/post/controller/PostController.java b/src/main/java/org/gachon/checkmate/domain/post/controller/PostController.java index 7641ba2..42f6465 100644 --- a/src/main/java/org/gachon/checkmate/domain/post/controller/PostController.java +++ b/src/main/java/org/gachon/checkmate/domain/post/controller/PostController.java @@ -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") @@ -24,7 +19,15 @@ public class PostController { public ResponseEntity> searchTextPost(@UserId final Long userId, @RequestParam final String text, final Pageable pageable) { - final List responseDto = postService.searchTextPost(userId, text, pageable); + final PostSearchResponseDto responseDto = postService.searchTextPost(userId, text, pageable); + return SuccessResponse.ok(responseDto); + } + + @GetMapping("/search/{key}") + public ResponseEntity> searchKeyWordPost(@UserId final Long userId, + @PathVariable final String key, + final Pageable pageable) { + final PostSearchResponseDto responseDto = postService.searchKeyWordPost(userId, key, pageable); return SuccessResponse.ok(responseDto); } } From 758bc95cb303fee19801884eee2efaf403242029 Mon Sep 17 00:00:00 2001 From: RyuKwanKon Date: Tue, 2 Jan 2024 21:35:51 +0900 Subject: [PATCH 2/5] =?UTF-8?q?[feat]=20#13=20key=20word=20=EA=B8=B0?= =?UTF-8?q?=EC=A4=80=20post=20=EA=B2=80=EC=83=89=20=EC=BF=BC=EB=A6=AC=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/PostQuerydslRepository.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gachon/checkmate/domain/post/repository/PostQuerydslRepository.java b/src/main/java/org/gachon/checkmate/domain/post/repository/PostQuerydslRepository.java index e754184..5546c8f 100644 --- a/src/main/java/org/gachon/checkmate/domain/post/repository/PostQuerydslRepository.java +++ b/src/main/java/org/gachon/checkmate/domain/post/repository/PostQuerydslRepository.java @@ -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; @@ -23,6 +24,32 @@ public class PostQuerydslRepository { private final JPAQueryFactory queryFactory; + public Page searchKeyPost(ImportantKeyType importantKeyType, Pageable pageable) { + List 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 countQuery = queryFactory + .selectFrom(post); + return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchCount); + } + public Page searchTextPost(String text, Pageable pageable) { List content = queryFactory .select(new QPostSearchDto( @@ -37,8 +64,8 @@ public Page 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()) @@ -48,4 +75,16 @@ public Page 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()); + } } From 3810ca08b3ea6cb7a1df05f11fac267a4c6d0d98 Mon Sep 17 00:00:00 2001 From: RyuKwanKon Date: Tue, 2 Jan 2024 21:36:46 +0900 Subject: [PATCH 3/5] =?UTF-8?q?[feat]=20#13=20key=20word=20=EA=B8=B0?= =?UTF-8?q?=EC=A4=80=20post=20=EA=B2=80=EC=83=89=20api=20service=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/post/service/PostService.java | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/gachon/checkmate/domain/post/service/PostService.java b/src/main/java/org/gachon/checkmate/domain/post/service/PostService.java index fa226ac..32b2898 100644 --- a/src/main/java/org/gachon/checkmate/domain/post/service/PostService.java +++ b/src/main/java/org/gachon/checkmate/domain/post/service/PostService.java @@ -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; @@ -28,20 +31,28 @@ public class PostService { private final CheckListRepository checkListRepository; private final PostQuerydslRepository postQuerydslRepository; - public List searchTextPost(Long userId, String text, Pageable pageable) { + public PostSearchResponseDto searchKeyWordPost(Long userId, String key, Pageable pageable) { CheckList checkList = getCheckList(userId); - Page postSearchDtoList = getPostSearchDto(text, pageable); - return createPostSearchResponseDto(postSearchDtoList, checkList); + ImportantKeyType importantKeyType = EnumValueUtils.toEntityCode(ImportantKeyType.class, key); + Page postSearchList = getKeySearchResults(importantKeyType, pageable); + List searchResults = createPostSearchResponseDto(postSearchList, checkList); + return PostSearchResponseDto.of(searchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements()); } - private List createPostSearchResponseDto(Page postSearchDtoList, CheckList checkList) { + public PostSearchResponseDto searchTextPost(Long userId, String text, Pageable pageable) { + CheckList checkList = getCheckList(userId); + Page postSearchList = getTextSearchResults(text, pageable); + List searchResults = createPostSearchResponseDto(postSearchList, checkList); + return PostSearchResponseDto.of(searchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements()); + } + + private List createPostSearchResponseDto(Page 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()); } @@ -64,7 +75,11 @@ private int getRemainDate(LocalDate endDate) { return (int) endDate.until(LocalDate.now(), ChronoUnit.DAYS); } - private Page getPostSearchDto(String text, Pageable pageable) { + private Page getKeySearchResults(ImportantKeyType importantKeyType, Pageable pageable) { + return postQuerydslRepository.searchKeyPost(importantKeyType, pageable); + } + + private Page getTextSearchResults(String text, Pageable pageable) { return postQuerydslRepository.searchTextPost(text, pageable); } From ddb8b5ae4a90d629413c05cff0dd85e91ddb2177 Mon Sep 17 00:00:00 2001 From: RyuKwanKon Date: Tue, 2 Jan 2024 21:37:27 +0900 Subject: [PATCH 4/5] =?UTF-8?q?[feat]=20#13=20post=20=EA=B2=80=EC=83=89=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=EA=B3=B5=ED=86=B5=20elements=20response?= =?UTF-8?q?=20dto=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PostSearchElementResponseDto.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/org/gachon/checkmate/domain/post/dto/response/PostSearchElementResponseDto.java diff --git a/src/main/java/org/gachon/checkmate/domain/post/dto/response/PostSearchElementResponseDto.java b/src/main/java/org/gachon/checkmate/domain/post/dto/response/PostSearchElementResponseDto.java new file mode 100644 index 0000000..bf801d3 --- /dev/null +++ b/src/main/java/org/gachon/checkmate/domain/post/dto/response/PostSearchElementResponseDto.java @@ -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(); + } +} From 05fbbe826e78514695ad1c43dcb37c76f76358b8 Mon Sep 17 00:00:00 2001 From: RyuKwanKon Date: Tue, 2 Jan 2024 21:37:56 +0900 Subject: [PATCH 5/5] =?UTF-8?q?[feat]=20#13=20post=20=EA=B2=80=EC=83=89=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=EC=B5=9C=EC=A2=85=20response=20dto=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/PostSearchResponseDto.java | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/gachon/checkmate/domain/post/dto/response/PostSearchResponseDto.java b/src/main/java/org/gachon/checkmate/domain/post/dto/response/PostSearchResponseDto.java index 3bc2399..5beca5f 100644 --- a/src/main/java/org/gachon/checkmate/domain/post/dto/response/PostSearchResponseDto.java +++ b/src/main/java/org/gachon/checkmate/domain/post/dto/response/PostSearchResponseDto.java @@ -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 results, + int totalPages, + long totalElements) { + public static PostSearchResponseDto of(List 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(); } }