Skip to content

Commit

Permalink
Merge pull request #159 from Team-HMH/feat/#158-send-challenge-is-suc…
Browse files Browse the repository at this point in the history
…cess

feat: 챌린지 성공 여부 전송 api 추가
  • Loading branch information
kseysh authored Jun 9, 2024
2 parents 7b42e37 + 88c34cc commit 7c99349
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import sopt.org.hmh.domain.dailychallenge.dto.request.FinishedDailyChallengeListRequest;
import sopt.org.hmh.domain.dailychallenge.dto.request.FinishedDailyChallengeStatusListRequest;
import sopt.org.hmh.global.auth.jwt.JwtConstants;
import sopt.org.hmh.global.common.response.BaseResponse;
import sopt.org.hmh.global.common.response.EmptyJsonResponse;
Expand All @@ -33,6 +34,13 @@ public interface DailyChallengeApi {
ResponseEntity<BaseResponse<EmptyJsonResponse>> orderAddHistoryDailyChallenge(
@Parameter(hidden = true) final Long userId,
final String os,
final FinishedDailyChallengeListRequest request);
final FinishedDailyChallengeListRequest request
);

ResponseEntity<BaseResponse<EmptyJsonResponse>> orderChangeStatusDailyChallenge(
@Parameter(hidden = true) final Long userId,
final String os,
final FinishedDailyChallengeStatusListRequest request
);
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.springframework.web.bind.annotation.*;
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.service.DailyChallengeFacade;
import sopt.org.hmh.domain.dailychallenge.service.DailyChallengeService;
import sopt.org.hmh.global.auth.UserId;
import sopt.org.hmh.global.common.response.BaseResponse;
import sopt.org.hmh.global.common.response.EmptyJsonResponse;
Expand All @@ -16,6 +18,7 @@
public class DailyChallengeController implements DailyChallengeApi {

private final DailyChallengeFacade dailyChallengeFacade;
private final DailyChallengeService dailyChallengeService;

@Override
@PostMapping("/finish")
Expand All @@ -29,4 +32,17 @@ public ResponseEntity<BaseResponse<EmptyJsonResponse>> orderAddHistoryDailyChall
.status(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS.getHttpStatus())
.body(BaseResponse.success(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS, new EmptyJsonResponse()));
}

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

import java.util.List;

public record FinishedDailyChallengeStatusListRequest(
List<FinishedDailyChallengeStatusRequest> finishedDailyChallenges
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sopt.org.hmh.domain.dailychallenge.dto.request;

import java.time.LocalDate;

public record FinishedDailyChallengeStatusRequest(
LocalDate challengeDate,
boolean isSuccess
) {

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package sopt.org.hmh.domain.dailychallenge.service;

import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.hmh.domain.dailychallenge.domain.DailyChallenge;
import sopt.org.hmh.domain.dailychallenge.domain.Status;
import sopt.org.hmh.domain.dailychallenge.domain.exception.DailyChallengeError;
import sopt.org.hmh.domain.dailychallenge.domain.exception.DailyChallengeException;
import sopt.org.hmh.domain.dailychallenge.dto.request.FinishedDailyChallengeStatusListRequest;
import sopt.org.hmh.domain.dailychallenge.repository.DailyChallengeRepository;

@Service
Expand All @@ -22,8 +24,16 @@ public DailyChallenge findByChallengeDateAndUserIdOrThrowException(LocalDate cha
.orElseThrow(() -> new DailyChallengeException(DailyChallengeError.DAILY_CHALLENGE_NOT_FOUND));
}

public void validateDailyChallengeStatus(DailyChallenge dailyChallenge, Status expected) {
if (dailyChallenge.getStatus() != expected) {
public void validateDailyChallengeStatus(Status dailyChallengeStatus, List<Status> expectedStatuses) {
boolean isAlreadyProcessed = true;
for (Status expected : expectedStatuses) {
if (dailyChallengeStatus == expected) {
isAlreadyProcessed = false;
break;
}
}

if (isAlreadyProcessed) {
throw new DailyChallengeException(DailyChallengeError.DAILY_CHALLENGE_ALREADY_PROCESSED);
}
}
Expand All @@ -42,4 +52,17 @@ private void handleAlreadyProcessedDailyChallenge(DailyChallenge dailyChallenge)
}
throw new DailyChallengeException(DailyChallengeError.DAILY_CHALLENGE_ALREADY_PROCESSED);
}

public void changeDailyChallengeStatusByIsSuccess(Long userId, FinishedDailyChallengeStatusListRequest requests) {
requests.finishedDailyChallenges().forEach(request -> {
DailyChallenge dailyChallenge = this.findByChallengeDateAndUserIdOrThrowException(request.challengeDate(), userId);
if (request.isSuccess()) {
this.validateDailyChallengeStatus(dailyChallenge.getStatus(), List.of(Status.NONE));
dailyChallenge.changeStatus(Status.UNEARNED);
} else {
this.validateDailyChallengeStatus(dailyChallenge.getStatus(), List.of(Status.NONE, Status.FAILURE));
dailyChallenge.changeStatus(Status.FAILURE);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public UsePointResponse usePointAndChallengeFailed(Long userId, LocalDate challe
DailyChallenge dailyChallenge = dailyChallengeService.findByChallengeDateAndUserIdOrThrowException(challengeDate, userId);
User user = userService.findByIdOrThrowException(userId);

dailyChallengeService.validateDailyChallengeStatus(dailyChallenge, Status.NONE);
dailyChallengeService.validateDailyChallengeStatus(dailyChallenge.getStatus(), List.of(Status.NONE));
dailyChallenge.changeStatus(Status.FAILURE);

return new UsePointResponse(
Expand All @@ -42,7 +42,7 @@ public EarnPointResponse earnPointAndChallengeEarned(Long userId, LocalDate chal
DailyChallenge dailyChallenge = dailyChallengeService.findByChallengeDateAndUserIdOrThrowException(challengeDate, userId);
User user = userService.findByIdOrThrowException(userId);

dailyChallengeService.validateDailyChallengeStatus(dailyChallenge, Status.UNEARNED);
dailyChallengeService.validateDailyChallengeStatus(dailyChallenge.getStatus(), List.of(Status.UNEARNED));
dailyChallenge.changeStatus(Status.EARNED);

return new EarnPointResponse(
Expand Down

0 comments on commit 7c99349

Please sign in to comment.