Skip to content

Commit

Permalink
Merge pull request #21 from KNU-HAEDAL/issue/#5-3
Browse files Browse the repository at this point in the history
[Feat]: 진행중인 챌린지 조회 구현 완료
  • Loading branch information
momnpa333 authored Jun 1, 2024
2 parents f4440be + 4e883e6 commit f89237f
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
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.ChallengeCurrent;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeService;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallengeService;
import org.haedal.zzansuni.global.jwt.JwtUser;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -102,8 +104,13 @@ public ApiResponse<PagingResponse<ChallengeRes.ChallengeCurrentResponse>> getCha
@Valid PagingRequest pagingRequest,
@AuthenticationPrincipal JwtUser jwtUser
) {
// challengeService.getChallengeCurrentsPaging(1L, pagingRequest);
throw new RuntimeException("Not implemented");
Page<ChallengeCurrent> page = userChallengeService.getCurrentChallenges(
jwtUser.getId(), pagingRequest.toPageable());

PagingResponse<ChallengeRes.ChallengeCurrentResponse> response = PagingResponse.from(
page, ChallengeRes.ChallengeCurrentResponse::from
);
return ApiResponse.success(response);
}

@Operation(summary = "완료한 챌린지 조회", description = "완료한 챌린지 페이징 조회한다.")
Expand Down
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.ChallengeCurrent;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel.ChallengeRecord;
import org.haedal.zzansuni.domain.challengegroup.challengeverification.ChallengeVerificationModel;

Expand Down Expand Up @@ -88,6 +89,19 @@ public record ChallengeCurrentResponse(

) {

public static ChallengeCurrentResponse from(ChallengeCurrent challengeCurrent) {
return ChallengeCurrentResponse.builder()
.challengeId(challengeCurrent.challengeId())
.title(challengeCurrent.title())
.successCount(challengeCurrent.successCount())
.totalCount(challengeCurrent.totalCount())
.participationDate(challengeCurrent.participationDate())
.startDate(challengeCurrent.startDate())
.endDate(challengeCurrent.endDate())
.category(challengeCurrent.category())
.reviewWritten(challengeCurrent.reviewWritten())
.build();
}
}

@Builder
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.haedal.zzansuni.domain.challengegroup.challenge;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Builder;
import lombok.Getter;
import org.haedal.zzansuni.domain.challengegroup.ChallengeCategory;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.haedal.zzansuni.domain.challengegroup.DayType;
import org.haedal.zzansuni.domain.challengegroup.challengeverification.ChallengeVerification;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;

@Getter
@Builder
Expand Down Expand Up @@ -76,5 +79,34 @@ public static ChallengeRecord from(Challenge challenge, ChallengeGroup challenge
}
}

@Builder
public record ChallengeCurrent(
Long challengeId,
String title,
Integer totalCount,
Integer successCount,
LocalDateTime participationDate,
LocalDate startDate,
LocalDate endDate,
ChallengeCategory category,
Boolean reviewWritten
) {

public static ChallengeCurrent from(UserChallenge userChallenge, Boolean reviewWritten) {
Challenge challenge = userChallenge.getChallenge();
return ChallengeCurrent.builder()
.challengeId(challenge.getId())
.title(challenge.getChallengeGroup().getTitle())
.totalCount(challenge.getRequiredCount())
.successCount(userChallenge.getChallengeVerifications().size())
.participationDate(userChallenge.getCreatedAt())
.startDate(challenge.getStartDate())
.endDate(challenge.getEndDate())
.category(challenge.getChallengeGroup().getCategory())
.reviewWritten(reviewWritten)
.build();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public ChallengeVerificationModel getChallengeRecordDetail(Long recordId) {
*/
@Transactional
public Long createReview(ChallengeCommand.ReviewCreate command, Long challengeId, Long userId) {
UserChallenge userChallenge = userChallengeReader.getByUserIdAndChallengeId(userId,
challengeId);
UserChallenge userChallenge = userChallengeReader.findByUserIdAndChallengeId(userId,
challengeId).orElseThrow(() -> new IllegalArgumentException("현재 참여중인 챌린지가 아닙니다."));

//이미 리뷰를 작성했는지 확인
challengeReviewReader.findByUserChallengeId(challengeId)
Expand All @@ -74,4 +74,6 @@ public Long createReview(ChallengeCommand.ReviewCreate command, Long challengeId
challengeReviewStore.store(challengeReview);
return challengeReview.getId();
}


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

import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface ChallengeReviewReader {
Expand All @@ -8,4 +10,5 @@ public interface ChallengeReviewReader {

Optional<ChallengeReview> findByUserChallengeId(Long challengeId);

Map<Long, Boolean> getReviewWrittenMapByUserChallengeId(List<Long> userChallengeIds);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
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;

public interface UserChallengeReader {

UserChallenge getById(Long id);

UserChallenge getByIdWithVerificationAndChallenge(Long id);

Optional<UserChallenge> findById(Long id);

UserChallenge getByUserIdAndChallengeId(Long userId, Long challengeId);

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

Page<UserChallenge> getPageByUserId(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package org.haedal.zzansuni.domain.challengegroup.userchallenge;

import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeReader;
import org.haedal.zzansuni.domain.challengegroup.challengereview.ChallengeReviewReader;
import org.haedal.zzansuni.domain.user.User;
import org.haedal.zzansuni.domain.user.UserReader;
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 @@ -18,6 +23,7 @@ public class UserChallengeService {

private final UserChallengeStore userChallengeStore;
private final UserChallengeReader userChallengeReader;
private final ChallengeReviewReader challengeReviewReader;
private final UserReader userReader;
private final ChallengeReader challengeReader;

Expand Down Expand Up @@ -49,18 +55,35 @@ public void participateChallenge(Long userId, Long challengeId) {
*/
@Transactional
public ChallengeModel.ChallengeVerificationResult verification(
Long userChallengeId,
ChallengeCommand.VerificationCreate command
Long userChallengeId,
ChallengeCommand.VerificationCreate command
) {
UserChallenge userChallenge = userChallengeReader.getByIdWithVerificationAndChallenge(userChallengeId);
UserChallenge userChallenge = userChallengeReader.getByIdWithVerificationAndChallenge(
userChallengeId);
userChallenge.addChallengeVerification(command);


// 챌린지 RequiredCount 가져오기 위해 챌린지 정보 가져온다
Challenge challenge = userChallenge.getChallenge();


return ChallengeModel.ChallengeVerificationResult
.of(challenge.getRequiredCount(), userChallenge.getChallengeVerifications().size(), challenge.getOnceExp());
.of(challenge.getRequiredCount(), userChallenge.getChallengeVerifications().size(),
challenge.getOnceExp());
}

/**
* 진행중인 챌린지 페이징 조회
*/
@Transactional(readOnly = true)
public Page<ChallengeModel.ChallengeCurrent> getCurrentChallenges(Long userId,
Pageable pageable) {
Page<UserChallenge> userChallengePage = userChallengeReader
.getPageByUserId(userId, pageable);

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

return userChallengePage.map(
m -> ChallengeModel.ChallengeCurrent.from(m, reviewWrittenMap.get(m.getId())));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.haedal.zzansuni.infrastructure.challengegroup.challenge;

import org.haedal.zzansuni.controller.challengegroup.challenge.ChallengeRes.ChallengeCurrentResponse;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCurrentDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ChallengeRepository extends JpaRepository<Challenge, Long> {

// /**
// * userId로 현재 진행중인 챌린지 조회 <br> userId로 챌린지를 가져온다.
// */
// @Query(
// "SELECT new org.haedal.zzansuni.infrastructure.challengegroup.challenge.dto.ChallengeCurrentDto("
// + "c.id, c.title, ,"
// + " c.startDate, c.endDate,"
// + " c.participantCount, c.participantLimit, c.status, c.user.id, c.user.nickname, c.user.profileImage) "
// + "FROM Challenge c WHERE c.user.id = :userId")
// Page<ChallengeCurrentDto> findAllChallengeCurrent(Long userId, Pageable pageable);

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.haedal.zzansuni.infrastructure.challengegroup.challengereview;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,4 +27,23 @@ public Optional<ChallengeReview> findByUserChallengeId(Long challengeId) {
return challengeReviewRepository.findByUserChallengeId(challengeId);
}

@Override
public Map<Long, Boolean> getReviewWrittenMapByUserChallengeId(List<Long> userChallengeIds) {
List<ChallengeReview> byUserChallengeIdIn = challengeReviewRepository.findByUserChallengeIdIn(
userChallengeIds);
// 리뷰가 있으면 map에 true, 없으면 false로 Map 생성

//1. userChallengeIds를 순회하면서 reviewWrittenMap에 false로 초기화
Map<Long, Boolean> reviewWrittenMap =
userChallengeIds.stream()
.collect(HashMap::new, (m, v) -> m.put(v, false), HashMap::putAll);

//2. byUserChallengeIdIn을 순회하면서
// 리뷰가 존재했다면, reviewWrittenMap에 true로 변경
byUserChallengeIdIn.forEach(
review -> reviewWrittenMap.put(review.getUserChallenge().getId(), true));

return reviewWrittenMap;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.haedal.zzansuni.infrastructure.challengegroup.challengereview;

import java.util.List;
import java.util.Optional;
import org.haedal.zzansuni.domain.challengegroup.challengereview.ChallengeReview;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChallengeReviewRepository extends JpaRepository<ChallengeReview, Long> {

Optional<ChallengeReview> findByUserChallengeId(Long userChallengeId);

List<ChallengeReview> findByUserChallengeIdIn(List<Long> userChallengeIds);
}
Loading

0 comments on commit f89237f

Please sign in to comment.