Skip to content

Commit

Permalink
Merge pull request #271 from bounswe/issue#261-change-training-progra…
Browse files Browse the repository at this point in the history
…m-model

Issue#261 Change Training Program Model
  • Loading branch information
oguzhekim authored Dec 13, 2024
2 parents 706f98d + faf7bff commit 31c2093
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.group7.demo.dtos;

import com.group7.demo.models.enums.ProgramLevel;
import com.group7.demo.models.enums.ProgramType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.springframework.lang.NonNull;

import java.util.List;

Expand All @@ -11,16 +14,16 @@
@Builder
@AllArgsConstructor
public class TrainingProgramRequest {
@NonNull
private String title;
private List<ExerciseDto> exercises;
@NonNull
private String description;


@Data
@AllArgsConstructor
public static class ExerciseDto {
private Long id;
private int repetitions;
private int sets;
}
@NonNull
private ProgramType type;
@NonNull
private ProgramLevel level;
@NonNull
private int interval;
@NonNull
private List<WeekRequest> weeks;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.group7.demo.dtos;

import com.group7.demo.models.enums.ProgramLevel;
import com.group7.demo.models.enums.ProgramType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -14,9 +16,14 @@
public class TrainingProgramResponse {
private Long id;
private String title;
private List<ExerciseDetail> exercises;
private String description;
private String trainerUsername;
private Set<String> participants;
private String trainer;
private ProgramType type;
private ProgramLevel level;
private int interval;
private double rating;
private int ratingCount;
private LocalDateTime createdAt;
}
private List<WeekResponse> weeks;
private Set<String> participants;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.group7.demo.dtos;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.NonNull;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class WeekRequest {
@NonNull
private List<WorkoutRequest> workouts;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.group7.demo.dtos;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@AllArgsConstructor
@Builder
public class WeekResponse {
private Long id;
private int weekNumber;
private List<WorkoutResponse> workouts;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.group7.demo.dtos;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.NonNull;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class WorkoutExerciseRequest {
@NonNull
private Long exerciseId;
@NonNull
private int repetitions;
@NonNull
private int sets;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.group7.demo.dtos;

import com.group7.demo.models.Exercise;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@AllArgsConstructor
public class WorkoutExerciseResponse {
private Long id;
private int exerciseNumber;
private Exercise exercise;
private int repetitions;
private int sets;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.group7.demo.dtos;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.NonNull;

import java.util.List;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class WorkoutRequest {
@NonNull
private String name;
@NonNull
private List<WorkoutExerciseRequest> exercises;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.group7.demo.dtos;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@AllArgsConstructor
@Builder
public class WorkoutResponse {
private Long id;
private String name;
private int workoutNumber;
private List<WorkoutExerciseResponse> workoutExercises;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ public TrainingProgramResponse mapToTrainingProgramResponse(TrainingProgram prog
.id(program.getId())
.title(program.getTitle())
.description(program.getDescription())
.trainerUsername(program.getTrainer().getUsername())
.trainer(program.getTrainer().getUsername())
.type(program.getType())
.level(program.getLevel())
.interval(program.getInterval())
.rating(program.getRating())
.ratingCount(program.getRatingCount())
.createdAt(program.getCreatedAt())
.exercises(program.getExercises().stream()
.map(this::mapToExerciseDetailResponse)
.sorted(Comparator.comparing(ExerciseDetail::getId))
.weeks(program.getWeeks().stream()
.map(this::mapToWeekResponse)
.sorted(Comparator.comparing(WeekResponse::getWeekNumber))
.collect(Collectors.toList()))
.participants(program.getParticipants() == null ?
Set.of() :
Expand All @@ -55,12 +60,36 @@ public TrainingProgramResponse mapToTrainingProgramResponse(TrainingProgram prog
.build();
}

public ExerciseDetail mapToExerciseDetailResponse(TrainingProgramExercise trainingProgramExercise) {
return ExerciseDetail.builder()
.id(trainingProgramExercise.getId())
.exercise(trainingProgramExercise.getExercise())
.repetitions(trainingProgramExercise.getRepetitions())
.sets(trainingProgramExercise.getSets())
public WeekResponse mapToWeekResponse(Week week) {
return WeekResponse.builder()
.id(week.getId())
.weekNumber(week.getWeekNumber())
.workouts(week.getWorkouts().stream()
.map(this::mapToWorkoutResponse)
.sorted(Comparator.comparing(WorkoutResponse::getWorkoutNumber))
.collect(Collectors.toList()))
.build();
}

public WorkoutResponse mapToWorkoutResponse(Workout workout) {
return WorkoutResponse.builder()
.id(workout.getId())
.name(workout.getName())
.workoutNumber(workout.getWorkoutNumber())
.workoutExercises(workout.getExercises().stream()
.map(this::maptoWorkoutExerciseResponse)
.sorted(Comparator.comparing(WorkoutExerciseResponse::getExerciseNumber))
.collect(Collectors.toList()))
.build();
}

public WorkoutExerciseResponse maptoWorkoutExerciseResponse(WorkoutExercise workoutExercise) {
return WorkoutExerciseResponse.builder()
.id(workoutExercise.getId())
.exerciseNumber(workoutExercise.getExerciseNumber())
.exercise(workoutExercise.getExercise())
.repetitions(workoutExercise.getRepetitions())
.sets(workoutExercise.getSets())
.build();
}

Expand All @@ -69,7 +98,13 @@ public UserTrainingProgramResponse mapToUserTrainingProgramResponse(UserTraining
Map<Long, Boolean> completedExercises = userTrainingProgram.getExerciseProgress(); // Now returns Map<Long, Boolean>

// Use the new mapper function for exercises
List<UserExerciseDetail> exerciseDetails = program.getExercises().stream()
List<UserExerciseDetail> exerciseDetails = program.getWeeks().stream()
.map(Week::getWorkouts)
.map(workouts -> workouts.stream()
.map(Workout::getExercises)
.flatMap(List::stream)
.collect(Collectors.toList()))
.flatMap(List::stream)
.map(exercise -> mapToUserExerciseDetailResponse(exercise, completedExercises))
.sorted(Comparator.comparing(UserExerciseDetail::getId))
.collect(Collectors.toList());
Expand All @@ -90,7 +125,7 @@ public UserTrainingProgramResponse mapToUserTrainingProgramResponse(UserTraining
.build();
}

public UserExerciseDetail mapToUserExerciseDetailResponse(TrainingProgramExercise trainingProgramExercise, Map<Long, Boolean> completedExercises) {
public UserExerciseDetail mapToUserExerciseDetailResponse(WorkoutExercise trainingProgramExercise, Map<Long, Boolean> completedExercises) {
return UserExerciseDetail.builder()
.id(trainingProgramExercise.getId())
.exercise(trainingProgramExercise.getExercise())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.group7.demo.models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.group7.demo.models.enums.ProgramLevel;
import com.group7.demo.models.enums.ProgramType;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;
import java.util.HashSet;
Expand All @@ -22,17 +26,28 @@ public class TrainingProgram {

private String title;

// @Lob
@Column(columnDefinition = "TEXT")
private String description;

@ManyToOne
@JoinColumn(name = "trainer_id")
private User trainer;

private double rating;

private int ratingCount;

private ProgramType type;

private ProgramLevel level;

private int interval;

@OneToMany(mappedBy = "trainingProgram", cascade = CascadeType.ALL)
private List<TrainingProgramExercise> exercises;
@JsonManagedReference
private List<Week> weeks;

@CreationTimestamp
private LocalDateTime createdAt;

@OneToMany(mappedBy = "trainingProgram", cascade = CascadeType.ALL)
Expand Down
31 changes: 31 additions & 0 deletions backend/demo-group7/src/main/java/com/group7/demo/models/Week.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.group7.demo.models;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.*;

import java.util.List;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
public class Week {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private int weekNumber;

@ManyToOne
@JoinColumn(name = "training_program_id", nullable = false)
@JsonBackReference
private TrainingProgram trainingProgram;

@OneToMany(mappedBy = "week", cascade = CascadeType.ALL)
@JsonManagedReference
private List<Workout> workouts;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.group7.demo.models;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.*;

import java.util.List;

@Entity
@NoArgsConstructor
@Builder
@AllArgsConstructor
@Getter
@Setter
public class Workout {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

private int workoutNumber;

@ManyToOne
@JoinColumn(name = "week_id")
@JsonBackReference
private Week week;

@OneToMany(mappedBy = "workout", cascade = CascadeType.ALL)
@JsonManagedReference
private List<WorkoutExercise> exercises;
}
Loading

0 comments on commit 31c2093

Please sign in to comment.