Skip to content

Commit

Permalink
Merge pull request #31 from KNU-HAEDAL/issue/#29
Browse files Browse the repository at this point in the history
[Feat]: 구현 완료
  • Loading branch information
momnpa333 authored Jun 1, 2024
2 parents 03d9c59 + 9994787 commit aae67d6
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.haedal.zzansuni.controller.user.UserRes;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeService;
import org.haedal.zzansuni.domain.challengegroup.review.ChallengeReviewModel;
import org.haedal.zzansuni.domain.challengegroup.review.ChallengeReviewModel.ChallengeReviewWithChallenge;
import org.haedal.zzansuni.domain.challengegroup.review.ChallengeReviewModel.ChallengeReviewWithUserInfo;
import org.haedal.zzansuni.global.jwt.JwtUser;
Expand Down Expand Up @@ -78,4 +79,14 @@ public ApiResponse<Long> challengeReviewCreate(
return ApiResponse.success(response, "챌린지 리뷰 작성에 성공하였습니다.");
}

@ResponseStatus(HttpStatus.OK)
@Operation(summary = "챌린지 그룹별 리뷰 평점 조회", description = "챌린지 그룹별 리뷰 평점 조회")
@GetMapping("/api/challengeGroups/{challengeGroupId}/reviews/score")
public ApiResponse<ChallengeReviewRes.ChallengeReviewScoreResponse> getChallengeGroupReviewScore(
@PathVariable Long challengeGroupId
) {
var response = ChallengeReviewRes.ChallengeReviewScoreResponse.from(
challengeService.getChallengeGroupReviewScore(challengeGroupId));
return ApiResponse.success(response, "챌린지 그룹별 리뷰 평점 조회에 성공하였습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.haedal.zzansuni.controller.challengegroup.review;

import java.util.Map;
import lombok.Builder;
import org.haedal.zzansuni.controller.user.UserRes;
import org.haedal.zzansuni.domain.challengegroup.review.ChallengeReviewModel;
import org.haedal.zzansuni.domain.challengegroup.review.ChallengeReviewModel.ChallengeReviewWithChallenge;
import org.haedal.zzansuni.domain.challengegroup.review.ChallengeReviewModel.ChallengeReviewWithUserInfo;

Expand Down Expand Up @@ -54,4 +56,23 @@ public static ChallengeReviewWithChalengeDto from(
.build();
}
}

@Builder
public record ChallengeReviewScoreResponse(
Float averageRating,
/**
* key: rating, value: count
*/
Map<Integer, Integer> ratingCount
) {

public static ChallengeReviewScoreResponse from(
ChallengeReviewModel.ChallengeReviewScore challengeReviewScore) {
return ChallengeReviewScoreResponse.builder()
.averageRating(challengeReviewScore.averageRating())
.ratingCount(challengeReviewScore.ratingCount())
.build();
}
}

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

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
Expand Down Expand Up @@ -36,6 +38,7 @@ public VerificationCreate afterUpload(String imageUrl) {
@Getter
@Builder
public static class VerificationCreate extends SelfValidating<Verificate> {

@NotBlank(message = "내용은 필수입니다.")
private final String content;
private final String imageUrl;
Expand All @@ -55,13 +58,17 @@ public static class ReviewCreate extends SelfValidating<ReviewCreate> {
private final String content;

@NotNull(message = "평점은 필수입니다.")
@Min(1)
@Max(5)
private final Integer rating;

public ReviewCreate(String content, Integer rating) {
this.content = content;
this.rating = rating;
this.validateSelf();
}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,15 @@ public Page<ChallengeReviewWithUserInfo> getChallengeReviews(Pageable pageable)

return challengeReviewPage.map(ChallengeReviewWithUserInfo::from);
}

/**
* 챌린지 그룹 리뷰 평점 가져오기
*/
@Transactional(readOnly = true)
public ChallengeReviewModel.ChallengeReviewScore getChallengeGroupReviewScore(
Long challengeGroupId) {
List<ChallengeReview> challengeReviews = challengeReviewReader.findByChallengeGroupId(
challengeGroupId);
return ChallengeReviewModel.ChallengeReviewScore.of(challengeReviews);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.haedal.zzansuni.domain.challengegroup.review;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Builder;
import org.haedal.zzansuni.domain.user.UserModel;

Expand Down Expand Up @@ -55,4 +58,33 @@ public static ChallengeReviewWithChallenge from(ChallengeReview challengeReview)

}

@Builder
public record ChallengeReviewScore(
Float averageRating,
Map<Integer, Integer> ratingCount
) {

public static ChallengeReviewScore of(List<ChallengeReview> challengeReviews) {
// key: rating, value: count
// rating은 1,2,3,4,5 이며 value는 각각의 rating이 몇개인지 count
Map<Integer, Integer> ratingCount = new HashMap<>();
for (int i = 1; i <= 5; i++) {
ratingCount.put(i, 0);
}
challengeReviews.forEach(challengeReview -> {
var rating = challengeReview.getRating();
ratingCount.put(rating, ratingCount.get(rating) + 1);
});

var averageRating = challengeReviews.stream()
.mapToInt(ChallengeReview::getRating)
.average()
.orElse(0);
return ChallengeReviewScore.builder()
.averageRating((float) averageRating)
.ratingCount(ratingCount)
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ Page<ChallengeReview> getChallengeReviewPageByChallengeGroupId(Long challengeGro

Page<ChallengeReview> getChallengeReviewPage(Pageable pageable);

List<ChallengeReview> findByChallengeGroupId(Long challengeGroupId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,13 @@ public Page<ChallengeReview> getChallengeReviewPage(Pageable pageable) {

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

@Override
public List<ChallengeReview> findByChallengeGroupId(Long challengeGroupId) {
return queryFactory
.select(QChallengeReview.challengeReview)
.from(QChallengeReview.challengeReview)
.where(QChallengeReview.challengeReview.challengeGroupId.eq(challengeGroupId))
.fetch();
}
}

0 comments on commit aae67d6

Please sign in to comment.