Skip to content

Commit

Permalink
Merge pull request #302 from bounswe/issue#301-add-last-completed-wor…
Browse files Browse the repository at this point in the history
…kout-date-to-program-response

Added lastCompletedWorkoutDate to TrainingProgramWithTrackingResponse
  • Loading branch information
oguzhekim authored Dec 15, 2024
2 parents 7da94c0 + 4952798 commit 887b150
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Builder;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
Expand All @@ -28,6 +29,7 @@ public class TrainingProgramWithTrackingResponse {
private TrainingProgramWithTrackingStatus status;
private LocalDateTime joinedAt;
private LocalDateTime completedAt;
private LocalDate lastCompletedWorkoutDate;
private List<WeekWithTrackingResponse> weeks;
private Set<String> participants;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -107,6 +108,7 @@ public TrainingProgramWithTrackingResponse mapToTrainingProgramWithTrackingRespo
.joinedAt(trainingProgramWithTracking.getJoinedAt())
.status(trainingProgramWithTracking.getStatus())
.completedAt(trainingProgramWithTracking.getCompletedAt())
.lastCompletedWorkoutDate(getLastCompletedWorkoutDate(trainingProgramWithTracking))
.weeks(trainingProgramWithTracking.getWeeksWithTracking().stream()
.map(this::mapToWeekWithTrackingResponse)
.sorted(Comparator.comparing(WeekWithTrackingResponse::getWeekNumber))
Expand Down Expand Up @@ -157,6 +159,15 @@ private WorkoutExerciseWithTrackingResponse mapToWorkoutExerciseWithTracking(Wor
.build();
}

private LocalDate getLastCompletedWorkoutDate(TrainingProgramWithTracking trainingProgramWithTracking) {
return trainingProgramWithTracking.getWeeksWithTracking().stream()
.flatMap(weekWithTracking -> weekWithTracking.getWorkoutsWithTracking().stream())
.filter(workoutWithTracking -> workoutWithTracking.getCompletedAt() != null)
.map(workoutWithTracking -> workoutWithTracking.getCompletedAt().toLocalDate())
.max(LocalDate::compareTo)
.orElse(null);
}


public FeedbackResponse mapToFeedbackResponse(Feedback feedback) {
return FeedbackResponse.builder()
Expand Down

0 comments on commit 887b150

Please sign in to comment.