-
Notifications
You must be signed in to change notification settings - Fork 2
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 - 달성현황 정보 불러오기 #50
Changes from 10 commits
cfa4cdd
800ae81
41a0b6a
237f683
856b3e4
02251b4
8f12863
e504bb3
fa4ce85
c8feab6
7854d3e
fd0b12f
084d584
699e03b
33a5d8e
edb91b0
f80bc89
513e1c7
d04abb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package sopt.org.HMH.domain.challenge.dto.response; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import sopt.org.HMH.domain.app.dto.response.AppGoalTimeResponse; | ||
import sopt.org.HMH.domain.challenge.domain.Challenge; | ||
import sopt.org.HMH.domain.dailychallenge.domain.DailyChallenge; | ||
import sopt.org.HMH.domain.dailychallenge.domain.Status; | ||
|
||
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, DailyChallenge dailyChallenge, List<Status> statuses, Integer todayIndex) { | ||
return ChallengeResponse.builder() | ||
.period(challenge.getPeriod()) | ||
.statuses(statuses) | ||
.todayIndex(todayIndex) | ||
.goalTime(dailyChallenge.getGoalTime()) | ||
.apps(dailyChallenge.getApps().stream().map(AppGoalTimeResponse::of).toList()) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,58 @@ | ||
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.domain.DailyChallenge; | ||
import sopt.org.HMH.domain.dailychallenge.domain.Status; | ||
import sopt.org.HMH.domain.dailychallenge.repository.DailyChallengeRepository; | ||
import sopt.org.HMH.domain.dailychallenge.service.DailyChallengeService; | ||
import sopt.org.HMH.domain.user.domain.User; | ||
import sopt.org.HMH.domain.user.repository.UserRepository; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChallengeService { | ||
|
||
private final ChallengeRepository challengeRepository; | ||
private final UserRepository userRepository; | ||
|
||
private final DailyChallengeService dailyChallengeService; | ||
private final DailyChallengeRepository dailyChallengeRepository; | ||
|
||
@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); | ||
for (int count = 0; count <= request.period(); count++) | ||
dailyChallengeService.addDailyChallenge(challenge, request.goalTime(), request.apps(), os); | ||
|
||
return AddChallengeResponse.of(challenge.getId()); | ||
} | ||
|
||
public ChallengeResponse getChallenge(Long userId, String os) { | ||
val challenge = challengeRepository.findFirstByUserIdOrderByCreatedAtDesc(userId); | ||
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 = calculateTodayDailyChallengeIndex(startDayOfChallenge.getCreatedAt()); | ||
return ChallengeResponse.of(challenge, dailyChallenges.get(todayIndex), statuses, todayIndex); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: response를 위한 response 값 세팅은 response dto내에서 한다면 전달하는 매개변수가 적어질 수 있을 것 같습니다! |
||
|
||
private Integer calculateTodayDailyChallengeIndex(LocalDateTime startDateOfChallenge) { | ||
return (int) ChronoUnit.DAYS.between(LocalDateTime.now().toLocalDate(), startDateOfChallenge.toLocalDate()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -20,11 +22,13 @@ public static Long getUserId(Principal principal) { | |
return Long.valueOf(principal.getName()); | ||
} | ||
|
||
public static DailyChallenge getTodayDailyChallenge(ChallengeRepository challengeRepository, | ||
public static DailyChallenge getTodayDailyChallengeByUserId(ChallengeRepository challengeRepository, | ||
DailyChallengeRepository dailyChallengeRepository, | ||
final Long userId) { | ||
val challenge = challengeRepository.findFirstByUserIdOrderByCreatedAtDesc(userId); | ||
val startDateOfChallenge = challenge.getDailyChallenges().get(0).getCreatedAt().toLocalDate(); | ||
|
||
return dailyChallengeRepository.findFirstByChallengeIdOrderByCreatedAtDesc(challenge.getId()); | ||
val todayDailyChallengeIndex = (int) ChronoUnit.DAYS.between(LocalDateTime.now().toLocalDate(), startDateOfChallenge); | ||
return challenge.getDailyChallenges().get(todayDailyChallengeIndex); | ||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: 이 부분에 사용되는 sql query가 너무 많다고 생각됩니다. dailyChllenge에 오늘이 챌린지 기간 내 며칠째인지의 필드를 추가하는 것을 고려해보는 것은 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sql query는 가장 최신의 챌린지를 구하는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하 제가 sql 쿼리라고 잘못 작성했네요...
물론 저도 아직 성능 테스트를 해보지는 않았지만... 로직의 복잡성을 증가시키는 것 보다 DB에 필드를 하나 더 추가하는 것이 성능에 더 유리할 것 같다 생각한 것 같아요. 또 인덱스를 구하는 것은 자주 사용되는 로직일 것 같아서요! 지금 보니 하나의 메서드를 추가로 사용하는 것이면 굳이 DB에 추가하지 않아도 될 것 같습니다! :) |
||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: repository를 매개변수로 넣어준다는 로직이 조금 어색한 것 같아요. 제가 봤었던 코드들에서는 보통 repository는 생성자 주입을 통해 관리했었기 때문입니다...! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: statuses도 DailyChallengeResponse의 apps 반환한 방식처럼 response내에서 변환하는 것이 더 가독성 및 service 클래스의 책임 측면에서 더 좋을 것 같습니다!