Skip to content

Commit

Permalink
feat: ✨ 루틴 달성률 추가 (#24)
Browse files Browse the repository at this point in the history
* fix: 🐛 빠진 코드 추가

* fix: 🐛 email 칼럼 다시 추가

* feat: ✨ 인가 서비스 추가 (#21)

* feat: ✨ 루틴 달성률 추가 (#23)
  • Loading branch information
LEEJaeHyeok97 authored Sep 27, 2024
1 parent 9317289 commit a367efc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import com.goormdari.domain.calendar.domain.Calendar;
import com.goormdari.domain.calendar.dto.response.CheckGoalProgressResponse;
import com.goormdari.domain.team.domain.QTeam;
import com.goormdari.domain.team.domain.Team;
import com.goormdari.domain.user.domain.QUser;
import com.goormdari.domain.user.domain.User;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.temporal.ChronoUnit;
import java.util.List;
Expand Down Expand Up @@ -60,15 +63,54 @@ public CheckGoalProgressResponse findByIdAndDate(Long userId, YearMonth date) {
))
.collect(Collectors.toList());

Double routineCompletionRate = calculateRoutineCompletionRate(userId, date);

return new CheckGoalProgressResponse(
userInfo.getNickname(),
userInfo.getProfileUrl(),
goal,
dDay,
dayAchiveList
dayAchiveList,
routineCompletionRate
);
}

private Double calculateRoutineCompletionRate(Long userId, YearMonth date) {
User user = queryFactory
.selectFrom(QUser.user)
.where(QUser.user.id.eq(userId))
.fetchOne();
Long teamId = user.getTeam().getId();

Team team = queryFactory
.selectFrom(QTeam.team)
.where(QTeam.team.id.eq(teamId))
.fetchOne();

LocalDateTime createdAt = team.getCreatedAt();

LocalDate deadLine = team.getDeadLine();
LocalDate createdAtDate = createdAt.toLocalDate();

// 일 수 차이 계산
Long daysBetween = ChronoUnit.DAYS.between(createdAtDate, deadLine);

// 루틴 4개 수행한 날짜 계산
Long successCount = queryFactory
.select(calendar.count())
.from(calendar)
.where(calendar.userId.eq(userId),
calendar.success_count.eq(4)
)
.fetchFirst();

Double rate = ((double) (successCount) / (double) (daysBetween)) * 100;

double roundedRate = Math.round(rate * 10) / 10.0;

return roundedRate;
}

private int calculateDDay(LocalDate deadLine) {
LocalDate now = LocalDate.now();
return (int) ChronoUnit.DAYS.between(now, deadLine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public record CheckGoalProgressResponse(
String profileUrl,
String goal,
int dDay,
List<DayAchive> dayAchiveList
List<DayAchive> dayAchiveList,
Double routineCompletionRate
) {
public record DayAchive(
LocalDate date,
Expand All @@ -21,11 +22,12 @@ public record DayAchive(
}

@QueryProjection
public CheckGoalProgressResponse(String nickname, String profileUrl, String goal, int dDay, List<DayAchive> dayAchiveList) {
public CheckGoalProgressResponse(String nickname, String profileUrl, String goal, int dDay, List<DayAchive> dayAchiveList, Double routineCompletionRate) {
this.nickname = nickname;
this.profileUrl = profileUrl;
this.goal = goal;
this.dDay = dDay;
this.dayAchiveList = dayAchiveList;
this.routineCompletionRate = routineCompletionRate;
}
}

0 comments on commit a367efc

Please sign in to comment.