Merge branch 'jpa'
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,5 +1,6 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
.vscode
|
||||
.idea
|
||||
# dependencies
|
||||
front_end/node_modules
|
||||
/.pnp
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# hackathon
|
||||
|
||||
In order to launch any task (Maven compile, clean install ...) be sure to launch the Docker compose in the root directory with :
|
||||
|
||||
docker compose up
|
||||
|
||||
## Getting started
|
||||
|
||||
|
||||
@@ -97,6 +97,18 @@
|
||||
<artifactId>spring-boot-starter-thymeleaf-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.32</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.5.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package hackathon.FrisbYEE.jpa.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ActiviteDTO {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String theme;
|
||||
private Long duree; // optional, can be null
|
||||
private List<String> dataActivite;
|
||||
private Integer sessionId;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package hackathon.FrisbYEE.jpa.dto;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AdminDTO {
|
||||
private Integer id;
|
||||
private String name;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package hackathon.FrisbYEE.jpa.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class AthleteDTO {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String categorie;
|
||||
private String niveau;
|
||||
private List<String> groupes = new ArrayList<>();
|
||||
private List<Integer> sessionIds = new ArrayList<>();
|
||||
private Integer userId;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package hackathon.FrisbYEE.jpa.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CoachDTO {
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package hackathon.FrisbYEE.jpa.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class SessionDTO {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private Boolean isRecurrent;
|
||||
private LocalDateTime creneau;
|
||||
private Long duree;
|
||||
private String groupe;
|
||||
|
||||
private Integer coachId;
|
||||
private List<Integer> athleteIds;
|
||||
private List<Integer> activiteIds;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package hackathon.FrisbYEE.jpa.dto;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.metier.Role;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserDTO {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String email;
|
||||
private Role role;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package hackathon.FrisbYEE.jpa.interfaces;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public interface IAthlete{
|
||||
public void run();
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package hackathon.FrisbYEE.jpa.metier;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import jakarta.persistence.Access;
|
||||
import jakarta.persistence.AccessType;
|
||||
import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Getter @Setter @NoArgsConstructor
|
||||
@Access(AccessType.FIELD)
|
||||
|
||||
public class Activite implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String theme;
|
||||
private Long duree;
|
||||
@ElementCollection
|
||||
private List<String> dataActivite = new ArrayList<>();
|
||||
|
||||
@ManyToOne
|
||||
private Session session;
|
||||
|
||||
public Activite(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Activite(String name, String theme, List<String> dataActivite, Long duree) {
|
||||
this.name = name;
|
||||
this.theme = theme;
|
||||
this.duree = duree;
|
||||
this.dataActivite = dataActivite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Activite [id=" + id + " , name=" + name + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package hackathon.FrisbYEE.jpa.metier;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToOne;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import jakarta.persistence.Access;
|
||||
import jakarta.persistence.AccessType;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Getter @Setter @NoArgsConstructor
|
||||
@Access(AccessType.FIELD)
|
||||
|
||||
public class Admin {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
@OneToOne(mappedBy = "admin")
|
||||
private User user;
|
||||
|
||||
public Admin(String name){
|
||||
this.name = name;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Admin [id=" + id + " , name=" + name + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package hackathon.FrisbYEE.jpa.metier;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import jakarta.persistence.OneToOne;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import jakarta.persistence.Access;
|
||||
import jakarta.persistence.AccessType;
|
||||
import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Data @NoArgsConstructor
|
||||
@Access(AccessType.FIELD)
|
||||
|
||||
public class Athlete {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String categorie;
|
||||
private String niveau;
|
||||
@ElementCollection
|
||||
private List<String> groupe = new ArrayList<>();
|
||||
|
||||
|
||||
@ManyToMany(mappedBy = "athletes")
|
||||
private List<Session> sessions = new ArrayList<>(); // plusieurs sessions sont possibles
|
||||
|
||||
@OneToOne(mappedBy = "athlete")
|
||||
private User user;
|
||||
|
||||
public Athlete(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Athlete(String name, String categorie, String niveau, List<String> groupe){
|
||||
this.name = name;
|
||||
this.categorie = categorie;
|
||||
this.niveau = niveau;
|
||||
this.groupe = groupe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Athlete [id=" + id + " , name=" + name + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package hackathon.FrisbYEE.jpa.metier;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OneToOne;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import jakarta.persistence.Access;
|
||||
import jakarta.persistence.AccessType;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Getter @Setter @NoArgsConstructor
|
||||
@Access(AccessType.FIELD)
|
||||
|
||||
public class Coach {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "coach")
|
||||
private List<Session> sessions = new ArrayList<>(); // Un coach peut avoir plusieurs sessions
|
||||
|
||||
@OneToOne(mappedBy = "coach")
|
||||
private User user;
|
||||
|
||||
public Coach(String name){
|
||||
this.name = name;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Coach [id=" + id + " , name=" + name + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package hackathon.FrisbYEE.jpa.metier;
|
||||
|
||||
public enum Role {
|
||||
ADMIN,
|
||||
COACH,
|
||||
ATHLETE
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package hackathon.FrisbYEE.jpa.metier;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import jakarta.persistence.Access;
|
||||
import jakarta.persistence.AccessType;
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Getter @Setter @NoArgsConstructor
|
||||
@Access(AccessType.FIELD)
|
||||
|
||||
public class Session {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String name;
|
||||
private Boolean isRecurrent;
|
||||
private LocalDateTime creneau;
|
||||
private Long duree;
|
||||
private String groupe;
|
||||
|
||||
@ManyToOne
|
||||
private Coach coach; // un coach par session
|
||||
|
||||
@ManyToMany
|
||||
private List<Athlete> athletes = new ArrayList<>(); // plusieurs athlètes par session
|
||||
|
||||
@OneToMany(mappedBy = "session", cascade = CascadeType.PERSIST)
|
||||
private List<Activite> activites = new ArrayList<>(); // plusieurs activités par session
|
||||
|
||||
public Session(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Session(String name, Boolean isRecurrent, LocalDateTime creneau, Long duree, String groupe) {
|
||||
this.name = name;
|
||||
this.isRecurrent = isRecurrent;
|
||||
this.creneau = creneau;
|
||||
this.duree = duree;
|
||||
this.groupe = groupe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Session [id=" + id + " , name=" + name + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package hackathon.FrisbYEE.jpa.metier;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToOne;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import java.io.Serializable;
|
||||
import jakarta.persistence.Access;
|
||||
import jakarta.persistence.AccessType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.CascadeType;
|
||||
|
||||
@Entity
|
||||
@Getter @Setter @NoArgsConstructor
|
||||
@Access(AccessType.FIELD)
|
||||
|
||||
public class User implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
@Column(unique = true, nullable = false) //pas possible d'avoir le même nom
|
||||
private String name;
|
||||
@Column (nullable = false)
|
||||
private String motDePasse;
|
||||
private String email;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private Role role;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Coach coach;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Athlete athlete;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Admin admin;
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public User(String name, String motDePasse, String email, Role role) {
|
||||
this.name = name;
|
||||
this.motDePasse = motDePasse;
|
||||
this.email = email;
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [id=" + id + " , name=" + name + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package hackathon.FrisbYEE.jpa.service;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.metier.Activite;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ActiviteDAO extends JpaRepository<Activite, Integer> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package hackathon.FrisbYEE.jpa.service;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.metier.Admin;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface AdminDAO extends JpaRepository<Admin, Integer> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package hackathon.FrisbYEE.jpa.service;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface AthleteDAO extends JpaRepository<Athlete, Integer> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package hackathon.FrisbYEE.jpa.service;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.metier.Coach;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CoachDAO extends JpaRepository<Coach, Integer> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package hackathon.FrisbYEE.jpa.service;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.metier.Session;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface SessionDAO extends JpaRepository<Session, Integer> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package hackathon.FrisbYEE.rest;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.dto.ActiviteDTO;
|
||||
import hackathon.FrisbYEE.jpa.metier.Activite;
|
||||
import hackathon.FrisbYEE.jpa.metier.Session;
|
||||
import hackathon.FrisbYEE.jpa.service.ActiviteDAO;
|
||||
import hackathon.FrisbYEE.jpa.service.SessionDAO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/activite")
|
||||
|
||||
public class ActiviteResource {
|
||||
@Autowired
|
||||
private ActiviteDAO activiteDAO;
|
||||
|
||||
@Autowired
|
||||
private SessionDAO sessionDAO;
|
||||
|
||||
/*
|
||||
* POST /activite/create
|
||||
* DELETE /activite/delete/{id}
|
||||
*
|
||||
*/
|
||||
|
||||
@PostMapping("/create")
|
||||
@ResponseBody
|
||||
@PreAuthorize("hasRole('Coach')")
|
||||
public ResponseEntity<String> create(@RequestBody ActiviteDTO dto) {
|
||||
|
||||
try {
|
||||
Session session = sessionDAO.findById(dto.getSessionId()).get();
|
||||
Activite activite = new Activite();
|
||||
activite.setName(dto.getName());
|
||||
activite.setTheme(dto.getTheme());
|
||||
activite.setDuree(dto.getDuree() != null ? dto.getDuree() : 0L);
|
||||
activite.setDataActivite(dto.getDataActivite());
|
||||
activite.setSession(session);
|
||||
activiteDAO.save(activite);
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + ex.getMessage());
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("Activity created");
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@ResponseBody
|
||||
@PreAuthorize("hasRole('Coach')")
|
||||
public ResponseEntity<String> delete(@PathVariable("id") int id) {
|
||||
try {
|
||||
Activite activite = activiteDAO.findById(id).get();
|
||||
activiteDAO.delete(activite);
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + ex.getMessage());
|
||||
|
||||
}
|
||||
return ResponseEntity.ok("Activity deleted");
|
||||
}
|
||||
|
||||
@PostMapping("/update/{id}")
|
||||
@ResponseBody
|
||||
@PreAuthorize("hasRole('Coach')")
|
||||
public ResponseEntity<String> modifyById(@PathVariable("id") int id, @RequestBody ActiviteDTO dto) {
|
||||
try {
|
||||
Session session = sessionDAO.findById(dto.getSessionId()).get();
|
||||
Activite activite = activiteDAO.findById(id).get();
|
||||
activite.setName(dto.getName());
|
||||
activite.setTheme(dto.getTheme());
|
||||
activite.setDuree(dto.getDuree() != null ? dto.getDuree() : 0L);
|
||||
activite.setDataActivite(dto.getDataActivite());
|
||||
activite.setSession(session);
|
||||
activiteDAO.save(activite);
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + ex.getMessage());
|
||||
}
|
||||
return ResponseEntity.ok("Activity modified");
|
||||
|
||||
}
|
||||
|
||||
@GetMapping("/read/{id}")
|
||||
@ResponseBody
|
||||
public ResponseEntity<ActiviteDTO> getActivityById(@PathVariable("id") int id) {
|
||||
try {
|
||||
Activite activite = activiteDAO.findById(id).get();
|
||||
ActiviteDTO dto = new ActiviteDTO();
|
||||
dto.setName(activite.getName());
|
||||
dto.setId(activite.getId());
|
||||
dto.setTheme(activite.getTheme());
|
||||
dto.setDuree(activite.getDuree());
|
||||
dto.setDataActivite(activite.getDataActivite());
|
||||
dto.setSessionId(activite.getSession() != null ? activite.getSession().getId() : null);
|
||||
|
||||
return ResponseEntity.ok(dto);
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@ResponseBody
|
||||
public ResponseEntity<List<ActiviteDTO>> getAllActivity() {
|
||||
try {
|
||||
List<Activite> activites = activiteDAO.findAll();
|
||||
List<ActiviteDTO> dtos = activites.stream().map(activite -> {
|
||||
ActiviteDTO dto = new ActiviteDTO();
|
||||
dto.setName(activite.getName());
|
||||
dto.setId(activite.getId());
|
||||
dto.setTheme(activite.getTheme());
|
||||
dto.setDuree(activite.getDuree());
|
||||
dto.setDataActivite(activite.getDataActivite());
|
||||
dto.setSessionId(activite.getSession() != null ? activite.getSession().getId() : null);
|
||||
return dto;
|
||||
}).collect(Collectors.toList());
|
||||
return ResponseEntity.ok(dtos);
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package hackathon.FrisbYEE.rest;
|
||||
|
||||
public class ActiviteResources {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package hackathon.FrisbYEE.rest;
|
||||
|
||||
import org.apache.el.stream.Optional;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import hackathon.FrisbYEE.jpa.dto.AthleteDTO;
|
||||
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
||||
import hackathon.FrisbYEE.jpa.service.AthleteDAO;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/athletes")
|
||||
public class AthleteResource {
|
||||
@Autowired
|
||||
private AthleteDAO athleteDAO;
|
||||
|
||||
@PostMapping("/create")
|
||||
@PreAuthorize("hasRole('Admin')") // Only admin can create??
|
||||
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
|
||||
Athlete ahtlete = new Athlete();
|
||||
athlete.setName(dto.getName())
|
||||
athlete.setCategorie(dto.getCategorie())
|
||||
athlete.setNiveau(dto.getNiveau())
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
|
||||
}
|
||||
|
||||
@PostMapping("/all")
|
||||
@PreAuthorize("hasRole('Admin') or hasRole('Coach') or hasRole('Athlete')")
|
||||
public ReponseEntity<List<AthleteDTO>> all() {
|
||||
List<Athlete> athletes = athleteDAO.findAll();
|
||||
List<AthleteDTO> dtos = new ArrayList<>();
|
||||
for (Athlete athlete : athletes) {
|
||||
dtos.add(maptoDTO(athlete));
|
||||
}
|
||||
return ResponseEntity.ok(dtos);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@PreAuthorize("hasRole('Admin') or hasRole('Coach') or hasRole('Athlete')")
|
||||
public ResponseEntity<AthleteDTO> getById(@PathVariable Integer id) {
|
||||
return athleteDAO.findById(id)
|
||||
.map(athlete -> ResponseEntity.ok(mapToDTO(athlete)))
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@PreAuthorize("hasRole('ADMIN') or #id == principal.id")
|
||||
public ResponseEntity<AthleteDTO> update(@PathVariable Integer id,@RequestBody AthleteDTO dto) {
|
||||
try {
|
||||
Athlete athlete = athleteDAO.findById(id).get();
|
||||
athlete.setName(dto.getName());
|
||||
athlete.setCategorie(dto.getCategorie());
|
||||
athlete.setNiveau(dto.getNiveau());
|
||||
|
||||
// Optional
|
||||
athlete.setDuree(dto.getDuree());
|
||||
athlete.setTheme(dto.getTheme());
|
||||
|
||||
// List
|
||||
if (dto.getDataActivite() != null) {
|
||||
athlete.setDataActivite(dto.getDataActivite());
|
||||
}
|
||||
|
||||
// Relationship: sessionId → session
|
||||
if (dto.getSessionId() != null) {
|
||||
Session session = sessionDAO.findById(dto.getSessionId())
|
||||
.orElseThrow(() -> new RuntimeException("Session not found"));
|
||||
athlete.setSession(session);
|
||||
}
|
||||
|
||||
athleteDAO.save(athlete);
|
||||
return ResponseEntity.ok(mapToDTO(athlete));
|
||||
}catch (Exception ex){
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@PreAuthorize("hasRole('Admin')")
|
||||
public ResponseEntity<Void> delete(@PathVariable Integer id) {
|
||||
if (!athleteDAO.existsById(id)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
athleteDAO.deleteById(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
private AthleteDTO mapToDTO(Athlete athlete) {
|
||||
AthleteDTO dto = new AthleteDTO();
|
||||
dto.setId(athlete.getId());
|
||||
dto.setName(athlete.getName());
|
||||
dto.setCategorie(athlete.getCategorie());
|
||||
dto.setNiveau(athlete.getNiveau());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package hackathon.FrisbYEE.rest;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.dto.CoachDTO;
|
||||
import hackathon.FrisbYEE.jpa.metier.Coach;
|
||||
import hackathon.FrisbYEE.jpa.service.CoachDAO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CoachResource {
|
||||
@Autowired
|
||||
private CoachDAO coachDAO;
|
||||
|
||||
@PostMapping("/create")
|
||||
@PreAuthorize("hasRole('Admin')") // Only admin can create
|
||||
public ResponseEntity<CoachDTO> create(@RequestBody CoachDTO dto) {
|
||||
Coach coach = new Coach();
|
||||
coach.setName(dto.getName());
|
||||
coachDAO.save(coach);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(coach));
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@PreAuthorize("hasRole('Admin') or hasRole('Coach')")
|
||||
public List<CoachDTO> getAll() {
|
||||
List<Coach> coaches = coachDAO.findAll();
|
||||
List<CoachDTO> dtos = new ArrayList<>();
|
||||
for (Coach coach : coaches) {
|
||||
dtos.add(mapToDTO(coach));
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@PreAuthorize("hasRole('Admin') or hasRole('Coach')")
|
||||
public CoachDTO getById(@PathVariable Integer id) {
|
||||
Coach coach = coachDAO.findById(id)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
|
||||
return mapToDTO(coach);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
@PreAuthorize("hasRole('Admin')")
|
||||
public ResponseEntity<CoachDTO> update(@PathVariable Integer id, @RequestBody CoachDTO dto) {
|
||||
Coach coach = coachDAO.findById(id)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
|
||||
if (dto.getName() != null) coach.setName(dto.getName());
|
||||
coachDAO.save(coach);
|
||||
return ResponseEntity.ok(mapToDTO(coach));
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@PreAuthorize("hasRole('Admin')")
|
||||
public ResponseEntity<Void> delete(@PathVariable Integer id) {
|
||||
Coach coach = coachDAO.findById(id)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
|
||||
coachDAO.delete(coach);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
private CoachDTO mapToDTO(Coach coach) {
|
||||
CoachDTO dto = new CoachDTO();
|
||||
dto.setId(coach.getId());
|
||||
dto.setName(coach.getName());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package hackathon.FrisbYEE.rest;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.dto.SessionDTO;
|
||||
import hackathon.FrisbYEE.jpa.metier.Activite;
|
||||
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
||||
import hackathon.FrisbYEE.jpa.metier.Coach;
|
||||
import hackathon.FrisbYEE.jpa.metier.Session;
|
||||
import hackathon.FrisbYEE.jpa.service.ActiviteDAO;
|
||||
import hackathon.FrisbYEE.jpa.service.AthleteDAO;
|
||||
import hackathon.FrisbYEE.jpa.service.CoachDAO;
|
||||
import hackathon.FrisbYEE.jpa.service.SessionDAO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/session")
|
||||
|
||||
public class SessionResource {
|
||||
@Autowired
|
||||
private SessionDAO sessionDAO;
|
||||
|
||||
@Autowired
|
||||
private CoachDAO coachDAO;
|
||||
|
||||
@Autowired
|
||||
private AthleteDAO athleteDAO;
|
||||
|
||||
@Autowired
|
||||
private ActiviteDAO activiteDAO;
|
||||
|
||||
@PostMapping("/create")
|
||||
@ResponseBody
|
||||
@PreAuthorize("hasRole('Coach')")
|
||||
public ResponseEntity<?> create(@RequestBody SessionDTO dto) {
|
||||
try {
|
||||
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
|
||||
Coach coach = coachDAO.findById(dto.getCoachId()).get();
|
||||
Session session = new Session();
|
||||
session.setName(dto.getName());
|
||||
session.setIsRecurrent(dto.getIsRecurrent());
|
||||
session.setCreneau(dto.getCreneau());
|
||||
session.setDuree(dto.getDuree());
|
||||
session.setGroupe(dto.getGroupe());
|
||||
session.setCoach(coach);
|
||||
session.setAthletes(athletes);
|
||||
sessionDAO.save(session);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session));
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
|
||||
public ResponseEntity<List<SessionDTO>> getAll() {
|
||||
List<Session> sessions = sessionDAO.findAll();
|
||||
List<SessionDTO> dtos = new ArrayList<>();
|
||||
for (Session session : sessions) {
|
||||
dtos.add(maptoDTO(session));
|
||||
}
|
||||
return ResponseEntity.ok(dtos);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
|
||||
public ResponseEntity<?> getById(@PathVariable Integer id) {
|
||||
try {
|
||||
Session session = sessionDAO.findById(id).orElseThrow();
|
||||
return ResponseEntity.ok(maptoDTO(session));
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@ResponseBody
|
||||
@PreAuthorize("hasRole('Coach')")
|
||||
public ResponseEntity<String> delete(@PathVariable("id") int id) {
|
||||
try {
|
||||
Session session = sessionDAO.findById(id).get();
|
||||
sessionDAO.delete(session);
|
||||
return ResponseEntity.ok("Session deleted successfully");
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
@PreAuthorize("hasRole('Coach')")
|
||||
public ResponseEntity<Void> updateSession(@PathVariable Integer id, @RequestBody SessionDTO dto) {
|
||||
Session session = sessionDAO.findById(id).orElseThrow(() -> new ResponseStatusException(
|
||||
HttpStatus.NOT_FOUND, "Session not found with id " + id));
|
||||
|
||||
if (dto.getDuree() != null) {
|
||||
session.setDuree(dto.getDuree());
|
||||
}
|
||||
if (dto.getAthleteIds() != null) {
|
||||
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
|
||||
session.setAthletes(athletes);
|
||||
}
|
||||
if (dto.getActiviteIds() != null) {
|
||||
List<Activite> activites = activiteDAO.findAllById(dto.getActiviteIds());
|
||||
session.setActivites(activites);
|
||||
}
|
||||
sessionDAO.save(session);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
private SessionDTO maptoDTO(Session s) {
|
||||
SessionDTO dto = new SessionDTO();
|
||||
dto.setId(s.getId());
|
||||
dto.setName(s.getName());
|
||||
dto.setIsRecurrent(s.getIsRecurrent());
|
||||
dto.setCreneau(s.getCreneau());
|
||||
dto.setDuree(s.getDuree());
|
||||
dto.setGroupe(s.getGroupe());
|
||||
// Coach
|
||||
if (s.getCoach() != null) {
|
||||
dto.setCoachId(s.getCoach().getId());
|
||||
}
|
||||
// Athletes
|
||||
if (s.getAthletes() != null) {
|
||||
List<Integer> athleteIds = new ArrayList<>();
|
||||
for (Athlete athlete : s.getAthletes()) {
|
||||
athleteIds.add(athlete.getId());
|
||||
}
|
||||
dto.setAthleteIds(athleteIds);
|
||||
}
|
||||
// Activites
|
||||
if (s.getActivites() != null) {
|
||||
List<Integer> activiteIds = new ArrayList<>();
|
||||
for (Activite activite : s.getActivites()) {
|
||||
activiteIds.add(activite.getId());
|
||||
}
|
||||
dto.setActiviteIds(activiteIds);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user