Skip to content

Commit

Permalink
Merge pull request #26 from KNU-HAEDAL/issue/#22-1
Browse files Browse the repository at this point in the history
Issue/#22 1
  • Loading branch information
bayy1216 authored Jun 1, 2024
2 parents f1da284 + 68f5c76 commit d1cf003
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,15 +35,9 @@ public ApiResponse<PagingResponse<ChallengeGroupRes.ChallengeGroupDto>> getChall
@Valid PagingRequest pagingRequest,
@RequestParam ChallengeCategory category
) {
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.getChallengeGroupsPaging(pagingRequest.toPageable(), category);
var response = PagingResponse.from(page, ChallengeGroupRes.ChallengeGroupDto::from);
return ApiResponse.success(response);
}

@Operation(summary = "챌린지 그룹 상세 조회", description = "챌린지 상세 조회한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

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
@@ -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;

Expand All @@ -12,6 +14,12 @@ public class ChallengeGroupQueryService {
private final ChallengeGroupReader challengeGroupReader;
private final ChallengeGroupImageReader challengeGroupImageReader;

@Transactional(readOnly = true)
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)
public ChallengeGroupModel.Detail getChallengeGroupDetail(Long challengeGroupId) {
ChallengeGroup challengeGroup = challengeGroupReader.getByIdWithChallenges(challengeGroupId);
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 d1cf003

Please sign in to comment.