Skip to content

Commit

Permalink
Merge pull request #170 from bounswe/issue#160-create-training-program
Browse files Browse the repository at this point in the history
Issue#160 create training program
  • Loading branch information
m1u1s1 authored Oct 21, 2024
2 parents 3b62fed + 8023b34 commit 927c724
Show file tree
Hide file tree
Showing 17 changed files with 449 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.group7.demo.controllers;

import com.group7.demo.dtos.TrainingProgramRequest;
import com.group7.demo.dtos.TrainingProgramResponse;
import com.group7.demo.services.TrainingProgramService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/training-programs")
@AllArgsConstructor
public class TrainingProgramController {

private final TrainingProgramService trainingProgramService;

// Endpoint to create a new training program
@PostMapping
public ResponseEntity<TrainingProgramResponse> createTrainingProgram(@RequestBody TrainingProgramRequest request) {
TrainingProgramResponse createdProgram = trainingProgramService.createTrainingProgram(request);
return ResponseEntity.status(HttpStatus.CREATED).body(createdProgram);
}

// Optional: Endpoint to fetch a list of training programs
@GetMapping
public ResponseEntity<List<TrainingProgramResponse>> getAllTrainingPrograms() {
List<TrainingProgramResponse> trainingPrograms = trainingProgramService.getAllTrainingPrograms();
return ResponseEntity.ok(trainingPrograms);
}

// Optional: Endpoint to fetch a specific training program by ID
@GetMapping("/{id}")
public ResponseEntity<TrainingProgramResponse> getTrainingProgramById(@PathVariable Long id) {
TrainingProgramResponse trainingProgram = trainingProgramService.getTrainingProgramById(id);
return ResponseEntity.ok(trainingProgram);
}

// Optional: Endpoint to delete a training program by ID
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteTrainingProgram(@PathVariable Long id) {
trainingProgramService.deleteTrainingProgram(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.group7.demo.dtos;

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

@Data
@Builder
@AllArgsConstructor
public class ExerciseDetailRequest {

private Integer sets;
private Integer repetitions;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.group7.demo.dtos;

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

@Data
@Builder
@AllArgsConstructor
public class ExerciseDetailResponse {
private Integer sets;
private Integer repetitions;

// Getters, setters, and builder...
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.group7.demo.dtos;

import com.group7.demo.models.enums.MuscleGroup;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
public class ExerciseRequest {

private String name; // Name of the exercise
private MuscleGroup muscleGroup; // Enum for muscle groups
private Integer sets; // Number of sets
private Integer repetitions; // Number of repetitions per set
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.group7.demo.dtos;

import com.group7.demo.models.enums.MuscleGroup;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@AllArgsConstructor
public class ExerciseResponse {
private String name;
private MuscleGroup muscleGroup;
private ExerciseDetailResponse exerciseDetail;

// Getters, setters, and builder...
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.group7.demo.dtos;

import com.group7.demo.models.enums.LocationType;
import com.group7.demo.models.enums.ProgramType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;


@Data
@Builder
@AllArgsConstructor
public class TrainingProgramRequest {

private String title;
private ProgramType programType;
private LocationType locationType;
private List<ExerciseRequest> exercises;
private String description;
private Long trainerId; // The ID of the trainer creating the program
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.group7.demo.dtos;

import com.group7.demo.models.enums.LocationType;
import com.group7.demo.models.enums.ProgramType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@AllArgsConstructor
public class TrainingProgramResponse {
private Long id;
private String title;
private ProgramType programType;
private LocationType locationType;
private List<ExerciseResponse> exercises;
private String description;
private String trainerUsername;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.group7.demo.models;
import com.group7.demo.models.enums.MuscleGroup;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Exercise {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(unique = true)
private String name; // Name of the exercise

@Enumerated(EnumType.STRING)
private MuscleGroup muscleGroup; // Targeted muscle group

@OneToOne(mappedBy = "exercise", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private ExerciseDetail exerciseDetail; // Link to the sets and repetitions

@ManyToMany(mappedBy = "exercises", fetch = FetchType.LAZY)
private List<TrainingProgram> trainingPrograms = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.group7.demo.models;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class ExerciseDetail {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Integer sets; // Number of sets

private Integer repetitions; // Number of repetitions per set

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "exercise_id", nullable = false)
private Exercise exercise; // Link to the exercise
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.group7.demo.models;

import com.group7.demo.models.enums.LocationType;
import com.group7.demo.models.enums.ProgramType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class TrainingProgram {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;

@Enumerated(EnumType.STRING)
private ProgramType programType; // GROUP or INDIVIDUAL

@Enumerated(EnumType.STRING)
private LocationType locationType; // HOME, GYM, OUTDOOR

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "training_program_exercise", // Name of the join table
joinColumns = @JoinColumn(name = "training_program_id"), // Foreign key to TrainingProgram
inverseJoinColumns = @JoinColumn(name = "exercise_id") // Foreign key to Exercise
) private List<Exercise> exercises; // Exercises linked to the training program

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User trainer;

private String description; // Optional description of the program
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.hibernate.annotations.UpdateTimestamp;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Getter
Expand Down Expand Up @@ -60,4 +61,8 @@ public class User {

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Post> posts = new HashSet<>();

@OneToMany(mappedBy = "trainer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<TrainingProgram> trainingPrograms;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.group7.demo.models.enums;

public enum LocationType {
HOME,
GYM,
OUTDOOR
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.group7.demo.models.enums;

public enum MuscleGroup {
CHEST,
BACK,
LEGS,
ARMS,
SHOULDERS,
CORE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.group7.demo.models.enums;

public enum ProgramType {
GROUP,
INDIVIDUAL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.group7.demo.repository;

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

import java.util.Optional;

public interface ExerciseRepository extends JpaRepository<Exercise, Long> {
Optional<Exercise> findByName(String name);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.group7.demo.repository;

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

public interface TrainingProgramRepository extends JpaRepository<TrainingProgram, Long> {
}
Loading

0 comments on commit 927c724

Please sign in to comment.