Skip to content

Commit

Permalink
Fix: 시작안한 챌린지 검색 오류 수정 (#102)
Browse files Browse the repository at this point in the history
[Fix] 챌린지 status = recruited, unmatched 추가 & 관련 로직 수정
  • Loading branch information
pingowl authored Dec 8, 2023
2 parents 1605a51 + 071971f commit 38e4e8f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/main/java/igoMoney/BE/domain/Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Challenge extends BaseEntity {
private Integer term=7; // days
private LocalDate endDate;
@Builder.Default
private String status="notStarted"; // notStarted/inProgress/cancel/done
private String status="notStarted"; // notStarted/recruited/inProgress/cancel/done/unmatched

@Builder.Default
@OneToMany(mappedBy = "challenge", cascade = CascadeType.ALL)
Expand All @@ -61,7 +61,9 @@ public static Challenge createChallenge(ChallengeUser... challengeUsers) {
return challenge;
}

public void startChallenge() { this.startDate = LocalDate.now().plusDays(1); this.status = "inProgress";}
public void startChallenge() { this.status = "inProgress";}
public void setChallengeRecruited() { this.status = "recruited"; }
public void setChallengeUnmatched() { this.status = "unmatched"; }
public void stopChallenge() { this.endDate = LocalDate.now(); this.status = "cancel";}
public void finishChallenge() { this.endDate = LocalDate.now().plusDays(-1); this.status = "done";}
public void setWinner(Long userId) { this.winnerId = userId;}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import igoMoney.BE.domain.Challenge;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;

public interface ChallengeRepository extends JpaRepository<Challenge, Long>, ChallengeCustomRepository {

@Query("select c from Challenge c where c.startDate > :date")
List<Challenge> findAllByStartDateIsAfter(@Param("date") LocalDate date);
List<Challenge> findAllByStartDateAndStatus(LocalDate date, String status);
List<Challenge> findAllByStatus(String status);
List<Challenge> findAllByEndDateAndStatus(LocalDate date, String status);
}
36 changes: 31 additions & 5 deletions src/main/java/igoMoney/BE/service/ChallengeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public void applyChallenge(Long userId, Long challengeId) {
throw new CustomException(ErrorCode.EXIST_USER_CHALLENGE);
}
Challenge findChallenge = getChallengeOrThrow(challengeId);
findChallenge.setChallengeRecruited();
findUser.updateUserStatus(true, challengeId); // 챌린지 참여 정보 업데이트
findUser.addChallengeCount();

Expand Down Expand Up @@ -251,16 +252,41 @@ public ChallengeTotalCostResponse getTotalCostPerChallengeUser(Long challengeId,
return response;
}

@Scheduled(cron="0 0 0 * * *", zone = "Asia/Seoul") // 초 분 시 일 월 요일
public void startChallenge(){
List<Challenge> challenges = challengeRepository.findAllByStartDateAndStatus( LocalDate.now(),"recruited");
for(Challenge c : challenges){
c.startChallenge();
}
}

@Scheduled(cron="0 0 0 * * *", zone = "Asia/Seoul") // 초 분 시 일 월 요일
public void handleUnmatchedChallenge(){
List<Challenge> challenges = challengeRepository.findAllByStartDateAndStatus( LocalDate.now(),"notStarted");
for(Challenge c : challenges){
c.setChallengeUnmatched();
User findUser = getUserOrThrow(c.getLeaderId());
findUser.updateUserStatus(false, null); // 사용자 챌린지 상태 변경
findUser.resetReportedCount();
Notification notification = Notification.builder()
.user(findUser)
.title("챌린지 현황")
.message(findUser.getNickname() +"님! 지정하신 챌린지 시작일까지 상대방 매칭이 안 되어서 챌린지가 취소되었어요. 새로운 챌린지를 도전해보세요.")
.build();
notificationService.makeNotification(notification);
}
}

// 챌린지 완료 (마지막날까지 성공)
@Scheduled(cron="0 0 0 * * *", zone = "Asia/Seoul") // 초 분 시 일 월 요일
public void finishChallenge() {

List<Challenge> challengeList = challengeRepository.findAllByStatus("inProgress");
List<Challenge> challenges = challengeRepository.findAllByStatus("inProgress");
Integer minCost = 99999999;
Long winnerId = null;
Boolean check =false;
Integer tempCost = 99999999;
for (Challenge c : challengeList) {
for (Challenge c : challenges) {
if (c.getStartDate().plusDays(7).isEqual(LocalDate.now())){
// Challenge : 챌린지 종료 설정
c.finishChallenge();
Expand Down Expand Up @@ -419,12 +445,12 @@ private User getChallengeOtherUser(Long challengeId, Long userId) {
// 챌린지 참가자 정보 조회
private List<User> getAllChallengeUser(Long challengeId) {

List<User> userList = new ArrayList<>();
List<User> users = new ArrayList<>();
List<ChallengeUser> challengeUserList = challengeUserRepository.findAllByChallengeId(challengeId);
for (ChallengeUser c : challengeUserList) {
userList.add(c.getUser());
users.add(c.getUser());
}
return userList;
return users;
}

// 특정 챌린지에 유저가 참여했는지 조회
Expand Down

0 comments on commit 38e4e8f

Please sign in to comment.