From 1c2783f979a253e1cb8300d0386b141252eed77d Mon Sep 17 00:00:00 2001 From: Sanghoon Jeong <67852689+wjdtkdgns@users.noreply.github.com> Date: Wed, 16 Aug 2023 16:19:29 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EA=B2=80=EC=83=89=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20response=EC=97=90=20=EC=B4=9D=20=EA=B2=80=EC=83=89?= =?UTF-8?q?=20=EA=B2=B0=EA=B3=BC=20=EA=B0=9C=EC=88=98=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20(#107)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [feat] 검색 response page로 전환 #106 * [chore] spotless 적용 #106 --- .../api/auth/service/WithdrawUserUseCase.java | 3 +++ .../server/api/common/page/PageResponse.java | 23 ++++++++++++++++ .../model/dto/response/SearchResponse.java | 16 +++++------- .../service/SearchArchivingUseCase.java | 20 +++++++------- .../server/domain/common/util/PageUtil.java | 26 +++++++++++++++++++ .../archiving/adaptor/ArchivingAdaptor.java | 5 ++-- .../repository/ArchivingCustomRepository.java | 5 ++-- .../ArchivingCustomRepositoryImpl.java | 19 ++++++++------ 8 files changed, 86 insertions(+), 31 deletions(-) create mode 100644 Api/src/main/java/allchive/server/api/common/page/PageResponse.java create mode 100644 Domain/src/main/java/allchive/server/domain/common/util/PageUtil.java diff --git a/Api/src/main/java/allchive/server/api/auth/service/WithdrawUserUseCase.java b/Api/src/main/java/allchive/server/api/auth/service/WithdrawUserUseCase.java index 8a51428c..28aaf10f 100644 --- a/Api/src/main/java/allchive/server/api/auth/service/WithdrawUserUseCase.java +++ b/Api/src/main/java/allchive/server/api/auth/service/WithdrawUserUseCase.java @@ -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; @@ -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) { @@ -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); } } diff --git a/Api/src/main/java/allchive/server/api/common/page/PageResponse.java b/Api/src/main/java/allchive/server/api/common/page/PageResponse.java new file mode 100644 index 00000000..1617d1c5 --- /dev/null +++ b/Api/src/main/java/allchive/server/api/common/page/PageResponse.java @@ -0,0 +1,23 @@ +package allchive.server.api.common.page; + + +import java.util.List; +import org.springframework.data.domain.Page; + +public record PageResponse( + List content, + int page, + int size, + long totalElements, + int totalPages, + boolean hasNextPage) { + public static PageResponse of(Page page) { + return new PageResponse<>( + page.getContent(), + page.getNumber(), + page.getNumberOfElements(), + page.getTotalElements(), + page.getTotalPages(), + page.hasNext()); + } +} diff --git a/Api/src/main/java/allchive/server/api/search/model/dto/response/SearchResponse.java b/Api/src/main/java/allchive/server/api/search/model/dto/response/SearchResponse.java index 7d9c7f78..bafccfef 100644 --- a/Api/src/main/java/allchive/server/api/search/model/dto/response/SearchResponse.java +++ b/Api/src/main/java/allchive/server/api/search/model/dto/response/SearchResponse.java @@ -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 archivings; - SliceResponse community; + PageResponse archivings; + PageResponse community; @Builder private SearchResponse( - SliceResponse archivings, - SliceResponse community) { + PageResponse archivings, PageResponse community) { this.archivings = archivings; this.community = community; } public static SearchResponse forAll( - SliceResponse archivings, - SliceResponse community) { + PageResponse archivings, PageResponse community) { return SearchResponse.builder().archivings(archivings).community(community).build(); } - public static SearchResponse forMy(SliceResponse archivings) { + public static SearchResponse forMy(PageResponse archivings) { return SearchResponse.builder().archivings(archivings).community(null).build(); } - public static SearchResponse forCommunity(SliceResponse community) { + public static SearchResponse forCommunity(PageResponse community) { return SearchResponse.builder().archivings(null).community(community).build(); } } diff --git a/Api/src/main/java/allchive/server/api/search/service/SearchArchivingUseCase.java b/Api/src/main/java/allchive/server/api/search/service/SearchArchivingUseCase.java index ddb55a13..168ca036 100644 --- a/Api/src/main/java/allchive/server/api/search/service/SearchArchivingUseCase.java +++ b/Api/src/main/java/allchive/server/api/search/service/SearchArchivingUseCase.java @@ -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; @@ -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 @@ -43,8 +43,8 @@ public SearchResponse execute(Pageable pageable, ArchivingType type, String word validateExecution(word); Long userId = SecurityUtil.getCurrentUserId(); Set tagArchivingIds = getTagArchivingIds(userId, word); - SliceResponse my; - SliceResponse community; + PageResponse my; + PageResponse community; renewalLatestSearch(userId, word); switch (type) { case ALL -> { @@ -93,23 +93,23 @@ private boolean isExistSearchKeyword(List searches, String keyword .isEmpty(); } - private SliceResponse getMyArchivings( + private PageResponse getMyArchivings( Long userId, String keyword, Pageable pageable, Set tagArchivingIds) { - Slice archivingSlices = + Page archivingPages = archivingAdaptor .querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds( userId, keyword, pageable, tagArchivingIds) .map(archiving -> ArchivingResponse.of(archiving, Boolean.FALSE)); - return SliceResponse.of(archivingSlices); + return PageResponse.of(archivingPages); } - private SliceResponse getCommunityArchivings( + private PageResponse getCommunityArchivings( Long userId, String keyword, Pageable pageable, Set tagArchivingIds) { List archivingIdList = scrapAdaptor.findAllByUserId(userId).stream().map(Scrap::getArchivingId).toList(); List blockList = blockAdaptor.findByBlockFrom(userId).stream().map(Block::getBlockUser).toList(); - Slice archivingSlices = + Page archivingSlices = archivingAdaptor .querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds( archivingIdList, @@ -119,6 +119,6 @@ private SliceResponse getCommunityArchivings( tagArchivingIds, userId) .map(archiving -> ArchivingResponse.of(archiving, Boolean.FALSE)); - return SliceResponse.of(archivingSlices); + return PageResponse.of(archivingSlices); } } diff --git a/Domain/src/main/java/allchive/server/domain/common/util/PageUtil.java b/Domain/src/main/java/allchive/server/domain/common/util/PageUtil.java new file mode 100644 index 00000000..c725c81f --- /dev/null +++ b/Domain/src/main/java/allchive/server/domain/common/util/PageUtil.java @@ -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 Page toPage(QueryResults results, Pageable pageable) { + boolean hasNext = hasNext(results.getResults(), pageable); + return new PageImpl<>( + hasNext ? getContent(results.getResults(), pageable) : results.getResults(), + pageable, + results.getTotal()); + } + + // 다음 페이지 있는지 확인 + private static boolean hasNext(List content, Pageable pageable) { + return pageable.isPaged() && content.size() > pageable.getPageSize(); + } + + // 데이터 1개 빼고 반환 + private static List getContent(List content, Pageable pageable) { + return content.subList(0, pageable.getPageSize()); + } +} diff --git a/Domain/src/main/java/allchive/server/domain/domains/archiving/adaptor/ArchivingAdaptor.java b/Domain/src/main/java/allchive/server/domain/domains/archiving/adaptor/ArchivingAdaptor.java index baf05a7b..bf008e5f 100644 --- a/Domain/src/main/java/allchive/server/domain/domains/archiving/adaptor/ArchivingAdaptor.java +++ b/Domain/src/main/java/allchive/server/domain/domains/archiving/adaptor/ArchivingAdaptor.java @@ -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; @@ -74,13 +75,13 @@ public void deleteAllById(List archivingIds) { archivingRepository.deleteAllById(archivingIds); } - public Slice querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds( + public Page querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds( Long userId, String keyword, Pageable pageable, Set tagArchivingIds) { return archivingRepository.querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds( userId, keyword, pageable, tagArchivingIds); } - public Slice querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds( + public Page querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds( List archivingIdList, List blockList, String keyword, diff --git a/Domain/src/main/java/allchive/server/domain/domains/archiving/repository/ArchivingCustomRepository.java b/Domain/src/main/java/allchive/server/domain/domains/archiving/repository/ArchivingCustomRepository.java index af10a2e7..90ff7449 100644 --- a/Domain/src/main/java/allchive/server/domain/domains/archiving/repository/ArchivingCustomRepository.java +++ b/Domain/src/main/java/allchive/server/domain/domains/archiving/repository/ArchivingCustomRepository.java @@ -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; @@ -21,10 +22,10 @@ Slice querySliceArchivingByIdIn( boolean queryArchivingExistById(Long archivingId); - Slice querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds( + Page querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds( Long userId, String keyword, Pageable pageable, Set tagArchivingIds); - Slice querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds( + Page querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds( List archivingIdList, List blockList, String keyword, diff --git a/Domain/src/main/java/allchive/server/domain/domains/archiving/repository/ArchivingCustomRepositoryImpl.java b/Domain/src/main/java/allchive/server/domain/domains/archiving/repository/ArchivingCustomRepositoryImpl.java index e55765c3..a5934c30 100644 --- a/Domain/src/main/java/allchive/server/domain/domains/archiving/repository/ArchivingCustomRepositoryImpl.java +++ b/Domain/src/main/java/allchive/server/domain/domains/archiving/repository/ArchivingCustomRepositoryImpl.java @@ -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; @@ -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; @@ -95,9 +98,9 @@ public boolean queryArchivingExistById(Long archivingId) { } @Override - public Slice querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds( + public Page querySliceArchivingByUserIdAndKeywordsOrderByTagArchvingIds( Long userId, String keyword, Pageable pageable, Set tagArchivingIds) { - List archivings = + QueryResults results = queryFactory .selectFrom(archiving) .where( @@ -107,19 +110,19 @@ public Slice 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 querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds( + public Page querySliceArchivingByKeywordExceptBlockOrderByTagArchvingIds( List archivingIdList, List blockList, String keyword, Pageable pageable, Set tagArchivingIds, Long userId) { - List archivings = + QueryResults archivings = queryFactory .select(archiving) .from(archiving) @@ -135,8 +138,8 @@ public Slice querySliceArchivingByKeywordExceptBlockOrderByTagArchvin createdAtDesc()) .offset(pageable.getOffset()) .limit(pageable.getPageSize() + PLUS_ONE) - .fetch(); - return SliceUtil.toSlice(archivings, pageable); + .fetchResults(); + return PageUtil.toPage(archivings, pageable); } @Override