Skip to content

Commit

Permalink
Merge pull request #24 from KNU-HAEDAL/issue/#5-4
Browse files Browse the repository at this point in the history
[Feat]: 완료된 챌린지 페이징 조회
  • Loading branch information
momnpa333 authored Jun 1, 2024
2 parents c5816d9 + e0d3ec0 commit 2b343fe
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import lombok.extern.slf4j.Slf4j;
import org.haedal.zzansuni.controller.PagingRequest;
import org.haedal.zzansuni.controller.PagingResponse;
import org.haedal.zzansuni.controller.challengegroup.challenge.ChallengeRes.ChallengeCompleteResponse;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.domain.ImageUploader;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel.ChallengeComplete;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel.ChallengeCurrent;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeService;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallengeService;
Expand Down Expand Up @@ -104,10 +105,16 @@ public ApiResponse<PagingResponse<ChallengeRes.ChallengeCurrentResponse>> getCha

@Operation(summary = "완료한 챌린지 조회", description = "완료한 챌린지 페이징 조회한다.")
@GetMapping("/api/user/challenges/completes")
public ApiResponse<PagingResponse<ChallengeRes.ChallengeCompleteDto>> getChallengeCompletesPaging(
public ApiResponse<PagingResponse<ChallengeCompleteResponse>> getChallengeCompletesPaging(
@Valid PagingRequest pagingRequest,
@AuthenticationPrincipal JwtUser jwtUser
) {
throw new RuntimeException("Not implemented");
Page<ChallengeComplete> page = userChallengeService.getCompleteChallenges(
jwtUser.getId(), pagingRequest.toPageable());

PagingResponse<ChallengeCompleteResponse> response = PagingResponse.from(
page, ChallengeRes.ChallengeCompleteResponse::from
);
return ApiResponse.success(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.LocalDateTime;
import java.util.List;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel.ChallengeComplete;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel.ChallengeCurrent;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel.ChallengeRecord;
import org.haedal.zzansuni.domain.challengegroup.verification.ChallengeVerificationModel;
Expand Down Expand Up @@ -105,16 +106,22 @@ public static ChallengeCurrentResponse from(ChallengeCurrent challengeCurrent) {
}

@Builder
public record ChallengeCompleteDto(
public record ChallengeCompleteResponse(
Long id,
String title,

LocalDate successDate,

ChallengeCategory category,
Boolean reviewWritten

) {

public static ChallengeCompleteResponse from(ChallengeComplete challengeComplete) {
return ChallengeCompleteResponse.builder()
.id(challengeComplete.challengeId())
.title(challengeComplete.title())
.successDate(challengeComplete.successDate())
.category(challengeComplete.category())
.reviewWritten(challengeComplete.reviewWritten())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,32 @@ public static ChallengeCurrent from(UserChallenge userChallenge, Boolean reviewW

}

@Builder
public record ChallengeComplete(
Long challengeId,
String title,
LocalDate successDate,
ChallengeCategory category,
Boolean reviewWritten
) {

public static ChallengeComplete from(UserChallenge userChallenge, Boolean reviewWritten
) {
Challenge challenge = userChallenge.getChallenge();
return ChallengeComplete.builder()
.challengeId(challenge.getId())
.title(challenge.getChallengeGroup().getTitle())
// 성공한 날짜는 가장 최근에 인증한 날짜로 설정
.successDate(userChallenge.getChallengeVerifications().stream()
.map(ChallengeVerification::getCreatedAt)
.max(LocalDateTime::compareTo)
.map(LocalDateTime::toLocalDate)
.orElse(null))
.category(challenge.getChallengeGroup().getCategory())
.reviewWritten(reviewWritten)
.build();
}

}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.haedal.zzansuni.domain.challengegroup.userchallenge;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -18,5 +16,7 @@ public interface UserChallengeReader {

Optional<UserChallenge> findByUserIdAndChallengeId(Long userId, Long challengeId);

Page<UserChallenge> getPageByUserId(Long userId, Pageable pageable);
Page<UserChallenge> getCurrentChallengePageByUserId(Long userId, Pageable pageable);

Page<UserChallenge> getCompletedChallengePageByUserId(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ChallengeModel.ChallengeVerificationResult verification(
public Page<ChallengeModel.ChallengeCurrent> getCurrentChallenges(Long userId,
Pageable pageable) {
Page<UserChallenge> userChallengePage = userChallengeReader
.getPageByUserId(userId, pageable);
.getCurrentChallengePageByUserId(userId, pageable);

List<Long> userChallengeIds = userChallengePage.map(UserChallenge::getId).getContent();
Map<Long, Boolean> reviewWrittenMap = challengeReviewReader
Expand All @@ -86,4 +86,20 @@ public Page<ChallengeModel.ChallengeCurrent> getCurrentChallenges(Long userId,
return userChallengePage.map(
m -> ChallengeModel.ChallengeCurrent.from(m, reviewWrittenMap.get(m.getId())));
}

/**
* 완료한 챌린지 페이징 조회
*/
public Page<ChallengeModel.ChallengeComplete> getCompleteChallenges(Long userId,
Pageable pageable) {
Page<UserChallenge> userChallengePage = userChallengeReader
.getCompletedChallengePageByUserId(userId, pageable);

List<Long> userChallengeIds = userChallengePage.map(UserChallenge::getId).getContent();
Map<Long, Boolean> reviewWrittenMap = challengeReviewReader
.getReviewWrittenMapByUserChallengeId(userChallengeIds);

return userChallengePage.map(
m -> ChallengeModel.ChallengeComplete.from(m, reviewWrittenMap.get(m.getId())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Optional<UserChallenge> findByUserIdAndChallengeId(Long userId, Long chal
* 해당 방법을 통해 연관된 엔터티를 모두 가져와 N+1 문제를 해결한다.
*/
@Override
public Page<UserChallenge> getPageByUserId(Long userId, Pageable pageable) {
public Page<UserChallenge> getCurrentChallengePageByUserId(Long userId, Pageable pageable) {
Long count = queryFactory
.select(QUserChallenge.userChallenge.count())
.from(QUserChallenge.userChallenge)
Expand All @@ -80,11 +80,44 @@ public Page<UserChallenge> getPageByUserId(Long userId, Pageable pageable) {
return new PageImpl<>(userChallenges, pageable, count == null ? 0 : count);
}

/**
* userId로 현재 완료중인 챌린지 조회 <p> 페이징 처리를 하면서, userChallenge에서 이와 연관되어 있는 challenge, challengeGroup,
* challengeVerifications를 가져온다.
* <p>
* 해당 방법을 통해 연관된 엔터티를 모두 가져와 N+1 문제를 해결한다.
*/
@Override
public Page<UserChallenge> getCompletedChallengePageByUserId(Long userId, Pageable pageable) {
Long count = queryFactory
.select(QUserChallenge.userChallenge.count())
.from(QUserChallenge.userChallenge)
.where(eqUserId(userId), eqProceeding())
.fetchOne();
List<UserChallenge> userChallenges = queryFactory
.selectFrom(QUserChallenge.userChallenge)
.where(eqUserId(userId), neProceeding())
.join(QUserChallenge.userChallenge.challenge).fetchJoin() // manyToOne 관계
.join(QUserChallenge.userChallenge.challenge.challengeGroup)
.fetchJoin() // manyToOne의 manyToOne 관계 엔티티를 가져옴
.leftJoin(QUserChallenge.userChallenge.challengeVerifications)
.fetchJoin() // oneToMany 관계
.orderBy(QUserChallenge.userChallenge.id.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

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

private BooleanExpression eqUserId(Long userId) {
return QUserChallenge.userChallenge.user.id.eq(userId);
}

private BooleanExpression eqProceeding() {
return QUserChallenge.userChallenge.status.eq(ChallengeStatus.PROCEEDING);
}

private BooleanExpression neProceeding() {
return QUserChallenge.userChallenge.status.ne(ChallengeStatus.PROCEEDING);
}
}

0 comments on commit 2b343fe

Please sign in to comment.