Skip to content

Commit

Permalink
Merge pull request #184 from Team-HMH/modify/#183-change-response-posโ€ฆ
Browse files Browse the repository at this point in the history
โ€ฆt-challenge

modify - ์ผ๋ณ„์ฑŒ๋ฆฐ์ง€ ๊ฒฐ๊ณผ ์ „์†ก ํ›„ ์ฑŒ๋ฆฐ์ง€์˜ ๋ณ€๊ฒฝ๋œ ์ƒํƒœ๊ฐ’๋“ค์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ˆ˜์ •
  • Loading branch information
jumining authored Jul 29, 2024
2 parents 8a34f26 + 4fbb30f commit c03f2d1
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Challenge extends BaseTimeEntity {
private List<ChallengeApp> apps;

@OneToMany(mappedBy = "challenge", cascade = CascadeType.REMOVE, orphanRemoval = true)
@OrderBy("challengeDate ASC")
private List<DailyChallenge> historyDailyChallenges;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sopt.org.hmh.domain.dailychallenge.domain.DailyChallenge;
import sopt.org.hmh.domain.dailychallenge.domain.Status;

import java.util.Comparator;
import java.util.List;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.RequestHeader;
import sopt.org.hmh.domain.dailychallenge.dto.request.FinishedDailyChallengeListRequest;
import sopt.org.hmh.domain.dailychallenge.dto.request.FinishedDailyChallengeStatusListRequest;
import sopt.org.hmh.domain.dailychallenge.dto.response.ChallengeStatusesResponse;
import sopt.org.hmh.global.auth.jwt.JwtConstants;
import sopt.org.hmh.global.common.constant.CustomHeaderType;
import sopt.org.hmh.global.common.response.BaseResponse;
Expand All @@ -34,7 +35,7 @@ public interface DailyChallengeApi {
responseCode = "500",
description = "์„œ๋ฒ„ ๋‚ด๋ถ€ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.",
content = @Content)})
ResponseEntity<BaseResponse<EmptyJsonResponse>> orderAddHistoryDailyChallenge(
ResponseEntity<BaseResponse<ChallengeStatusesResponse>> orderAddHistoryDailyChallenge(
@Parameter(hidden = true) final Long userId,
@RequestHeader(CustomHeaderType.TIME_ZONE) final String os,
@RequestHeader(CustomHeaderType.TIME_ZONE) final String timeZone,
Expand All @@ -55,7 +56,7 @@ ResponseEntity<BaseResponse<EmptyJsonResponse>> orderAddHistoryDailyChallenge(
responseCode = "500",
description = "์„œ๋ฒ„ ๋‚ด๋ถ€ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.",
content = @Content)})
ResponseEntity<BaseResponse<EmptyJsonResponse>> orderChangeStatusDailyChallenge(
ResponseEntity<BaseResponse<ChallengeStatusesResponse>> orderChangeStatusDailyChallenge(
@Parameter(hidden = true) final Long userId,
@RequestHeader(CustomHeaderType.TIME_ZONE) final String timeZone,
@RequestBody final FinishedDailyChallengeStatusListRequest request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sopt.org.hmh.domain.challenge.dto.response.ChallengeResponse;
import sopt.org.hmh.domain.challenge.service.ChallengeFacade;
import sopt.org.hmh.domain.dailychallenge.domain.exception.DailyChallengeSuccess;
import sopt.org.hmh.domain.dailychallenge.dto.request.FinishedDailyChallengeListRequest;
import sopt.org.hmh.domain.dailychallenge.dto.request.FinishedDailyChallengeStatusListRequest;
import sopt.org.hmh.domain.dailychallenge.dto.response.ChallengeStatusesResponse;
import sopt.org.hmh.domain.dailychallenge.service.DailyChallengeFacade;
import sopt.org.hmh.global.auth.UserId;
import sopt.org.hmh.global.common.constant.CustomHeaderType;
Expand All @@ -22,28 +25,28 @@ public class DailyChallengeController implements DailyChallengeApi {

@Override
@PostMapping("/finish")
public ResponseEntity<BaseResponse<EmptyJsonResponse>> orderAddHistoryDailyChallenge(
public ResponseEntity<BaseResponse<ChallengeStatusesResponse>> orderAddHistoryDailyChallenge(
@UserId final Long userId,
@RequestHeader(CustomHeaderType.OS) final String os,
@RequestHeader(CustomHeaderType.TIME_ZONE) final String timeZone,
@RequestBody @Valid final FinishedDailyChallengeListRequest request
) {
dailyChallengeFacade.addFinishedDailyChallengeHistory(userId, request, os);
return ResponseEntity
.status(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS.getHttpStatus())
.body(BaseResponse.success(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS, new EmptyJsonResponse()));
.body(BaseResponse.success(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS,
new ChallengeStatusesResponse(dailyChallengeFacade.addFinishedDailyChallengeHistory(userId, request, os))));
}

@Override
@PostMapping("/success")
public ResponseEntity<BaseResponse<EmptyJsonResponse>> orderChangeStatusDailyChallenge(
public ResponseEntity<BaseResponse<ChallengeStatusesResponse>> orderChangeStatusDailyChallenge(
@UserId final Long userId,
@RequestHeader(CustomHeaderType.TIME_ZONE) final String timeZone,
@RequestBody final FinishedDailyChallengeStatusListRequest request
) {
dailyChallengeFacade.changeDailyChallengeStatusByIsSuccess(userId, request);
return ResponseEntity
.status(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS.getHttpStatus())
.body(BaseResponse.success(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS, new EmptyJsonResponse()));
.body(BaseResponse.success(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS,
new ChallengeStatusesResponse(dailyChallengeFacade.changeDailyChallengeStatusByIsSuccess(userId, request))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sopt.org.hmh.domain.dailychallenge.dto.response;

import sopt.org.hmh.domain.dailychallenge.domain.Status;

import java.util.List;

public record ChallengeStatusesResponse(
List<Status> statuses
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface DailyChallengeJpaRepository extends JpaRepository<DailyChalleng

Optional<DailyChallenge> findByChallengeDateAndUserId(LocalDate challengeDate, Long userId);

List<DailyChallenge> findAllByChallengeIdOrderByChallengeDate(Long challengeId);
List<DailyChallenge> findAllByChallengeId(Long challengeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface DailyChallengeRepository {

Optional<DailyChallenge> findByChallengeDateAndUserId(LocalDate challengeDate, Long userId);

List<DailyChallenge> findAllByChallengeIdOrderByChallengeDate(Long challengeId);
List<DailyChallenge> findAllByChallengeId(Long challengeId);

boolean existsByUserIdAndChallengeDateIn(Long userId, List<LocalDate> localDates);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public Optional<DailyChallenge> findByChallengeDateAndUserId(LocalDate challenge
}

@Override
public List<DailyChallenge> findAllByChallengeIdOrderByChallengeDate(Long challengeId) {
return dailyChallengeJpaRepository.findAllByChallengeIdOrderByChallengeDate(challengeId);
public List<DailyChallenge> findAllByChallengeId(Long challengeId) {
return dailyChallengeJpaRepository.findAllByChallengeId(challengeId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.hmh.domain.app.domain.ChallengeApp;
import sopt.org.hmh.domain.app.service.HistoryAppService;
import sopt.org.hmh.domain.challenge.domain.Challenge;
import sopt.org.hmh.domain.challenge.service.ChallengeService;
import sopt.org.hmh.domain.dailychallenge.domain.DailyChallenge;
import sopt.org.hmh.domain.dailychallenge.domain.Status;
Expand All @@ -23,28 +23,27 @@ public class DailyChallengeFacade {
private final UserService userService;

@Transactional
public void addFinishedDailyChallengeHistory(Long userId, FinishedDailyChallengeListRequest requests, String os) {
Long currentChallengeId = userService.getCurrentChallengeIdByUserId(userId);
List<ChallengeApp> currentChallengeApps =
challengeService.getCurrentChallengeAppByChallengeId(currentChallengeId);

requests.finishedDailyChallenges().forEach(request -> {
DailyChallenge dailyChallenge =
dailyChallengeService.findDailyChallengeByChallengeIdAndChallengePeriodIndex(
currentChallengeId, request.challengePeriodIndex());
public List<Status> addFinishedDailyChallengeHistory(Long userId, FinishedDailyChallengeListRequest request, String os) {
Challenge challenge = challengeService.findByIdOrElseThrow(userService.getCurrentChallengeIdByUserId(userId));

request.finishedDailyChallenges().forEach(challengeRequest -> {
DailyChallenge dailyChallenge = dailyChallengeService
.findDailyChallengeByChallengePeriodIndex(challenge, challengeRequest.challengePeriodIndex());
dailyChallengeService.changeStatusByCurrentStatus(dailyChallenge);
historyAppService.addHistoryApp(currentChallengeApps, request.apps(), dailyChallenge, os);
historyAppService.addHistoryApp(challenge.getApps(), challengeRequest.apps(), dailyChallenge, os);
});

return getHistoryDailyChallenges(challenge);
}

@Transactional
public void changeDailyChallengeStatusByIsSuccess(Long userId, FinishedDailyChallengeStatusListRequest requests) {
Long currentChallengeId = userService.getCurrentChallengeIdByUserId(userId);
requests.finishedDailyChallenges().forEach(request -> {
DailyChallenge dailyChallenge =
dailyChallengeService.findDailyChallengeByChallengeIdAndChallengePeriodIndex(
currentChallengeId, request.challengePeriodIndex());
if (request.isSuccess()) {
public List<Status> changeDailyChallengeStatusByIsSuccess(Long userId, FinishedDailyChallengeStatusListRequest request) {
Challenge challenge = challengeService.findByIdOrElseThrow(userService.getCurrentChallengeIdByUserId(userId));

request.finishedDailyChallenges().forEach(challengeRequest -> {
DailyChallenge dailyChallenge = dailyChallengeService
.findDailyChallengeByChallengePeriodIndex(challenge, challengeRequest.challengePeriodIndex());
if (challengeRequest.isSuccess()) {
dailyChallengeService.validateDailyChallengeStatus(dailyChallenge.getStatus(), List.of(Status.NONE));
dailyChallenge.changeStatus(Status.UNEARNED);
} else {
Expand All @@ -53,5 +52,14 @@ public void changeDailyChallengeStatusByIsSuccess(Long userId, FinishedDailyChal
dailyChallenge.changeStatus(Status.FAILURE);
}
});

return getHistoryDailyChallenges(challenge);
}

private List<Status> getHistoryDailyChallenges(Challenge challenge) {
return challenge.getHistoryDailyChallenges()
.stream()
.map(DailyChallenge::getStatus)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public DailyChallenge findDailyChallengeByChallengeDateAndUserIdOrElseThrow(Loca
.orElseThrow(() -> new DailyChallengeException(DailyChallengeError.DAILY_CHALLENGE_NOT_FOUND));
}

public DailyChallenge findDailyChallengeByChallengeIdAndChallengePeriodIndex(Long challengeId, Integer challengePeriodIndex) {
public DailyChallenge findDailyChallengeByChallengePeriodIndex(Challenge challenge, Integer challengePeriodIndex) {
return Optional.ofNullable(
dailyChallengeRepository.findAllByChallengeIdOrderByChallengeDate(challengeId).get(challengePeriodIndex)
challenge.getHistoryDailyChallenges().get(challengePeriodIndex)
).orElseThrow(() -> new DailyChallengeException(DailyChallengeError.DAILY_CHALLENGE_PERIOD_INDEX_NOT_FOUND));
}

Expand Down Expand Up @@ -85,12 +85,12 @@ private List<DailyChallenge> createDailyChallengeByChallengePeriod(Challenge cha
.toList();
}

public List<DailyChallenge> getDailyChallengesByChallengeIdOrderByChallengeDate(Long challengeId) {
return dailyChallengeRepository.findAllByChallengeIdOrderByChallengeDate(challengeId);
public List<DailyChallenge> getDailyChallengesByChallengeId(Long challengeId) {
return dailyChallengeRepository.findAllByChallengeId(challengeId);
}

public void changeInfoOfDailyChallenges(Long challengeId, List<Status> statuses, LocalDate challengeDate) {
List<DailyChallenge> dailyChallenges = this.getDailyChallengesByChallengeIdOrderByChallengeDate(challengeId);
List<DailyChallenge> dailyChallenges = this.getDailyChallengesByChallengeId(challengeId);
changeStatusOfDailyChallenges(dailyChallenges, statuses);
changeChallengeDateOfDailyChallenges(dailyChallenges, challengeDate);
}
Expand Down

0 comments on commit c03f2d1

Please sign in to comment.