Skip to content

Commit

Permalink
Merge pull request #27 from KNU-HAEDAL/issue/#22-2
Browse files Browse the repository at this point in the history
[Feat] 페이징 쇼츠 api 구현완료 (#22-2)
  • Loading branch information
bayy1216 authored Jun 1, 2024
2 parents d1cf003 + 923f517 commit 2d100fe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,13 @@ public ApiResponse<ChallengeGroupRes.ChallengeGroupRankingPagingResponse> getCha
@Operation(summary = "챌린지 그룹 숏폼 페이징", description = "챌린지 숏폼 페이징 조회한다.")
@GetMapping("/api/challengeGroups/shorts")
public ApiResponse<PagingResponse<ChallengeGroupRes.ChallengeGroupDto>> getChallengeShortsPaging(
@RequestParam Long page
@Valid PagingRequest pagingRequest,
@AuthenticationPrincipal JwtUser jwtUser
) {
return ApiResponse.success(
PagingResponse.<ChallengeGroupRes.ChallengeGroupDto>builder()
.hasNext(false)
.totalPage(1)
.data(List.of(
new ChallengeGroupRes.ChallengeGroupDto(1L,
"title", "thumbnailUrl", 12,
LocalDate.now(), LocalDate.now(), ChallengeCategory.VOLUNTEER)
))
.build()
);
var page = challengeGroupQueryService.getChallengeGroupsShortsPaging(pagingRequest.toPageable(), jwtUser.getId());
var response = PagingResponse.from(page, ChallengeGroupRes.ChallengeGroupDto::from);

return ApiResponse.success(response);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public ChallengeGroupModel.Detail getChallengeGroupDetail(Long challengeGroupId)
List<ChallengeGroupImage> challengeGroupImages = challengeGroupImageReader.getByChallengeGroupId(challengeGroupId);
return ChallengeGroupModel.Detail.from(challengeGroup, challengeGroupImages);
}

@Transactional(readOnly = true)
public Page<ChallengeGroupModel.Info> getChallengeGroupsShortsPaging(Pageable pageable, Long userId) {
Page<ChallengeGroup> challengeGroups = challengeGroupReader.getChallengeGroupsShortsPaging(pageable, userId);
return challengeGroups.map(ChallengeGroupModel.Info::from);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface ChallengeGroupReader {
ChallengeGroup getByIdWithChallenges(Long challengeGroupId);

Page<ChallengeGroup> getChallengeGroupsPagingByCategory(Pageable pageable, ChallengeCategory category);

Page<ChallengeGroup> getChallengeGroupsShortsPaging(Pageable pageable, Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.haedal.zzansuni.infrastructure.challengegroup;

import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberTemplate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.ChallengeCategory;
Expand Down Expand Up @@ -51,4 +55,29 @@ public Page<ChallengeGroup> getChallengeGroupsPagingByCategory(Pageable pageable

return new PageImpl<>(page, pageable, count == null ? 0 : count);
}

@Override
public Page<ChallengeGroup> getChallengeGroupsShortsPaging(Pageable pageable, Long userId) {
Double seed = Double.valueOf(userId);

// 해시 값을 기준으로 정렬
NumberTemplate<Double> hashOrder = Expressions
.numberTemplate(Double.class, "SHA1(CONCAT({0}, id))", seed);


Long count = queryFactory
.select(QChallengeGroup.challengeGroup.count())
.from(QChallengeGroup.challengeGroup)
.fetchOne();
List<ChallengeGroup> page = queryFactory
.selectFrom(QChallengeGroup.challengeGroup)
.leftJoin(QChallengeGroup.challengeGroup.challenges).fetchJoin()
.orderBy(Expressions.numberTemplate(Double.class, "RAND({0})", userId).asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

return new PageImpl<>(page, pageable, count == null ? 0 : count);
}

}

0 comments on commit 2d100fe

Please sign in to comment.