diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupController.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupController.java index 82612ba..5799b18 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupController.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupController.java @@ -13,6 +13,7 @@ import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupQueryService; import org.haedal.zzansuni.domain.challengegroup.DayType; import org.haedal.zzansuni.global.jwt.JwtUser; +import org.springframework.data.domain.Pageable; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -34,15 +35,9 @@ public ApiResponse> getChall @Valid PagingRequest pagingRequest, @RequestParam ChallengeCategory category ) { - return ApiResponse.success(PagingResponse.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.getChallengeGroupsPaging(pagingRequest.toPageable(), category); + var response = PagingResponse.from(page, ChallengeGroupRes.ChallengeGroupDto::from); + return ApiResponse.success(response); } @Operation(summary = "챌린지 그룹 상세 조회", description = "챌린지 상세 조회한다.") diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupRes.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupRes.java index 6f3552a..30bcb88 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupRes.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupRes.java @@ -24,6 +24,32 @@ public record ChallengeGroupDto( ChallengeCategory category ) { + public static ChallengeGroupDto from( + ChallengeGroupModel.Detail challengeGroupDetail + ){ + return ChallengeGroupDto.builder() + .id(challengeGroupDetail.getId()) + .title(challengeGroupDetail.getTitle()) + .content(challengeGroupDetail.getContent()) + .participantCount(challengeGroupDetail.getCumulativeCount()) + .startDate(challengeGroupDetail.getMinStartDate()) + .endDate(challengeGroupDetail.getMaxEndDate()) + .category(challengeGroupDetail.getCategory()) + .build(); + } + public static ChallengeGroupDto from( + ChallengeGroupModel.Info challengeGroupInfo + ){ + return ChallengeGroupDto.builder() + .id(challengeGroupInfo.getId()) + .title(challengeGroupInfo.getTitle()) + .content(challengeGroupInfo.getContent()) + .participantCount(challengeGroupInfo.getCumulativeCount()) + .startDate(challengeGroupInfo.getMinStartDate()) + .endDate(challengeGroupInfo.getMaxEndDate()) + .category(challengeGroupInfo.getCategory()) + .build(); + } } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupModel.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupModel.java index 5aa44f7..68ff7d0 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupModel.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupModel.java @@ -28,6 +28,48 @@ public static Main from(ChallengeGroup challengeGroup) { } } + @Getter + @Builder + public static class Info { + private Long id; + private ChallengeCategory category; + private String title; + private String content; + private String guide; + private Integer cumulativeCount; + private List challenges; + + public LocalDate getMinStartDate() { + return challenges.stream() + .map(ChallengeModel::getStartDate) + .min(LocalDate::compareTo) + .orElse(LocalDate.now()); + } + + public LocalDate getMaxEndDate() { + return challenges.stream() + .map(ChallengeModel::getEndDate) + .max(LocalDate::compareTo) + .orElse(LocalDate.now()); + } + + public static Info from(ChallengeGroup challengeGroup) { + var challenges = challengeGroup.getChallenges().stream() + .map(ChallengeModel::from) + .toList(); + return Info.builder() + .id(challengeGroup.getId()) + .category(challengeGroup.getCategory()) + .title(challengeGroup.getTitle()) + .content(challengeGroup.getContent()) + .guide(challengeGroup.getGuide()) + .cumulativeCount(challengeGroup.getCumulativeCount()) + .challenges(challenges) + .build(); + } + } + + @Getter @Builder public static class Detail { diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java index 9e538bc..13a9d87 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java @@ -1,6 +1,8 @@ package org.haedal.zzansuni.domain.challengegroup; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -12,6 +14,12 @@ public class ChallengeGroupQueryService { private final ChallengeGroupReader challengeGroupReader; private final ChallengeGroupImageReader challengeGroupImageReader; + @Transactional(readOnly = true) + public Page getChallengeGroupsPaging(Pageable pageable, ChallengeCategory category) { + Page challengeGroups = challengeGroupReader.getChallengeGroupsPagingByCategory(pageable, category); + return challengeGroups.map(ChallengeGroupModel.Info::from); + } + @Transactional(readOnly = true) public ChallengeGroupModel.Detail getChallengeGroupDetail(Long challengeGroupId) { ChallengeGroup challengeGroup = challengeGroupReader.getByIdWithChallenges(challengeGroupId); diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java index a37352f..cf36a1b 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java @@ -1,7 +1,12 @@ package org.haedal.zzansuni.domain.challengegroup; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + public interface ChallengeGroupReader { ChallengeGroup getById(Long challengeGroupId); ChallengeGroup getByIdWithChallenges(Long challengeGroupId); + + Page getChallengeGroupsPagingByCategory(Pageable pageable, ChallengeCategory category); } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java index af9e2c5..5c9bbea 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java @@ -1,15 +1,23 @@ package org.haedal.zzansuni.infrastructure.challengegroup; +import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; +import org.haedal.zzansuni.domain.challengegroup.ChallengeCategory; import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup; import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupReader; +import org.haedal.zzansuni.domain.challengegroup.QChallengeGroup; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; +import java.util.List; import java.util.NoSuchElementException; @Component @RequiredArgsConstructor public class ChallengeGroupReaderImpl implements ChallengeGroupReader { + private final JPAQueryFactory queryFactory; private final ChallengeGroupRepository challengeGroupRepository; @Override public ChallengeGroup getById(Long challengeGroupId) { @@ -24,4 +32,23 @@ public ChallengeGroup getByIdWithChallenges(Long challengeGroupId) { .findByIdWithChallenges(challengeGroupId) .orElseThrow(NoSuchElementException::new); } + + @Override + public Page getChallengeGroupsPagingByCategory(Pageable pageable, ChallengeCategory category) { + Long count = queryFactory + .select(QChallengeGroup.challengeGroup.count()) + .from(QChallengeGroup.challengeGroup) + .where(QChallengeGroup.challengeGroup.category.eq(category)) + .fetchOne(); + + List page = queryFactory + .selectFrom(QChallengeGroup.challengeGroup) + .where(QChallengeGroup.challengeGroup.category.eq(category)) + .leftJoin(QChallengeGroup.challengeGroup.challenges).fetchJoin() + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .fetch(); + + return new PageImpl<>(page, pageable, count == null ? 0 : count); + } }