Skip to content

Commit

Permalink
Merge pull request #28 from KNU-HAEDAL/issue/#25
Browse files Browse the repository at this point in the history
Issue/#25
  • Loading branch information
momnpa333 authored Jun 1, 2024
2 parents 2d100fe + 3a9471b commit 7016643
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
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.ChallengeReviewWithChallenge;
import org.haedal.zzansuni.domain.challengegroup.review.ChallengeReviewModel.ChallengeReviewWithUserInfo;
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 All @@ -22,68 +25,56 @@
@RestController
@RequiredArgsConstructor
public class ChallengeReviewController {

private final ChallengeService challengeService;


@Operation(summary = "챌린지 그룹 최근 리뷰 페이징", description = "챌린지 최근 리뷰 페이징 조회.")
@GetMapping("/api/challengeGroups/reviews")
public ApiResponse<PagingResponse<ChallengeReviewRes.ChallengeReviewDto>> getChallengeReviews(
@Valid PagingRequest pagingRequest
@Valid PagingRequest pagingRequest
//TODO SORTING
) {
return ApiResponse.success(
PagingResponse.<ChallengeReviewRes.ChallengeReviewDto>builder()
.hasNext(false)
.totalPage(1)
.data(List.of(
new ChallengeReviewRes.ChallengeReviewDto(
1L, "title",
new UserRes.UserDto(
1L, "nickname", "https://picsum.photos/200/300", new UserRes.TierInfoDto(
"tier", 100, 50
)),
"content", 12

)
))
.build()
Page<ChallengeReviewWithUserInfo> page = challengeService.getChallengeReviews(
pagingRequest.toPageable());

PagingResponse<ChallengeReviewRes.ChallengeReviewDto> response = PagingResponse.from(
page, ChallengeReviewRes.ChallengeReviewDto::from
);

return ApiResponse.success(response);

}


@Operation(summary = "챌린지 그룹 리뷰 페이징", description = "챌린지 그룹 하위의 모든 챌린지 리뷰 페이징 조회.")
@GetMapping("/api/challengeGroups/{challengeGroupId}/reviews")
public ApiResponse<PagingResponse<ChallengeReviewRes.ChallengeReviewWithChalengeDto>> getChallengeReviewsPaging(
@PathVariable Long challengeGroupId,
@Valid PagingRequest pagingRequest
//TODO SORTING
@PathVariable Long challengeGroupId,
@Valid PagingRequest pagingRequest
//TODO SORTING
) {
return ApiResponse.success(PagingResponse.<ChallengeReviewRes.ChallengeReviewWithChalengeDto>builder()
.hasNext(false)
.totalPage(1)
.data(List.of(
new ChallengeReviewRes.ChallengeReviewWithChalengeDto(
1L, "title",12,
new UserRes.UserDto(
1L, "nickname", "https://picsum.photos/200/300", new UserRes.TierInfoDto(
"tier", 100, 50
)),
"content", 12

)
))
.build());
Page<ChallengeReviewWithChallenge> page = challengeService.getChallengeReviewsByGroupId(
challengeGroupId, pagingRequest.toPageable());

PagingResponse<ChallengeReviewRes.ChallengeReviewWithChalengeDto> response = PagingResponse.from(
page, ChallengeReviewRes.ChallengeReviewWithChalengeDto::from
);

return ApiResponse.success(response);

}

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "챌린지 리뷰 작성", description = "챌린지 리뷰를 작성한다.")
@PostMapping("/api/challenges/{challengeId}/reviews")
public ApiResponse<Long> challengeReviewCreate(
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser,
@RequestBody ChallengeReq.ChallengeReviewCreateRequest request
@PathVariable Long challengeId,
@AuthenticationPrincipal JwtUser jwtUser,
@RequestBody ChallengeReq.ChallengeReviewCreateRequest request
) {
Long response = challengeService.createReview(request.toCommand(), challengeId,
jwtUser.getId());
jwtUser.getId());
return ApiResponse.success(response, "챌린지 리뷰 작성에 성공하였습니다.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,56 @@

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

public class ChallengeReviewRes {


@Builder
public record ChallengeReviewDto(
Long challengeId,
String challengeTitle,
UserRes.UserDto user,
String content,
Integer rating
Long challengeId,
String challengeTitle,
UserRes.UserDto user,
String content,
Integer rating
) {

public static ChallengeReviewDto from(
ChallengeReviewWithUserInfo challengeReviewWithUserInfo) {
var user = UserRes.UserDto.from(challengeReviewWithUserInfo.user());
return ChallengeReviewDto.builder()
.challengeId(challengeReviewWithUserInfo.challengeId())
.challengeTitle(challengeReviewWithUserInfo.challengeTitle())
.user(user)
.content(challengeReviewWithUserInfo.content())
.rating(challengeReviewWithUserInfo.rating())
.build();
}

}

@Builder
public record ChallengeReviewWithChalengeDto(
Long challengeId,
String challengeTitle,
Integer challengeDifficulty,
UserRes.UserDto user,
String content,
Integer rating
Long challengeId,
String challengeTitle,
Integer challengeDifficulty,
UserRes.UserDto user,
String content,
Integer rating
) {

public static ChallengeReviewWithChalengeDto from(
ChallengeReviewWithChallenge challengeReviewWithChallenge) {
var user = UserRes.UserDto.from(challengeReviewWithChallenge.user());
return ChallengeReviewWithChalengeDto.builder()
.challengeId(challengeReviewWithChallenge.challengeId())
.challengeTitle(challengeReviewWithChallenge.challengeTitle())
.challengeDifficulty(challengeReviewWithChallenge.challengeDifficulty())
.user(user)
.content(challengeReviewWithChallenge.content())
.rating(challengeReviewWithChallenge.rating())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel.ChallengeRecord;
import org.haedal.zzansuni.domain.challengegroup.review.ChallengeReview;
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.domain.challengegroup.review.ChallengeReviewReader;
import org.haedal.zzansuni.domain.challengegroup.review.ChallengeReviewStore;
import org.haedal.zzansuni.domain.challengegroup.verification.ChallengeVerification;
import org.haedal.zzansuni.domain.challengegroup.verification.ChallengeVerificationModel;
import org.haedal.zzansuni.domain.challengegroup.verification.ChallengeVerificationReader;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallenge;
import org.haedal.zzansuni.domain.challengegroup.userchallenge.UserChallengeReader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -65,17 +70,41 @@ public Long createReview(ChallengeCommand.ReviewCreate command, Long challengeId
UserChallenge userChallenge = userChallengeReader.getByUserIdAndChallengeId(userId,
challengeId);
Long challengeGroupId = userChallenge
.getChallenge()
.getChallengeGroup()
.getId();
.getChallenge()
.getChallengeGroup()
.getId();

//이미 리뷰를 작성했는지 확인
challengeReviewReader.findByUserChallengeId(challengeId)
challengeReviewReader.findByUserChallengeId(userChallenge.getId())
.ifPresent(review -> {
throw new IllegalArgumentException("이미 리뷰를 작성했습니다.");
});
ChallengeReview challengeReview = ChallengeReview.create(userChallenge, command, challengeGroupId);
ChallengeReview challengeReview = ChallengeReview.create(userChallenge, command,
challengeGroupId);
challengeReviewStore.store(challengeReview);
return challengeReview.getId();
}

/**
* groupId로 챌린지 리뷰 가져오기
*/
@Transactional(readOnly = true)
public Page<ChallengeReviewModel.ChallengeReviewWithChallenge> getChallengeReviewsByGroupId(
Long challengeGroupId, Pageable pageable) {
Page<ChallengeReview> challengeReviewPage = challengeReviewReader.getChallengeReviewPageByChallengeGroupId(
challengeGroupId, pageable);

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

/**
* 챌린지 리뷰 가져오기
*/
@Transactional(readOnly = true)
public Page<ChallengeReviewWithUserInfo> getChallengeReviews(Pageable pageable) {
Page<ChallengeReview> challengeReviewPage = challengeReviewReader.getChallengeReviewPage(
pageable);

return challengeReviewPage.map(ChallengeReviewWithUserInfo::from);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.haedal.zzansuni.domain.challengegroup.review;

import lombok.Builder;
import org.haedal.zzansuni.domain.user.UserModel;

public class ChallengeReviewModel {

@Builder
public record ChallengeReviewWithUserInfo(
Long challengeId,
String challengeTitle,
UserModel user,
String content,
Integer rating
) {

public static ChallengeReviewWithUserInfo from(ChallengeReview challengeReview) {
var userModel = UserModel.from(challengeReview.getUserChallenge().getUser());
return ChallengeReviewWithUserInfo.builder()
.challengeId(challengeReview.getUserChallenge().getChallenge().getId())
.challengeTitle(
challengeReview.getUserChallenge().getChallenge().getChallengeGroup()
.getTitle())
.user(userModel)
.content(challengeReview.getContent())
.rating(challengeReview.getRating())
.build();
}
}

@Builder
public record ChallengeReviewWithChallenge(
Long challengeId,
String challengeTitle,
Integer challengeDifficulty,
UserModel user,
String content,
Integer rating
) {

public static ChallengeReviewWithChallenge from(ChallengeReview challengeReview) {
var userModel = UserModel.from(challengeReview.getUserChallenge().getUser());
return ChallengeReviewWithChallenge.builder()
.challengeId(challengeReview.getUserChallenge().getChallenge().getId())
.challengeTitle(
challengeReview.getUserChallenge().getChallenge().getChallengeGroup()
.getTitle())
.challengeDifficulty(
challengeReview.getUserChallenge().getChallenge().getDifficulty())
.user(userModel)
.content(challengeReview.getContent())
.rating(challengeReview.getRating())
.build();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
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 ChallengeReviewReader {

ChallengeReview getByChallengeId(Long challengeId);
ChallengeReview getByUserChallengeId(Long userChallengeId);

Optional<ChallengeReview> findByUserChallengeId(Long challengeId);
Optional<ChallengeReview> findByUserChallengeId(Long userChallengeId);

Map<Long, Boolean> getReviewWrittenMapByUserChallengeId(List<Long> userChallengeIds);

Page<ChallengeReview> getChallengeReviewPageByChallengeGroupId(Long challengeGroupId,
Pageable pageable);

Page<ChallengeReview> getChallengeReviewPage(Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public Page<ChallengeModel.ChallengeCurrent> getCurrentChallenges(Long userId,
/**
* 완료한 챌린지 페이징 조회
*/
@Transactional(readOnly = true)
public Page<ChallengeModel.ChallengeComplete> getCompleteChallenges(Long userId,
Pageable pageable) {
Page<UserChallenge> userChallengePage = userChallengeReader
Expand Down
Loading

0 comments on commit 7016643

Please sign in to comment.