Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat - 유저테이블에 현재 챌린지 정보 저장 #135

Merged
merged 5 commits into from
May 20, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
import sopt.org.hmh.domain.challenge.repository.ChallengeRepository;
import sopt.org.hmh.domain.dailychallenge.domain.DailyChallenge;
import sopt.org.hmh.domain.dailychallenge.domain.Status;
import sopt.org.hmh.domain.dailychallenge.repository.DailyChallengeRepository;
import sopt.org.hmh.domain.user.domain.User;
import sopt.org.hmh.domain.user.service.UserService;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -33,34 +38,49 @@ public class ChallengeService {

private final ChallengeRepository challengeRepository;
private final AppWithGoalTimeRepository appWithGoalTimeRepository;
private final DailyChallengeRepository dailyChallengeRepository;
private final UserService userService;

@Transactional
public Challenge addChallenge(Long userId, Integer period, Long goalTime, String os) {
validateChallengePeriod(period);
validateChallengeGoalTime(goalTime);

Optional<Challenge> previousChallenge = challengeRepository.findFirstByUserIdOrderByCreatedAtDesc(userId);

Challenge challenge = challengeRepository.save(Challenge.builder()
.userId(userId)
.period(period)
.goalTime(goalTime)
.build());

Optional<Challenge> previousChallenge = challengeRepository.findFirstByUserIdOrderByCreatedAtDesc(userId);
if (previousChallenge.isPresent()) {
List<AppGoalTimeRequest> previousApps = previousChallenge.get().getApps().stream()
.map(app -> new AppGoalTimeRequest(app.getAppCode(), app.getGoalTime()))
.toList();
addApps(challenge, previousApps, os);
}

List<DailyChallenge> dailyChallenges = new ArrayList<>();
LocalDate startDate = challenge.getCreatedAt().toLocalDate();
for (int dayCount = 0; dayCount < period; dayCount++) {
DailyChallenge dailyChallenge = DailyChallenge.builder()
.challengeDate(startDate.plusDays(dayCount))
.challenge(challenge)
.userId(userId)
.goalTime(goalTime).build();
dailyChallenges.add(dailyChallenge);
}
dailyChallengeRepository.saveAll(dailyChallenges);

User user = userService.findByIdOrThrowException(userId);
user.changeCurrentChallengeId(challenge.getId());

return challenge;
}

public ChallengeResponse getChallenge(Long userId) {
Challenge challenge = this.findFirstByUserIdOrderByCreatedAtDescOrElseThrow(userId);
Integer todayIndex = Boolean.TRUE.equals(hasChallengePeriodEnded(challenge.getCreatedAt(), challenge.getPeriod()))
? -1
: challenge.getHistoryDailyChallenges().size();
Integer todayIndex = calculateTodayIndex(challenge.getCreatedAt(), challenge.getPeriod());

return ChallengeResponse.builder()
.period(challenge.getPeriod())
Expand Down Expand Up @@ -118,10 +138,9 @@ public void deleteChallengeRelatedByUserId(List<Long> expiredUserIdList) {
challengeRepository.deleteByUserIdIn(expiredUserIdList);
}

private Boolean hasChallengePeriodEnded(LocalDateTime challengeCreatedAt, Integer period) {
Duration duration = Duration.between(LocalDateTime.now(), challengeCreatedAt);
long daysDifference = duration.toDays();
return daysDifference >= period;
private Integer calculateTodayIndex(LocalDateTime challengeCreateAt, int period) {
int daysBetween = (int) ChronoUnit.DAYS.between(challengeCreateAt.toLocalDate(), LocalDate.now());
return (daysBetween >= period) ? -1 : daysBetween;
}

private void validateChallengePeriod(Integer period) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/sopt/org/hmh/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ public Integer increasePoint(Integer earnedPoint) {
this.point += earnedPoint;
return this.point;
}

public void changeCurrentChallengeId(Long currentChallengeId) {
this.currentChallengeId = currentChallengeId;
}
}
Loading