Skip to content

Commit

Permalink
Merge pull request #138 from Team-HMH/feat/#117-get-challenge-with-st…
Browse files Browse the repository at this point in the history
…art-date

feat - 챌린지 달성현황뷰 response에 챌린지 시작날짜 추가
  • Loading branch information
jumining authored May 20, 2024
2 parents 49aace7 + 0b88979 commit 3c0a698
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ResponseEntity<BaseResponse<DailyChallengeResponse>> orderGetDailyChallen
public ResponseEntity<BaseResponse<?>> orderAddApps(@UserId final Long userId,
@RequestHeader("OS") final String os,
@RequestBody final AppArrayGoalTimeRequest requests) {
Challenge challenge = challengeService.findFirstByUserIdOrderByCreatedAtDescOrElseThrow(userId);
Challenge challenge = challengeService.findCurrentChallengeByUserId(userId);
challengeService.addApps(challenge, requests.apps(), os);

return ResponseEntity
Expand All @@ -74,7 +74,7 @@ public ResponseEntity<BaseResponse<?>> orderAddApps(@UserId final Long userId,
public ResponseEntity<BaseResponse<?>> orderRemoveApp(@UserId final Long userId,
@RequestHeader("OS") final String os,
@RequestBody final AppRemoveRequest request) {
Challenge challenge = challengeService.findFirstByUserIdOrderByCreatedAtDescOrElseThrow(userId);
Challenge challenge = challengeService.findCurrentChallengeByUserId(userId);
challengeService.removeApp(challenge, request, os);

return ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public record ChallengeResponse(
Integer period,
List<Status> statuses,
Integer todayIndex,
String startDate,
Long goalTime,
List<AppGoalTimeResponse> apps
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ public interface ChallengeRepository extends JpaRepository<Challenge, Long> {

Optional<Challenge> findById(Long id);

Optional<Challenge> findFirstByUserIdOrderByCreatedAtDesc(Long userId);

void deleteByUserIdIn(List<Long> userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

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

@Service
@RequiredArgsConstructor
Expand All @@ -52,9 +52,11 @@ public Challenge addChallenge(Long userId, Integer period, Long goalTime, String
.goalTime(goalTime)
.build());

Optional<Challenge> previousChallenge = challengeRepository.findFirstByUserIdOrderByCreatedAtDesc(userId);
if (previousChallenge.isPresent()) {
List<AppGoalTimeRequest> previousApps = previousChallenge.get().getApps().stream()
User user = userService.findByIdOrThrowException(userId);
Long previousChallengeId = user.getCurrentChallengeId();
if (previousChallengeId != null) {
Challenge previousChallenge = findByIdOrElseThrow(previousChallengeId);
List<AppGoalTimeRequest> previousApps = previousChallenge.getApps().stream()
.map(app -> new AppGoalTimeRequest(app.getAppCode(), app.getGoalTime()))
.toList();
addApps(challenge, previousApps, os);
Expand All @@ -72,14 +74,13 @@ public Challenge addChallenge(Long userId, Integer period, Long goalTime, String
}
dailyChallengeRepository.saveAll(dailyChallenges);

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

return challenge;
}

public ChallengeResponse getChallenge(Long userId) {
Challenge challenge = this.findFirstByUserIdOrderByCreatedAtDescOrElseThrow(userId);
Challenge challenge = findCurrentChallengeByUserId(userId);
Integer todayIndex = calculateTodayIndex(challenge.getCreatedAt(), challenge.getPeriod());

return ChallengeResponse.builder()
Expand All @@ -89,14 +90,15 @@ public ChallengeResponse getChallenge(Long userId) {
.map(DailyChallenge::getStatus)
.toList())
.todayIndex(todayIndex)
.startDate(challenge.getCreatedAt().toLocalDate().toString())
.goalTime(challenge.getGoalTime())
.apps(challenge.getApps().stream()
.map(app -> new AppGoalTimeResponse(app.getAppCode(), app.getGoalTime())).toList())
.build();
}

public DailyChallengeResponse getDailyChallenge(Long userId) {
Challenge challenge = this.findFirstByUserIdOrderByCreatedAtDescOrElseThrow(userId);
Challenge challenge = findCurrentChallengeByUserId(userId);

return DailyChallengeResponse.builder()
.status(Boolean.TRUE.equals(challenge.isChallengeFailedToday())
Expand Down Expand Up @@ -181,17 +183,16 @@ private void validateAppTime(Long appTime) {
throw new AppException(AppError.INVALID_TIME_RANGE);
}

@Deprecated
public Challenge findFirstByUserIdOrderByCreatedAtDescOrElseThrow(Long userId) {
return challengeRepository.findFirstByUserIdOrderByCreatedAtDesc(userId).orElseThrow(
() -> new ChallengeException(ChallengeError.CHALLENGE_NOT_FOUND));
}

public Challenge findByIdOrElseThrow(Long challengeId) {
return challengeRepository.findById(challengeId).orElseThrow(
() -> new ChallengeException(ChallengeError.CHALLENGE_NOT_FOUND));
}

public Challenge findCurrentChallengeByUserId(Long userId) {
User user = userService.findByIdOrThrowException(userId);
return findByIdOrElseThrow(user.getCurrentChallengeId());
}

public List<AppWithGoalTime> getCurrentChallengeAppWithGoalTimeByChallengeId(Long challengeId) {
return this.findByIdOrElseThrow(challengeId).getApps();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum UserError implements ErrorBase {
// 404 NOT FOUND
NOT_FOUND_USER(HttpStatus.NOT_FOUND, "유저를 찾을 수 없습니다."),
NOT_ENOUGH_POINTS(HttpStatus.BAD_REQUEST, "유저의 포인트가 부족합니다."),
NOT_FOUND_CURRENT_CHALLENGE_ID(HttpStatus.NOT_FOUND, "유저에서 현재 챌린지 id 정보를 찾을 수 없습니다.")
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sopt.org.hmh.domain.user.service;

import java.util.List;
import java.util.Optional;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -10,6 +12,8 @@
import sopt.org.hmh.domain.auth.exception.AuthException;
import sopt.org.hmh.domain.auth.repository.OnboardingInfoRepository;
import sopt.org.hmh.domain.auth.repository.ProblemRepository;
import sopt.org.hmh.domain.challenge.domain.exception.ChallengeError;
import sopt.org.hmh.domain.challenge.domain.exception.ChallengeException;
import sopt.org.hmh.domain.user.domain.OnboardingInfo;
import sopt.org.hmh.domain.user.domain.OnboardingProblem;
import sopt.org.hmh.domain.user.domain.User;
Expand Down Expand Up @@ -102,6 +106,7 @@ public User findByIdOrThrowException(Long userId) {
}

public Long getCurrentChallengeIdByUserId(Long userId) {
return this.findByIdOrThrowException(userId).getCurrentChallengeId();
return Optional.ofNullable(this.findByIdOrThrowException(userId).getCurrentChallengeId())
.orElseThrow(() -> new UserException(UserError.NOT_FOUND_CURRENT_CHALLENGE_ID));
}
}
}

0 comments on commit 3c0a698

Please sign in to comment.