Skip to content

Commit

Permalink
[Feat] 페이징 그룹 목록 페이징 api 구현완료 (#22-1)
Browse files Browse the repository at this point in the history
  • Loading branch information
bayy1216 committed Jun 1, 2024
1 parent 40d78fc commit 68f5c76
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChallengeModel> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public class ChallengeGroupQueryService {
private final ChallengeGroupImageReader challengeGroupImageReader;

@Transactional(readOnly = true)
public Page<ChallengeGroupModel.Detail> getChallengeGroupsPaging(Pageable pageable, ChallengeCategory category) {
throw new UnsupportedOperationException("Not implemented yet");
public Page<ChallengeGroupModel.Info> getChallengeGroupsPaging(Pageable pageable, ChallengeCategory category) {
Page<ChallengeGroup> challengeGroups = challengeGroupReader.getChallengeGroupsPagingByCategory(pageable, category);
return challengeGroups.map(ChallengeGroupModel.Info::from);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ChallengeGroup> getChallengeGroupsPagingByCategory(Pageable pageable, ChallengeCategory category);
}
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -24,4 +32,23 @@ public ChallengeGroup getByIdWithChallenges(Long challengeGroupId) {
.findByIdWithChallenges(challengeGroupId)
.orElseThrow(NoSuchElementException::new);
}

@Override
public Page<ChallengeGroup> getChallengeGroupsPagingByCategory(Pageable pageable, ChallengeCategory category) {
Long count = queryFactory
.select(QChallengeGroup.challengeGroup.count())
.from(QChallengeGroup.challengeGroup)
.where(QChallengeGroup.challengeGroup.category.eq(category))
.fetchOne();

List<ChallengeGroup> 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);
}
}

0 comments on commit 68f5c76

Please sign in to comment.