Skip to content

Commit

Permalink
Merge pull request #273 from bounswe/create-complete-exercise-endpoint
Browse files Browse the repository at this point in the history
Created complete exercise endpoint
  • Loading branch information
oguzhekim authored Dec 13, 2024
2 parents c7672c2 + 06bacb4 commit a0ac0e1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,15 @@ public ResponseEntity<List<TrainingProgramWithTrackingResponse>> getJoinedTraini
List<TrainingProgramWithTrackingResponse> responses = trainingProgramService.getJoinedTrainingPrograms(username);
return ResponseEntity.ok(responses);
}

@PostMapping("/{programId}/workout-exercises/{workoutExerciseId}/complete")
public ResponseEntity<TrainingProgramWithTrackingResponse> completeExercise(
@PathVariable Long programId,
@PathVariable Long workoutExerciseId,
@RequestBody List<Integer> completedSets,
HttpServletRequest request)
{
TrainingProgramWithTrackingResponse response = trainingProgramService.completeExercise(programId, workoutExerciseId, request, completedSets);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.group7.demo.repository;

import com.group7.demo.models.TrainingProgramWithTracking;
import com.group7.demo.models.WorkoutExerciseWithTracking;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface WorkoutExerciseWithTrackingRepository extends JpaRepository<WorkoutExerciseWithTracking, Long> {
Optional<WorkoutExerciseWithTracking> findByWorkoutWithTracking_WeekWithTracking_TrainingProgramWithTrackingAndWorkoutExercise_Id(TrainingProgramWithTracking trainingProgramWithTracking, Long workoutExerciseId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,78 @@ public TrainingProgramWithTrackingResponse leaveTrainingProgram(Long trainingPro
return mapper.mapToTrainingProgramWithTrackingResponse(trainingProgramWithTrackingRepository.save(trainingProgramWithTracking));
}

@Transactional
public TrainingProgramWithTrackingResponse completeExercise(Long trainingProgramId, Long workoutExerciseId, HttpServletRequest request, List<Integer> completedSets) {
// Get the authenticated user
User user = authenticationService.getAuthenticatedUserInternal(request);

// Retrieve the training program with tracking for the user
TrainingProgramWithTracking trainingProgramWithTracking = getOngoingUserTrainingProgram(user, trainingProgramId);

// Retrieve the WorkoutExerciseWithTracking entity
WorkoutExerciseWithTracking workoutExerciseWithTracking = workoutExerciseWithTrackingRepository
.findByWorkoutWithTracking_WeekWithTracking_TrainingProgramWithTrackingAndWorkoutExercise_Id(
trainingProgramWithTracking, workoutExerciseId)
.orElseThrow(() -> new EntityNotFoundException("Workout exercise not found with the specified ID in the training program."));

// Check if the exercise is already completed
if (workoutExerciseWithTracking.getCompletedAt() != null) {
throw new IllegalStateException("This workout exercise has already been completed.");
}

if (completedSets.size() != workoutExerciseWithTracking.getWorkoutExercise().getSets()) {
throw new IllegalArgumentException("Number of completed sets does not match the required number of sets for the exercise.");
}

WorkoutWithTracking currentWorkout = getCurrentWorkout(trainingProgramWithTracking);
if (!currentWorkout.equals(workoutExerciseWithTracking.getWorkoutWithTracking())) {
throw new IllegalStateException("You can't complete this exercise without completing previous workouts.");
}

// Update completed sets and mark the exercise as completed
workoutExerciseWithTracking.setCompletedSets(completedSets);
workoutExerciseWithTracking.setCompletedAt(LocalDateTime.now());
workoutExerciseWithTrackingRepository.save(workoutExerciseWithTracking);

// Check if all exercises in the workout are completed, and update workout tracking
WorkoutWithTracking workoutWithTracking = workoutExerciseWithTracking.getWorkoutWithTracking();
boolean allExercisesCompleted = workoutWithTracking.getWorkoutExercisesWithTracking().stream()
.allMatch(exercise -> exercise.getCompletedAt() != null);

if (allExercisesCompleted && workoutWithTracking.getCompletedAt() == null) {
workoutWithTracking.setCompletedAt(LocalDateTime.now());
workoutWithTrackingRepository.save(workoutWithTracking);
}

// Check if all workouts in the week are completed, and update week tracking
WeekWithTracking weekWithTracking = workoutWithTracking.getWeekWithTracking();
boolean allWorkoutsCompleted = weekWithTracking.getWorkoutsWithTracking().stream()
.allMatch(workout -> workout.getCompletedAt() != null);

if (allWorkoutsCompleted && weekWithTracking.getCompletedAt() == null) {
weekWithTracking.setCompletedAt(LocalDateTime.now());
weekWithTrackingRepository.save(weekWithTracking);
}

// Check if all weeks in the program are completed, and update program tracking
boolean allWeeksCompleted = trainingProgramWithTracking.getWeeksWithTracking().stream()
.allMatch(week -> week.getCompletedAt() != null);

if (allWeeksCompleted && trainingProgramWithTracking.getCompletedAt() == null) {
trainingProgramWithTracking.setCompletedAt(LocalDateTime.now());
trainingProgramWithTracking.setStatus(TrainingProgramWithTrackingStatus.COMPLETED);
trainingProgramWithTrackingRepository.save(trainingProgramWithTracking);
}

return mapper.mapToTrainingProgramWithTrackingResponse(trainingProgramWithTracking);
}

private WorkoutWithTracking getCurrentWorkout(TrainingProgramWithTracking trainingProgramWithTracking) {
return trainingProgramWithTracking.getWeeksWithTracking().stream()
.filter(week -> week.getCompletedAt() == null)
.flatMap(week -> week.getWorkoutsWithTracking().stream())
.filter(workout -> workout.getCompletedAt() == null)
.findFirst()
.orElseThrow(() -> new IllegalStateException("All workouts are completed in the training program."));
}
}

0 comments on commit a0ac0e1

Please sign in to comment.