Skip to content

Commit

Permalink
[feat] 검색 결과 response에 총 검색 결과 개수 추가 (#107)
Browse files Browse the repository at this point in the history
* [feat] 검색 response page로 전환 #106

* [chore] spotless 적용 #106
  • Loading branch information
wjdtkdgns authored Aug 16, 2023
1 parent 26be511 commit 1c2783f
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import allchive.server.core.error.exception.InvalidOauthProviderException;
import allchive.server.domain.domains.archiving.adaptor.ArchivingAdaptor;
import allchive.server.domain.domains.archiving.domain.Archiving;
import allchive.server.domain.domains.archiving.service.ArchivingDomainService;
import allchive.server.domain.domains.block.service.BlockDomainService;
import allchive.server.domain.domains.content.adaptor.TagAdaptor;
import allchive.server.domain.domains.content.domain.Tag;
Expand Down Expand Up @@ -43,6 +44,7 @@ public class WithdrawUserUseCase {
private final RecycleDomainService recycleDomainService;
private final ReportDomainService reportDomainService;
private final UserDomainService userDomainService;
private final ArchivingDomainService archivingDomainService;

@Transactional
public void execute(String appleAccessToken, String referer) {
Expand Down Expand Up @@ -94,6 +96,7 @@ private void withdrawService(Long userId, User user) {
contentDomainService.deleteAllByArchivingIdIn(archivingId);
recycleDomainService.deleteAllByUserId(userId);
reportDomainService.deleteAllByReportedUserId(userId);
archivingDomainService.deleteAllById(archivingId);
userDomainService.deleteUserById(userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package allchive.server.api.common.page;


import java.util.List;
import org.springframework.data.domain.Page;

public record PageResponse<T>(
List<T> content,
int page,
int size,
long totalElements,
int totalPages,
boolean hasNextPage) {
public static <T> PageResponse<T> of(Page<T> page) {
return new PageResponse<>(
page.getContent(),
page.getNumber(),
page.getNumberOfElements(),
page.getTotalElements(),
page.getTotalPages(),
page.hasNext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@


import allchive.server.api.archiving.model.dto.response.ArchivingResponse;
import allchive.server.api.common.slice.SliceResponse;
import allchive.server.api.common.page.PageResponse;
import lombok.Builder;
import lombok.Getter;

@Getter
public class SearchResponse {
SliceResponse<ArchivingResponse> archivings;
SliceResponse<ArchivingResponse> community;
PageResponse<ArchivingResponse> archivings;
PageResponse<ArchivingResponse> community;

@Builder
private SearchResponse(
SliceResponse<ArchivingResponse> archivings,
SliceResponse<ArchivingResponse> community) {
PageResponse<ArchivingResponse> archivings, PageResponse<ArchivingResponse> community) {
this.archivings = archivings;
this.community = community;
}

public static SearchResponse forAll(
SliceResponse<ArchivingResponse> archivings,
SliceResponse<ArchivingResponse> community) {
PageResponse<ArchivingResponse> archivings, PageResponse<ArchivingResponse> community) {
return SearchResponse.builder().archivings(archivings).community(community).build();
}

public static SearchResponse forMy(SliceResponse<ArchivingResponse> archivings) {
public static SearchResponse forMy(PageResponse<ArchivingResponse> archivings) {
return SearchResponse.builder().archivings(archivings).community(null).build();
}

public static SearchResponse forCommunity(SliceResponse<ArchivingResponse> community) {
public static SearchResponse forCommunity(PageResponse<ArchivingResponse> community) {
return SearchResponse.builder().archivings(null).community(community).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import allchive.server.api.archiving.model.dto.response.ArchivingResponse;
import allchive.server.api.common.slice.SliceResponse;
import allchive.server.api.common.page.PageResponse;
import allchive.server.api.common.util.StringParamUtil;
import allchive.server.api.config.security.SecurityUtil;
import allchive.server.api.search.model.dto.response.SearchResponse;
Expand All @@ -23,8 +23,8 @@
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.transaction.annotation.Transactional;

@UseCase
Expand All @@ -43,8 +43,8 @@ public SearchResponse execute(Pageable pageable, ArchivingType type, String word
validateExecution(word);
Long userId = SecurityUtil.getCurrentUserId();
Set<Long> tagArchivingIds = getTagArchivingIds(userId, word);
SliceResponse<ArchivingResponse> my;
SliceResponse<ArchivingResponse> community;
PageResponse<ArchivingResponse> my;
PageResponse<ArchivingResponse> community;
renewalLatestSearch(userId, word);
switch (type) {
case ALL -> {
Expand Down Expand Up @@ -93,23 +93,23 @@ private boolean isExistSearchKeyword(List<LatestSearch> searches, String keyword
.isEmpty();
}

private SliceResponse<ArchivingResponse> getMyArchivings(
private PageResponse<ArchivingResponse> getMyArchivings(
Long userId, String keyword, Pageable pageable, Set<Long> tagArchivingIds) {
Slice<ArchivingResponse> archivingSlices =
Page<ArchivingResponse> archivingPages =
archivingAdaptor
.querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds(
userId, keyword, pageable, tagArchivingIds)
.map(archiving -> ArchivingResponse.of(archiving, Boolean.FALSE));
return SliceResponse.of(archivingSlices);
return PageResponse.of(archivingPages);
}

private SliceResponse<ArchivingResponse> getCommunityArchivings(
private PageResponse<ArchivingResponse> getCommunityArchivings(
Long userId, String keyword, Pageable pageable, Set<Long> tagArchivingIds) {
List<Long> archivingIdList =
scrapAdaptor.findAllByUserId(userId).stream().map(Scrap::getArchivingId).toList();
List<Long> blockList =
blockAdaptor.findByBlockFrom(userId).stream().map(Block::getBlockUser).toList();
Slice<ArchivingResponse> archivingSlices =
Page<ArchivingResponse> archivingSlices =
archivingAdaptor
.querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds(
archivingIdList,
Expand All @@ -119,6 +119,6 @@ private SliceResponse<ArchivingResponse> getCommunityArchivings(
tagArchivingIds,
userId)
.map(archiving -> ArchivingResponse.of(archiving, Boolean.FALSE));
return SliceResponse.of(archivingSlices);
return PageResponse.of(archivingSlices);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package allchive.server.domain.common.util;


import com.querydsl.core.QueryResults;
import java.util.List;
import org.springframework.data.domain.*;

public class PageUtil {
public static <T> Page<T> toPage(QueryResults<T> results, Pageable pageable) {
boolean hasNext = hasNext(results.getResults(), pageable);
return new PageImpl<>(
hasNext ? getContent(results.getResults(), pageable) : results.getResults(),
pageable,
results.getTotal());
}

// 다음 페이지 있는지 확인
private static <T> boolean hasNext(List<T> content, Pageable pageable) {
return pageable.isPaged() && content.size() > pageable.getPageSize();
}

// 데이터 1개 빼고 반환
private static <T> List<T> getContent(List<T> content, Pageable pageable) {
return content.subList(0, pageable.getPageSize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

Expand Down Expand Up @@ -74,13 +75,13 @@ public void deleteAllById(List<Long> archivingIds) {
archivingRepository.deleteAllById(archivingIds);
}

public Slice<Archiving> querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds(
public Page<Archiving> querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds(
Long userId, String keyword, Pageable pageable, Set<Long> tagArchivingIds) {
return archivingRepository.querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds(
userId, keyword, pageable, tagArchivingIds);
}

public Slice<Archiving> querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds(
public Page<Archiving> querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds(
List<Long> archivingIdList,
List<Long> blockList,
String keyword,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import allchive.server.domain.domains.archiving.domain.enums.Category;
import java.util.List;
import java.util.Set;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

Expand All @@ -21,10 +22,10 @@ Slice<Archiving> querySliceArchivingByIdIn(

boolean queryArchivingExistById(Long archivingId);

Slice<Archiving> querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds(
Page<Archiving> querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds(
Long userId, String keyword, Pageable pageable, Set<Long> tagArchivingIds);

Slice<Archiving> querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds(
Page<Archiving> querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds(
List<Long> archivingIdList,
List<Long> blockList,
String keyword,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import static allchive.server.core.consts.AllchiveConst.PLUS_ONE;
import static allchive.server.domain.domains.archiving.domain.QArchiving.archiving;

import allchive.server.domain.common.util.PageUtil;
import allchive.server.domain.common.util.SliceUtil;
import allchive.server.domain.domains.archiving.domain.Archiving;
import allchive.server.domain.domains.archiving.domain.enums.Category;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.CaseBuilder;
Expand All @@ -15,6 +17,7 @@
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

Expand Down Expand Up @@ -95,9 +98,9 @@ public boolean queryArchivingExistById(Long archivingId) {
}

@Override
public Slice<Archiving> querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds(
public Page<Archiving> querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds(
Long userId, String keyword, Pageable pageable, Set<Long> tagArchivingIds) {
List<Archiving> archivings =
QueryResults<Archiving> results =
queryFactory
.selectFrom(archiving)
.where(
Expand All @@ -107,19 +110,19 @@ public Slice<Archiving> querySliceArchivingByUserIdAndKeywordsOrderByTagArchving
.orderBy(idIn(tagArchivingIds), createdAtDesc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + PLUS_ONE)
.fetch();
return SliceUtil.toSlice(archivings, pageable);
.fetchResults();
return PageUtil.toPage(results, pageable);
}

@Override
public Slice<Archiving> querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds(
public Page<Archiving> querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds(
List<Long> archivingIdList,
List<Long> blockList,
String keyword,
Pageable pageable,
Set<Long> tagArchivingIds,
Long userId) {
List<Archiving> archivings =
QueryResults<Archiving> archivings =
queryFactory
.select(archiving)
.from(archiving)
Expand All @@ -135,8 +138,8 @@ public Slice<Archiving> querySliceArchivingByKeywordExceptBlockOrderByTagArchvin
createdAtDesc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + PLUS_ONE)
.fetch();
return SliceUtil.toSlice(archivings, pageable);
.fetchResults();
return PageUtil.toPage(archivings, pageable);
}

@Override
Expand Down

0 comments on commit 1c2783f

Please sign in to comment.