Skip to content

Commit

Permalink
Merge pull request #50 from Team-HMH/feat/#20-get-challenge-api
Browse files Browse the repository at this point in the history
�feat - 달성현황 정보 불러오기
  • Loading branch information
jumining authored Jan 15, 2024
2 parents 16127e2 + d04abb8 commit e05315d
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public record AppGoalTimeResponse(
String appCode,
Long goalTime
) {
static AppGoalTimeResponse of(App app) {
public static AppGoalTimeResponse of(App app) {
return new AppGoalTimeResponse(app.getAppCode(), app.getGoalTime());
}
}
6 changes: 2 additions & 4 deletions src/main/java/sopt/org/HMH/domain/app/service/AppService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public void addAppByChallengeId(Long dailyChallengeId, List<AppGoalTimeRequest>

@Transactional
public void removeApp(Long userId, AppDeleteRequest request, String os) {
Long todayDailyChallengeId = IdConverter.getTodayDailyChallenge(challengeRepository,
dailyChallengeRepository, userId).getId();
Long todayDailyChallengeId = IdConverter.getTodayDailyChallengeByUserId(challengeRepository, userId).getId();
App app = appRepository.findByDailyChallengeIdAndAppCodeAndOs(todayDailyChallengeId, request.appCode(), os);

appRepository.deleteById(app.getId());
Expand All @@ -48,8 +47,7 @@ public void removeApp(Long userId, AppDeleteRequest request, String os) {
public void addAppsByUserId(Long userId, List<AppGoalTimeRequest> requests, String os) {
for (AppGoalTimeRequest request : requests) {
appRepository.save(App.builder()
.dailyChallenge(IdConverter.getTodayDailyChallenge(challengeRepository,
dailyChallengeRepository, userId))
.dailyChallenge(IdConverter.getTodayDailyChallengeByUserId(challengeRepository, userId))
.appCode(request.appCode())
.goalTime(request.goalTime())
.os(os).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
Expand All @@ -10,6 +11,7 @@
import sopt.org.HMH.domain.challenge.domain.exception.ChallengeSuccess;
import sopt.org.HMH.domain.challenge.dto.request.ChallengeRequest;
import sopt.org.HMH.domain.challenge.dto.response.AddChallengeResponse;
import sopt.org.HMH.domain.challenge.dto.response.ChallengeResponse;
import sopt.org.HMH.domain.challenge.service.ChallengeService;
import sopt.org.HMH.global.common.response.ApiResponse;
import sopt.org.HMH.global.util.IdConverter;
Expand All @@ -34,4 +36,15 @@ public ResponseEntity<ApiResponse<AddChallengeResponse>> orderAddChallenge(
.body(ApiResponse.success(ChallengeSuccess.ADD_CHALLENGE_SUCCESS,
challengeService.addChallenge(IdConverter.getUserId(principal), request, os)));
}

@GetMapping
public ResponseEntity<ApiResponse<ChallengeResponse>> orderGetChallenge(
Principal principal,
@RequestHeader("OS") final String os
) {
return ResponseEntity
.status(ChallengeSuccess.GET_CHALLENGE_SUCCESS.getHttpStatus())
.body(ApiResponse.success(ChallengeSuccess.GET_CHALLENGE_SUCCESS,
challengeService.getChallenge(IdConverter.getUserId(principal), os)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@AllArgsConstructor
public enum ChallengeSuccess implements SuccessBase {

GET_CHALLENGE_SUCCESS(HttpStatus.OK, "챌린지 달성현황 조회 성공하였습니다."),
ADD_CHALLENGE_SUCCESS(HttpStatus.OK, "챌린지 생성을 성공하였습니다."),
;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sopt.org.HMH.domain.challenge.dto.response;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.val;
import sopt.org.HMH.domain.app.dto.response.AppGoalTimeResponse;
import sopt.org.HMH.domain.challenge.domain.Challenge;
import sopt.org.HMH.domain.dailychallenge.domain.Status;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Builder(access = AccessLevel.PRIVATE)
public record ChallengeResponse(
Integer period,
List<Status> statuses,
Integer todayIndex,
Long goalTime,
List<AppGoalTimeResponse> apps
) {
public static ChallengeResponse of(Challenge challenge) {
val dailyChallenges = challenge.getDailyChallenges();

val statuses = new ArrayList<Status>();
for (val dailyChallenge : dailyChallenges) {
statuses.add(dailyChallenge.getStatus());
}

val startDayOfChallenge = challenge.getDailyChallenges().get(0);
val todayIndex = calculateDaysSinceToday(startDayOfChallenge.getCreatedAt());
val todayDailyChallenge = dailyChallenges.get(todayIndex);

return ChallengeResponse.builder()
.period(challenge.getPeriod())
.statuses(statuses)
.todayIndex(todayIndex)
.goalTime(todayDailyChallenge.getGoalTime())
.apps(todayDailyChallenge.getApps()
.stream()
.map(AppGoalTimeResponse::of)
.toList())
.build();
}

private static Integer calculateDaysSinceToday(LocalDateTime dateToCompare) {
return (int) ChronoUnit.DAYS.between(LocalDateTime.now().toLocalDate(), dateToCompare.toLocalDate());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package sopt.org.HMH.domain.challenge.service;

import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.HMH.domain.challenge.domain.Challenge;
import sopt.org.HMH.domain.challenge.dto.request.ChallengeRequest;
import sopt.org.HMH.domain.challenge.dto.response.AddChallengeResponse;
import sopt.org.HMH.domain.challenge.dto.response.ChallengeResponse;
import sopt.org.HMH.domain.challenge.repository.ChallengeRepository;
import sopt.org.HMH.domain.dailychallenge.service.DailyChallengeService;

Expand All @@ -16,16 +18,18 @@ public class ChallengeService {
private final ChallengeRepository challengeRepository;
private final DailyChallengeService dailyChallengeService;


@Transactional
public AddChallengeResponse addChallenge(Long userId,
ChallengeRequest request,
String os) {
public AddChallengeResponse addChallenge(Long userId, ChallengeRequest request, String os) {
Challenge challenge = challengeRepository.save(Challenge.builder()
.period(request.period())
.userId(userId).build());
dailyChallengeService.addDailyChallenge(challenge, request.goalTime(), request.apps(), os);

dailyChallengeService.addDailyChallengesForPeriod(challenge, request, os);
return AddChallengeResponse.of(challenge.getId());
}
}

public ChallengeResponse getChallenge(Long userId, String os) {
val challenge = challengeRepository.findFirstByUserIdOrderByCreatedAtDesc(userId);

return ChallengeResponse.of(challenge);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ResponseEntity<ApiResponse<DailyChallengeResponse>> orderDetailDailyChall
return ResponseEntity
.status(DailyChallengeSuccess.GET_DAILY_CHALLENGE_SUCCESS.getHttpStatus())
.body(ApiResponse.success(DailyChallengeSuccess.GET_DAILY_CHALLENGE_SUCCESS,
dailyChallengeService.getDailyChallenge(IdConverter.getUserId(principal))));
dailyChallengeService.getDailyChallenge(IdConverter.getUserId(principal), os)));
}

@PatchMapping("/failure")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.AccessLevel;
import lombok.Builder;
import sopt.org.HMH.domain.app.domain.App;
import sopt.org.HMH.domain.app.dto.response.AppGoalTimeResponse;
import sopt.org.HMH.domain.dailychallenge.domain.DailyChallenge;

import java.util.List;
Expand All @@ -13,20 +13,15 @@ public record DailyChallengeResponse(
Long goalTime,
List<AppGoalTimeResponse> apps
) {
public static DailyChallengeResponse of(DailyChallenge dailyChallenge) {
public static DailyChallengeResponse of(DailyChallenge dailyChallenge, String os) {
return DailyChallengeResponse.builder()
.status(dailyChallenge.getStatus().toString())
.goalTime(dailyChallenge.getGoalTime())
.apps(dailyChallenge.getApps().stream().map(AppGoalTimeResponse::of).toList())
.apps(dailyChallenge.getApps()
.stream()
.filter(app -> os.equals(app.getOs()))
.map(AppGoalTimeResponse::of)
.toList())
.build();
}

public record AppGoalTimeResponse(
String appCode,
Long goalTime
) {
static AppGoalTimeResponse of(App app) {
return new AppGoalTimeResponse(app.getAppCode(), app.getGoalTime());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package sopt.org.HMH.domain.dailychallenge.service;

import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.HMH.domain.app.dto.request.AppGoalTimeRequest;
import sopt.org.HMH.domain.app.service.AppService;
import sopt.org.HMH.domain.challenge.domain.Challenge;
import sopt.org.HMH.domain.challenge.dto.request.ChallengeRequest;
import sopt.org.HMH.domain.challenge.repository.ChallengeRepository;
import sopt.org.HMH.domain.dailychallenge.domain.DailyChallenge;
import sopt.org.HMH.domain.dailychallenge.domain.Status;
Expand All @@ -25,27 +26,27 @@ public class DailyChallengeService {
private final ChallengeRepository challengeRepository;

@Transactional
public Long addDailyChallenge(Challenge challenge, Long goalTime, List<AppGoalTimeRequest> apps, String os) {
DailyChallenge dailyChallenge = dailyChallengeRepository.save(DailyChallenge.builder()
.challenge(challenge)
.goalTime(goalTime)
.build());
appService.addAppByChallengeId(dailyChallenge.getId(), apps, os);

return dailyChallenge.getId();
public List<DailyChallenge> addDailyChallengesForPeriod(Challenge challenge, ChallengeRequest request, String os) {
for (int count = 0; count <= request.period(); count++) {
DailyChallenge dailyChallenge = dailyChallengeRepository.save(DailyChallenge.builder()
.challenge(challenge)
.goalTime(request.goalTime())
.build());
appService.addAppByChallengeId(dailyChallenge.getId(), request.apps(), os);
}

return challenge.getDailyChallenges();
}

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

return DailyChallengeResponse.of(dailyChallenge);
return DailyChallengeResponse.of(dailyChallenge, os);
}

@Transactional
public void modifyDailyChallengeStatus(Long userId) {
DailyChallenge dailyChallenge = IdConverter.getTodayDailyChallenge(challengeRepository,
dailyChallengeRepository, userId);
DailyChallenge dailyChallenge = IdConverter.getTodayDailyChallengeByUserId(challengeRepository, userId);
dailyChallenge.modifyStatus(Status.FAILURE);
}
}
9 changes: 6 additions & 3 deletions src/main/java/sopt/org/HMH/global/util/IdConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import sopt.org.HMH.global.auth.jwt.exception.JwtException;

import java.security.Principal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import static java.util.Objects.isNull;

Expand All @@ -20,11 +22,12 @@ public static Long getUserId(Principal principal) {
return Long.valueOf(principal.getName());
}

public static DailyChallenge getTodayDailyChallenge(ChallengeRepository challengeRepository,
DailyChallengeRepository dailyChallengeRepository,
public static DailyChallenge getTodayDailyChallengeByUserId(ChallengeRepository challengeRepository,
final Long userId) {
val challenge = challengeRepository.findFirstByUserIdOrderByCreatedAtDesc(userId);
val startDateOfChallenge = challenge.getCreatedAt().toLocalDate();

return dailyChallengeRepository.findFirstByChallengeIdOrderByCreatedAtDesc(challenge.getId());
val todayDailyChallengeIndex = (int) ChronoUnit.DAYS.between(LocalDateTime.now().toLocalDate(), startDateOfChallenge);
return challenge.getDailyChallenges().get(todayDailyChallengeIndex);
}
}

0 comments on commit e05315d

Please sign in to comment.