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 - 일별 챌린지 실패 처리 기능 구현 #45

Merged
merged 14 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

public interface AppRepository extends JpaRepository<App, Long> {

App findByDailyChallengeIdAndAppCodeAndOs(Long dayChallengeId, String appCode, String os);
App findByDailyChallengeIdAndAppCodeAndOs(Long dailyChallengeId, String appCode, String os);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋㅋㅋ굳...

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package sopt.org.HMH.domain.challenge.domain;

import static jakarta.persistence.FetchType.*;
import static jakarta.persistence.GenerationType.*;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import sopt.org.HMH.domain.user.domain.User;
import sopt.org.HMH.global.common.domain.BaseTimeEntity;
import sopt.org.HMH.domain.dailychallenge.domain.DailyChallenge;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package sopt.org.HMH.domain.challenge.repository;

import java.time.LocalDateTime;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import sopt.org.HMH.domain.challenge.domain.Challenge;
import sopt.org.HMH.domain.challenge.domain.exception.ChallengeError;
import sopt.org.HMH.domain.challenge.domain.exception.ChallengeException;

public interface ChallengeRepository extends JpaRepository<Challenge, Long> {

default Challenge findByUserIdAndCreatedAtBetweenOrThrowException(Long userId, LocalDateTime startDate, LocalDateTime endDate) {
return findByUserIdAndCreatedAtBetween(userId, startDate, endDate).orElseThrow(() -> new ChallengeException(
ChallengeError.CHALLENGE_NOT_FOUND));
}
Optional<Challenge> findByUserIdAndCreatedAtBetween(Long userId, LocalDateTime startDate, LocalDateTime endDate);

Challenge findFirstByUserIdOrderByCreatedAtDesc(Long userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@
import sopt.org.HMH.domain.challenge.dto.response.AddChallengeResponse;
import sopt.org.HMH.domain.challenge.repository.ChallengeRepository;
import sopt.org.HMH.domain.dailychallenge.service.DailyChallengeService;
import sopt.org.HMH.domain.user.domain.User;
import sopt.org.HMH.domain.user.repository.UserRepository;

@Service
@RequiredArgsConstructor
public class ChallengeService {

private final ChallengeRepository challengeRepository;
private final UserRepository userRepository;

private final DailyChallengeService dailyChallengeService;


@Transactional
public AddChallengeResponse addChallenge(Long userId,
ChallengeRequest request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sopt.org.HMH.domain.dailychallenge.domain.exception.DailyChallengeSuccess;
import sopt.org.HMH.domain.dailychallenge.dto.response.DailyChallengeResponse;
import sopt.org.HMH.domain.dailychallenge.repository.DailyChallengeRepository;
import sopt.org.HMH.domain.dailychallenge.service.DailyChallengeService;
import sopt.org.HMH.global.common.response.ApiResponse;
import sopt.org.HMH.global.common.response.EmptyJsonResponse;
import sopt.org.HMH.global.util.IdConverter;

import java.security.Principal;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/dailyChallenge")
@RequestMapping("/api/v1/dailychallenge")
Comment on lines -20 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

일반적인 컨벤션과 맞지 않는게 다른 데도 있어서 한 이슈에서 다 수정하려고 했는데 수정하셨군요 url 수정됐다구 클라에게 따로 말해줘야 할 것 같아여

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 제가 그건 차마 생각을 못했네요.,..!! 노티 정말 감사합니다!!

public class DailyChallengeController {

private final DailyChallengeService dailyChallengeService;
Expand All @@ -28,8 +29,18 @@ public ResponseEntity<ApiResponse<DailyChallengeResponse>> orderDetailDailyChall
@RequestHeader("OS") final String os
) {
return ResponseEntity
.status(DailyChallengeSuccess.GET_DAY_CHALLENGE_SUCCESS.getHttpStatus())
.body(ApiResponse.success(DailyChallengeSuccess.GET_DAY_CHALLENGE_SUCCESS,
dailyChallengeService.getDailyChallenge(IdConverter.getUserId(principal), os)));
.status(DailyChallengeSuccess.GET_DAILY_CHALLENGE_SUCCESS.getHttpStatus())
.body(ApiResponse.success(DailyChallengeSuccess.GET_DAILY_CHALLENGE_SUCCESS,
dailyChallengeService.getDailyChallenge(IdConverter.getUserId(principal))));
}

@PatchMapping("/failure")
public ResponseEntity<ApiResponse<?>> orderChallengeDailyChallenge(
Principal principal
) {
dailyChallengeService.modifyDailyChallengeStatus(IdConverter.getUserId(principal));
return ResponseEntity
.status(DailyChallengeSuccess.MODIFY_DAILY_CHALLENGE_STATUS_SUCCESS.getHttpStatus())
.body(ApiResponse.success(DailyChallengeSuccess.MODIFY_DAILY_CHALLENGE_STATUS_SUCCESS, new EmptyJsonResponse()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public DailyChallenge(Challenge challenge, Long goalTime) {
this.goalTime = goalTime;
this.status = Status.NONE;
}

public void modifyStatus(Status status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
@AllArgsConstructor
public enum DailyChallengeSuccess implements SuccessBase {

GET_DAY_CHALLENGE_SUCCESS(HttpStatus.OK, "오늘의 일별 챌린지 이용 통계 조회를 성공하였습니다."),
GET_DAILY_CHALLENGE_SUCCESS(HttpStatus.OK, "오늘의 일별 챌린지 이용 통계 조회를 성공하였습니다."),
MODIFY_DAILY_CHALLENGE_STATUS_SUCCESS(HttpStatus.OK, "오늘의 일별 챌린지 STATUS 실패 처리를 성공하였습니다"),
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sopt.org.HMH.domain.challenge.domain.Challenge;
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.dto.response.DailyChallengeResponse;
import sopt.org.HMH.domain.dailychallenge.repository.DailyChallengeRepository;
import sopt.org.HMH.global.util.IdConverter;
Expand All @@ -16,6 +17,7 @@

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class DailyChallengeService {

private final DailyChallengeRepository dailyChallengeRepository;
Expand All @@ -33,10 +35,17 @@ public Long addDailyChallenge(Challenge challenge, Long goalTime, List<AppGoalTi
return dailyChallenge.getId();
}

public DailyChallengeResponse getDailyChallenge(Long userId, String os) {
public DailyChallengeResponse getDailyChallenge(Long userId) {
DailyChallenge dailyChallenge = IdConverter.getTodayDailyChallenge(challengeRepository,
dailyChallengeRepository, userId);

return DailyChallengeResponse.of(dailyChallenge);
}

@Transactional
public void modifyDailyChallengeStatus(Long userId) {
DailyChallenge dailyChallenge = IdConverter.getTodayDailyChallenge(challengeRepository,
dailyChallengeRepository, userId);
dailyChallenge.modifyStatus(Status.FAILURE);
}
}