Skip to content

Commit

Permalink
Merge pull request #98 from team-Ollie/develop
Browse files Browse the repository at this point in the history
[develop] 새로운 챌린지 참여시 participants 추가, 챌린지 광고 API dto 변경
  • Loading branch information
Haeun-Y authored Jul 1, 2024
2 parents def1f53 + 63735a8 commit 5f780c6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public BaseResponse<List<GetChallengesRes>> getChallenges(@RequestParam(value =
return new BaseResponse<>(challengeService.getChallenges(searchWord));
}

// // 챌린지 광고 조회
// @GetMapping("/ads")
// public BaseResponse<GetChallengeAdsRes> getChallengeAds() {
// return new BaseResponse<>(challengeService.getChallengeAds());
// }
// 챌린지 광고 조회
@GetMapping("/ads")
public BaseResponse<GetChallengeAdsRes> getChallengeAds() {
return new BaseResponse<>(challengeService.getChallengeAds());
}
}
22 changes: 22 additions & 0 deletions src/main/java/ollie/wecare/challenge/dto/GetChallengeAdRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ollie.wecare.challenge.dto;

import lombok.*;
import ollie.wecare.challenge.entity.Challenge;

@Getter
@AllArgsConstructor
@Builder
@NoArgsConstructor
public class GetChallengeAdRes {

private Long challengeIdx;
private String name;

public static GetChallengeAdRes fromChallenge(Challenge challenge) {
return GetChallengeAdRes.builder()
.challengeIdx(challenge.getChallengeIdx())
.name(challenge.getName())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
@NoArgsConstructor
public class GetChallengeAdsRes {

private GetChallengesRes mostParticipatedChallenge;
private GetChallengesRes mostAttendancedChallenge;
private GetChallengesRes mostRecentlyStartedChallenge;
private GetChallengeAdRes mostParticipatedChallenge;
private GetChallengeAdRes mostAttendancedChallenge;
private GetChallengeAdRes mostRecentlyStartedChallenge;

}
4 changes: 4 additions & 0 deletions src/main/java/ollie/wecare/challenge/entity/Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public void updateAttendanceCode(String attendanceCode) {
private Integer totalNum;

private Integer attendanceRate;

public void setParticipants(User user) {
if(user != null) this.participants.add(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import ollie.wecare.challenge.entity.Challenge;
import ollie.wecare.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -24,4 +25,5 @@ public interface ChallengeRepository extends JpaRepository<Challenge, Long> {

Optional<Challenge> findTop1ByOrderByCreatedDateDesc();
Optional<Challenge> findByChallengeIdxAndStatusEquals(Long challengeIdx, String status);
List<Challenge> findByParticipantsContaining(User user);
}
51 changes: 22 additions & 29 deletions src/main/java/ollie/wecare/challenge/service/ChallengeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ollie.wecare.user.entity.User;
import ollie.wecare.user.repository.UserRepository;
import ollie.wecare.user.service.UserService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -39,14 +40,10 @@ public class ChallengeService {
// 참여중인 챌린지 목록 조회
public BaseResponse<List<GetChallengesRes>> getMyChallenges(Long userIdx) throws BaseException {
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE).orElseThrow(() -> new BaseException(INVALID_USER_IDX));

List<ChallengeAttendance> attendances = challengeAttendanceRepository.findByUserAndStatusEquals(user, ACTIVE);
List<Challenge> challenges = attendances.stream()
.map(ChallengeAttendance::getChallenge)
return new BaseResponse<>(challengeRepository.findByParticipantsContaining(user).stream()
.map(challenge -> GetChallengesRes.fromChallenge(challenge, calculateMyAchievementRate(user, challenge)))
.distinct()
.toList();
List<GetChallengesRes> challengeList = challenges.stream().map(challenge -> GetChallengesRes.fromChallenge(challenge, calculateMyAchievementRate(user, challenge))).toList();
return new BaseResponse<>(challengeList);
.toList());
}

/*
Expand Down Expand Up @@ -86,13 +83,9 @@ public void attendChallenge(AttendChallengeReq attendChallengeReq) throws BaseEx
/*
* 새로운 챌린지 참여
* */
@Transactional
public void participateChallenge(PostChallengeReq postChallengeReq) throws BaseException {
ChallengeAttendance challengeAttendance = ChallengeAttendance.builder()
.user(userService.getUserWithValidation())
.challenge(challengeRepository.findById(postChallengeReq.getChallengeIdx()).orElseThrow(()-> new BaseException(INVALID_CHALLENGE_IDX)))
.build();
challengeAttendanceRepository.save(challengeAttendance);
Challenge challenge = challengeRepository.findById(postChallengeReq.getChallengeIdx()).orElseThrow(()-> new BaseException(INVALID_CHALLENGE_IDX));
challenge.setParticipants(userService.getUserWithValidation());
}

/*
Expand Down Expand Up @@ -145,20 +138,20 @@ private Integer calculateMyAchievementRate(User user, Challenge challenge) {
else return (attendanceCount/challenge.getTotalNum()) * 100;
}

// /*
// * 챌린지 배너 조회 (홈화면)
// * */
// public GetChallengeAdsRes getChallengeAds() {
//
// Challenge mostAttendancedChallenge = challengeRepository.findTop1ByOrderByAttendanceRateDesc().orElseThrow(()-> new BaseException(NO_CHALLENGE));
// Challenge mostParticipatedChallenge = challengeRepository.findMostParticipatedChallenge(PageRequest.of(0, 1)).getContent().get(0);
// Challenge mostRecentlyStartedChallenge = challengeRepository.findTop1ByOrderByCreatedDateDesc().orElseThrow(()-> new BaseException(NO_CHALLENGE));
//
//
// return GetChallengeAdsRes.builder()
// .mostAttendancedChallenge(GetChallengesRes.fromChallenge(mostAttendancedChallenge, 0L))
// .mostParticipatedChallenge(GetChallengesRes.fromChallenge(mostParticipatedChallenge, 0L))
// .mostRecentlyStartedChallenge(GetChallengesRes.fromChallenge(mostRecentlyStartedChallenge, 0L)).build();
//
// }
/*
* 챌린지 배너 조회 (홈화면)
* */
public GetChallengeAdsRes getChallengeAds() {

Challenge mostAttendancedChallenge = challengeRepository.findTop1ByOrderByAttendanceRateDesc().orElseThrow(()-> new BaseException(NO_CHALLENGE));
Challenge mostParticipatedChallenge = challengeRepository.findMostParticipatedChallenge(PageRequest.of(0, 1)).getContent().get(0);
Challenge mostRecentlyStartedChallenge = challengeRepository.findTop1ByOrderByCreatedDateDesc().orElseThrow(()-> new BaseException(NO_CHALLENGE));


return GetChallengeAdsRes.builder()
.mostAttendancedChallenge(GetChallengeAdRes.fromChallenge(mostAttendancedChallenge))
.mostParticipatedChallenge(GetChallengeAdRes.fromChallenge(mostParticipatedChallenge))
.mostRecentlyStartedChallenge(GetChallengeAdRes.fromChallenge(mostRecentlyStartedChallenge)).build();

}
}

0 comments on commit 5f780c6

Please sign in to comment.