Skip to content

Commit

Permalink
Merge pull request #189 from Team-HMH/fix/#188-find-previous-apps
Browse files Browse the repository at this point in the history
fix - #188 κΈ°μ‘΄ μ±Œλ¦°μ§€ id κ°’ λΆˆλŸ¬μ˜€λŠ” λ©”μ†Œλ“œ μˆœμ„œ μˆ˜μ •
  • Loading branch information
jumining authored Aug 19, 2024
2 parents e2455a3 + 7aa4633 commit 6409927
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ public class ChallengeFacade {
@Transactional
public void startNewChallenge(NewChallengeOrder newChallengeOrder) {
Challenge newChallenge = challengeService.addChallenge(newChallengeOrder.toChallengeEntity());
userService.changeCurrentChallengeIdByUserId(newChallengeOrder.getUserId(), newChallenge.getId());

dailyChallengeService.addDailyChallenge(newChallenge);

this.addAppsByNewChallengeOrder(newChallengeOrder, newChallenge);
userService.changeCurrentChallengeIdByUserId(newChallengeOrder.getUserId(), newChallenge.getId());
}

private void addAppsByNewChallengeOrder(NewChallengeOrder newChallengeOrder, Challenge newChallenge) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ResponseEntity<BaseResponse<ChallengeStatusesResponse>> orderAddHistoryDa
return ResponseEntity
.status(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS.getHttpStatus())
.body(BaseResponse.success(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS,
new ChallengeStatusesResponse(dailyChallengeFacade.addFinishedDailyChallengeHistory(userId, request, os))));
new ChallengeStatusesResponse(dailyChallengeFacade.addFinishedDailyChallengeHistory(userId, request, os, timeZone))));
}

@Override
Expand All @@ -47,6 +47,6 @@ public ResponseEntity<BaseResponse<ChallengeStatusesResponse>> orderChangeStatus
return ResponseEntity
.status(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS.getHttpStatus())
.body(BaseResponse.success(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS,
new ChallengeStatusesResponse(dailyChallengeFacade.changeDailyChallengeStatusByIsSuccess(userId, request))));
new ChallengeStatusesResponse(dailyChallengeFacade.changeDailyChallengeStatusByIsSuccess(userId, request, timeZone))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public enum DailyChallengeError implements ErrorBase {
DAILY_CHALLENGE_NOT_FOUND(HttpStatus.NOT_FOUND, "일별 μ±Œλ¦°μ§€λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
DAILY_CHALLENGE_PERIOD_INDEX_NOT_FOUND(HttpStatus.NOT_FOUND, "ν•΄λ‹Ή 인덱슀의 일별 μ±Œλ¦°μ§€λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
DAILY_CHALLENGE_ALREADY_PROCESSED(HttpStatus.BAD_REQUEST, "이미 처리된 일별 μ±Œλ¦°μ§€μž…λ‹ˆλ‹€."),
DAILY_CHALLENGE_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "이미 μ‘΄μž¬ν•˜λŠ” 일별 μ±Œλ¦°μ§€μž…λ‹ˆλ‹€.");
DAILY_CHALLENGE_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "이미 μ‘΄μž¬ν•˜λŠ” 일별 μ±Œλ¦°μ§€μž…λ‹ˆλ‹€."),
PERIOD_INDEX_NOT_VALID(HttpStatus.BAD_REQUEST, "μ§€λ‚œ λ‚ μ˜ μ •λ³΄λ§Œ 전솑할 수 μžˆμŠ΅λ‹ˆλ‹€.");

private final HttpStatus status;
private final String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sopt.org.hmh.domain.dailychallenge.service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -23,10 +25,13 @@ public class DailyChallengeFacade {
private final UserService userService;

@Transactional
public List<Status> addFinishedDailyChallengeHistory(Long userId, FinishedDailyChallengeListRequest request, String os) {
public List<Status> addFinishedDailyChallengeHistory(Long userId, FinishedDailyChallengeListRequest request, String os, String timeZone) {
Challenge challenge = challengeService.findByIdOrElseThrow(userService.getCurrentChallengeIdByUserId(userId));
Integer todayIndex = dailyChallengeService.calculateTodayIndex(challenge, LocalDate.now(ZoneId.of(timeZone)));

request.finishedDailyChallenges().forEach(challengeRequest -> {
dailyChallengeService.validatePeriodIndex(challengeRequest.challengePeriodIndex(), todayIndex);

DailyChallenge dailyChallenge = dailyChallengeService
.findDailyChallengeByChallengePeriodIndex(challenge, challengeRequest.challengePeriodIndex());
dailyChallengeService.changeStatusByCurrentStatus(dailyChallenge);
Expand All @@ -37,10 +42,13 @@ public List<Status> addFinishedDailyChallengeHistory(Long userId, FinishedDailyC
}

@Transactional
public List<Status> changeDailyChallengeStatusByIsSuccess(Long userId, FinishedDailyChallengeStatusListRequest request) {
public List<Status> changeDailyChallengeStatusByIsSuccess(Long userId, FinishedDailyChallengeStatusListRequest request, String timeZone) {
Challenge challenge = challengeService.findByIdOrElseThrow(userService.getCurrentChallengeIdByUserId(userId));
Integer todayIndex = dailyChallengeService.calculateTodayIndex(challenge, LocalDate.now(ZoneId.of(timeZone)));

request.finishedDailyChallenges().forEach(challengeRequest -> {
dailyChallengeService.validatePeriodIndex(challengeRequest.challengePeriodIndex(), todayIndex);

DailyChallenge dailyChallenge = dailyChallengeService
.findDailyChallengeByChallengePeriodIndex(challenge, challengeRequest.challengePeriodIndex());
if (challengeRequest.isSuccess()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sopt.org.hmh.domain.dailychallenge.service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -73,6 +74,10 @@ private void validateDuplicateDailyChallenge(Challenge challenge) {
}
}

public void validatePeriodIndex(Integer periodIndex, Integer todayIndex) {
if (periodIndex >= todayIndex) throw new DailyChallengeException(DailyChallengeError.PERIOD_INDEX_NOT_VALID);
}

private List<DailyChallenge> createDailyChallengeByChallengePeriod(Challenge challenge) {
LocalDate startDate = challenge.getStartDate();
Long userId = challenge.getUserId();
Expand Down Expand Up @@ -106,4 +111,10 @@ private void changeChallengeDateOfDailyChallenges(List<DailyChallenge> dailyChal
dailyChallenges.get(i).changeChallengeDate(challengeDate.plusDays(i));
}
}

public Integer calculateTodayIndex(Challenge challenge, LocalDate now) {
final int COMPLETED_CHALLENGE_INDEX = -1;
int daysBetween = (int) ChronoUnit.DAYS.between(challenge.getStartDate(), now);
return (daysBetween >= challenge.getPeriod()) ? COMPLETED_CHALLENGE_INDEX : daysBetween;
}
}

0 comments on commit 6409927

Please sign in to comment.