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 98aa49f..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 @@ -37,6 +37,19 @@ public static ChallengeGroupDto from( .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 9251b2c..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 @@ -15,8 +15,9 @@ public class ChallengeGroupQueryService { private final ChallengeGroupImageReader challengeGroupImageReader; @Transactional(readOnly = true) - public Page getChallengeGroupsPaging(Pageable pageable, ChallengeCategory category) { - throw new UnsupportedOperationException("Not implemented yet"); + public Page getChallengeGroupsPaging(Pageable pageable, ChallengeCategory category) { + Page challengeGroups = challengeGroupReader.getChallengeGroupsPagingByCategory(pageable, category); + return challengeGroups.map(ChallengeGroupModel.Info::from); } @Transactional(readOnly = true) 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); + } }