Skip to content

Commit

Permalink
Merge pull request #30 from KNU-HAEDAL/issue/#22-3
Browse files Browse the repository at this point in the history
Issue/#22 3
  • Loading branch information
bayy1216 authored Jun 1, 2024
2 parents 7016643 + 56c608d commit 03d9c59
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,11 @@ public ApiResponse<ChallengeGroupRes.ChallengeGroupRankingPagingResponse> getCha
@PathVariable Long challengeGroupId,
@Valid PagingRequest pagingRequest
) {
return ApiResponse.success(
new ChallengeGroupRes.ChallengeGroupRankingPagingResponse(
List.of(
new ChallengeGroupRes.ChallengeGroupRankingDto(1, 12,
new UserRes.UserDto(
1L, "nickname", "https://picsum.photos/200/300", new UserRes.TierInfoDto(
"tier", 100, 50

)
)
)
),
1,
new ChallengeGroupRes.ChallengeGroupRankingDto(
1, 12,
new UserRes.UserDto(
1L, "nickname", "https://picsum.photos/200/300", new UserRes.TierInfoDto(
"tier", 100, 50
)
)
)
));
var rankingPage = challengeGroupQueryService.getChallengeGroupRankingsPaging(challengeGroupId, pagingRequest.toPageable());
var rankingModel = challengeGroupQueryService.getChallengeGroupRanking(challengeGroupId, jwtUser.getId());
var response = ChallengeGroupRes.ChallengeGroupRankingPagingResponse
.from(rankingPage, rankingModel);
return ApiResponse.success(response);
}

//숏폼 조회
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupModel;
import org.haedal.zzansuni.domain.challengegroup.DayType;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.springframework.data.domain.Page;

import java.time.LocalDate;
import java.util.List;
Expand Down Expand Up @@ -135,6 +136,19 @@ public record ChallengeGroupRankingPagingResponse(
Integer totalPage,
ChallengeGroupRankingDto myRanking //null이면 랭킹이 없는 것
) {
public static ChallengeGroupRankingPagingResponse from(
Page<ChallengeGroupModel.Ranking> rankingPage,
ChallengeGroupModel.Ranking myRanking
){
var data = rankingPage.getContent().stream()
.map(ChallengeGroupRankingDto::from)
.toList();
return ChallengeGroupRankingPagingResponse.builder()
.data(data)
.totalPage(rankingPage.getTotalPages())
.myRanking(ChallengeGroupRankingDto.from(myRanking))
.build();
}

}

Expand All @@ -146,6 +160,16 @@ public record ChallengeGroupRankingDto(
Integer acquiredPoint,
UserRes.UserDto user
) {
public static ChallengeGroupRankingDto from(
ChallengeGroupModel.Ranking model
){
var user = UserRes.UserDto.from(model.getUser());
return ChallengeGroupRankingDto.builder()
.ranking(model.getRank())
.acquiredPoint(model.getAccumulatedPoint())
.user(user)
.build();
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ public ApiResponse<Void> challengeParticipation(
@Operation(summary = "챌린지 인증", description = "챌린지에 인증한다.")
@PostMapping("/api/challenges/{userChallengeId}/verification")
public ApiResponse<ChallengeRes.ChallengeVerificationResponse> challengeVerification(
@AuthenticationPrincipal JwtUser jwtUser,
@PathVariable Long userChallengeId,
@RequestPart("body") ChallengeReq.ChallengeVerificationRequest request,
@RequestPart("image") MultipartFile image
) {
ChallengeCommand.Verificate command = request.toCommand(image);
String imageUrl = imageUploader.upload(command.getImage());
ChallengeCommand.VerificationCreate afterUpload = command.afterUpload(imageUrl);
var model = userChallengeService.verification(userChallengeId, afterUpload);
var model = userChallengeService.verification(userChallengeId, jwtUser.getId(), afterUpload);
var response = ChallengeRes.ChallengeVerificationResponse.from(model);
return ApiResponse.success(response, "챌린지 인증에 성공하였습니다.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Builder;
import lombok.Getter;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.haedal.zzansuni.domain.user.UserModel;

import java.time.LocalDate;
import java.util.List;
Expand Down Expand Up @@ -116,4 +117,12 @@ public static Detail from(ChallengeGroup challengeGroup, List<ChallengeGroupImag
}
}

@Getter
@Builder
public static class Ranking {
private UserModel user;
private Integer rank;
private Integer accumulatedPoint;
}

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

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.user.UserModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
Expand All @@ -32,4 +35,32 @@ public Page<ChallengeGroupModel.Info> getChallengeGroupsShortsPaging(Pageable pa
Page<ChallengeGroup> challengeGroups = challengeGroupReader.getChallengeGroupsShortsPaging(pageable, userId);
return challengeGroups.map(ChallengeGroupModel.Info::from);
}

public Page<ChallengeGroupModel.Ranking> getChallengeGroupRankingsPaging(Long challengeGroupId, Pageable pageable) {
Page<ChallengeGroupUserExp> challengeGroupUserExps
= challengeGroupReader.getByChallengeGroupId(challengeGroupId, pageable);

return getRankingPage(pageable, challengeGroupUserExps);
}

private static PageImpl<ChallengeGroupModel.Ranking> getRankingPage(Pageable pageable, Page<ChallengeGroupUserExp> challengeGroupUserExps) {
List<ChallengeGroupModel.Ranking> rankings = new ArrayList<>();
for(int i = 0; i < challengeGroupUserExps.getContent().size(); i++) {
Integer rank = challengeGroupUserExps.getNumber() * challengeGroupUserExps.getSize() + 1 + i;
ChallengeGroupUserExp challengeGroupUserExp = challengeGroupUserExps.getContent().get(i);
var rankingModel = ChallengeGroupModel.Ranking.builder()
.user(UserModel.from(challengeGroupUserExp.getUser()))
.accumulatedPoint(challengeGroupUserExp.getTotalExp())
.rank(rank)
.build();
rankings.add(rankingModel);
}
return new PageImpl<>(rankings, pageable, challengeGroupUserExps.getTotalElements());
}

public ChallengeGroupModel.Ranking getChallengeGroupRanking(Long challengeGroupId, Long id) {
return challengeGroupReader.getRanking(challengeGroupId, id);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Optional;

public interface ChallengeGroupReader {
ChallengeGroup getById(Long challengeGroupId);

Expand All @@ -11,4 +13,10 @@ public interface ChallengeGroupReader {
Page<ChallengeGroup> getChallengeGroupsPagingByCategory(Pageable pageable, ChallengeCategory category);

Page<ChallengeGroup> getChallengeGroupsShortsPaging(Pageable pageable, Long userId);

Optional<ChallengeGroupUserExp> findByChallengeGroupIdAndUserId(Long challengeGroupId, Long userId);

Page<ChallengeGroupUserExp> getByChallengeGroupId(Long challengeGroupId, Pageable pageable);

ChallengeGroupModel.Ranking getRanking(Long challengeGroupId, Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.haedal.zzansuni.domain.challengegroup;

import jakarta.persistence.*;
import lombok.*;
import org.haedal.zzansuni.domain.user.User;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public class ChallengeGroupUserExp {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "challenge_group_id")
private ChallengeGroup challengeGroup;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

private Integer totalExp;

public static ChallengeGroupUserExp create(ChallengeGroup challengeGroup, User user) {
return ChallengeGroupUserExp.builder()
.challengeGroup(challengeGroup)
.user(user)
.totalExp(0)
.build();
}

public void addExp(Integer exp) {
this.totalExp += exp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.haedal.zzansuni.domain.challengegroup;

public interface ChallengeGroupUserExpStore {
ChallengeGroupUserExp store(ChallengeGroupUserExp challengeGroupUserExp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public static UserChallenge create(Challenge challenge, User user) {
public void addChallengeVerification(ChallengeCommand.VerificationCreate command) {
ChallengeVerification challengeVerification = ChallengeVerification.create(command, this);
this.challengeVerifications.add(challengeVerification);
user.addExp(challenge.getOnceExp());

// 만약 챌린지 인증 참여횟수와 필요참여획수가 같으면 챌린지 완료로 변경
if(this.challengeVerifications.size() == this.challenge.getRequiredCount()) {
user.addExp(challenge.getSuccessExp());
complete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupReader;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupUserExp;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupUserExpStore;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeCommand;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
Expand All @@ -26,7 +29,8 @@ public class UserChallengeService {
private final ChallengeReviewReader challengeReviewReader;
private final UserReader userReader;
private final ChallengeReader challengeReader;

private final ChallengeGroupReader challengeGroupReader;
private final ChallengeGroupUserExpStore challengeGroupUserExpStore;
/**
* 챌린지 참여하기 1. 유저와 챌린지 정보를 받아서 UserChallenge 테이블에 데이터 추가
*/
Expand Down Expand Up @@ -56,11 +60,29 @@ public void participateChallenge(Long userId, Long challengeId) {
@Transactional
public ChallengeModel.ChallengeVerificationResult verification(
Long userChallengeId,
Long userId,
ChallengeCommand.VerificationCreate command
) {
UserChallenge userChallenge = userChallengeReader.getByIdWithVerificationAndChallenge(
userChallengeId);
if(!userChallenge.getUser().getId().equals(userId)) {
throw new IllegalArgumentException("해당 챌린지에 참여한 유저가 아닙니다.");
}

Integer beforeExp = userChallenge.getUser().getExp();
userChallenge.addChallengeVerification(command);
Integer afterExp = userChallenge.getUser().getExp();

// 챌린지 경험치 획득 로직
ChallengeGroupUserExp challengeGroupUserExp = challengeGroupReader
.findByChallengeGroupIdAndUserId(userChallenge.getChallenge().getChallengeGroup().getId(),
userId)
.orElseGet(() -> {
ChallengeGroupUserExp entity = ChallengeGroupUserExp
.create(userChallenge.getChallenge().getChallengeGroup(), userChallenge.getUser());
return challengeGroupUserExpStore.store(entity);
});
challengeGroupUserExp.addExp(afterExp - beforeExp);

// 챌린지 RequiredCount 가져오기 위해 챌린지 정보 가져온다
Challenge challenge = userChallenge.getChallenge();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ public static User createManager(UserCommand.Create command){
public void update(UserCommand.Update userUpdate) {
this.nickname = userUpdate.getNickname();
}

public void addExp(Integer exp){
this.exp += exp;
}
}
Loading

0 comments on commit 03d9c59

Please sign in to comment.