Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f90387ba45 | ||
|
|
d38b88e68c | ||
|
|
90b55bea38 | ||
|
|
0afb619d40 | ||
|
|
8b240b8b60 | ||
|
|
c9891ae7e8 | ||
|
|
5e5661635e | ||
|
|
b3399f377b | ||
|
|
5085320a1f | ||
|
|
741d01bcd2 | ||
|
|
0c82691a40 | ||
|
|
9494bb3458 | ||
|
|
4de8e2da22 | ||
|
|
5c191bcff0 | ||
|
|
fad05e8bb1 | ||
|
|
98bd9c636b | ||
|
|
d124f2bda6 | ||
|
|
f4b6698090 | ||
|
|
c6f8e552eb | ||
|
|
609cc1b66f | ||
|
|
59117be5bb | ||
|
|
d17fee3ee9 | ||
|
|
fddfe32984 | ||
|
|
94dbc95437 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||||
|
.vscode
|
||||||
|
.idea
|
||||||
# dependencies
|
# dependencies
|
||||||
/node_modules
|
/node_modules
|
||||||
/.pnp
|
/.pnp
|
||||||
|
|||||||
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"java.configuration.updateBuildConfiguration": "interactive"
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
# hackathon
|
# 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
|
## Getting started
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-webmvc</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.thymeleaf.extras</groupId>
|
<groupId>org.thymeleaf.extras</groupId>
|
||||||
@@ -97,6 +97,11 @@
|
|||||||
<artifactId>spring-boot-starter-thymeleaf-test</artifactId>
|
<artifactId>spring-boot-starter-thymeleaf-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -1,12 +1,18 @@
|
|||||||
package hackathon.FrisbYEE.jpa.dto;
|
package hackathon.FrisbYEE.jpa.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class AthleteDTO implements java.io.Serializable {
|
@Data
|
||||||
|
public class AthleteDTO {
|
||||||
private String nom;
|
private Integer id;
|
||||||
private String niveau;
|
private String name;
|
||||||
private String categorie;
|
private String categorie;
|
||||||
private List<String> groupes;
|
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;
|
||||||
|
}
|
||||||
@@ -3,9 +3,8 @@ package hackathon.FrisbYEE.jpa.metier;
|
|||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import jakarta.persistence.ManyToOne;
|
import jakarta.persistence.ManyToOne;
|
||||||
import lombok.Getter;
|
import lombok.*;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.Setter;
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -22,7 +21,7 @@ public class Activite implements Serializable {
|
|||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
private String theme;
|
private String theme;
|
||||||
private Long duree;
|
private Long duree;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package hackathon.FrisbYEE.jpa.metier;
|
|||||||
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.OneToOne;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@@ -17,9 +18,12 @@ public class Admin {
|
|||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@OneToOne(mappedBy = "admin")
|
||||||
|
private User user;
|
||||||
|
|
||||||
public Admin(String name){
|
public Admin(String name){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package hackathon.FrisbYEE.jpa.metier;
|
|||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import jakarta.persistence.ManyToMany;
|
import jakarta.persistence.ManyToMany;
|
||||||
import lombok.Getter;
|
import jakarta.persistence.OneToOne;
|
||||||
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import jakarta.persistence.Access;
|
import jakarta.persistence.Access;
|
||||||
@@ -14,14 +14,14 @@ import jakarta.persistence.ElementCollection;
|
|||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Getter @Setter @NoArgsConstructor
|
@Data @NoArgsConstructor
|
||||||
@Access(AccessType.FIELD)
|
@Access(AccessType.FIELD)
|
||||||
|
|
||||||
public class Athlete {
|
public class Athlete {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
private String categorie;
|
private String categorie;
|
||||||
private String niveau;
|
private String niveau;
|
||||||
@@ -32,6 +32,9 @@ public class Athlete {
|
|||||||
@ManyToMany(mappedBy = "athletes")
|
@ManyToMany(mappedBy = "athletes")
|
||||||
private List<Session> sessions = new ArrayList<>(); // plusieurs sessions sont possibles
|
private List<Session> sessions = new ArrayList<>(); // plusieurs sessions sont possibles
|
||||||
|
|
||||||
|
@OneToOne(mappedBy = "athlete")
|
||||||
|
private User user;
|
||||||
|
|
||||||
public Athlete(String name){
|
public Athlete(String name){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package hackathon.FrisbYEE.jpa.metier;
|
|||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import jakarta.persistence.OneToMany;
|
import jakarta.persistence.OneToMany;
|
||||||
|
import jakarta.persistence.OneToOne;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@@ -20,12 +21,15 @@ public class Coach {
|
|||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "coach")
|
@OneToMany(mappedBy = "coach")
|
||||||
private List<Session> sessions = new ArrayList<>(); // Un coach peut avoir plusieurs sessions
|
private List<Session> sessions = new ArrayList<>(); // Un coach peut avoir plusieurs sessions
|
||||||
|
|
||||||
|
@OneToOne(mappedBy = "coach")
|
||||||
|
private User user;
|
||||||
|
|
||||||
public Coach(String name){
|
public Coach(String name){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package hackathon.FrisbYEE.jpa.metier;
|
||||||
|
|
||||||
|
public enum Role {
|
||||||
|
ADMIN,
|
||||||
|
COACH,
|
||||||
|
ATHLETE
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ public class Session {
|
|||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
private Boolean isRecurrent;
|
private Boolean isRecurrent;
|
||||||
private LocalDateTime creneau;
|
private LocalDateTime creneau;
|
||||||
|
|||||||
@@ -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 + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,12 @@ package hackathon.FrisbYEE.jpa.service;
|
|||||||
|
|
||||||
import hackathon.FrisbYEE.jpa.metier.Activite;
|
import hackathon.FrisbYEE.jpa.metier.Activite;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
public interface ActiviteDAO extends JpaRepository<Activite, Integer> {
|
public interface ActiviteDAO extends JpaRepository<Activite, Integer> {
|
||||||
Activite findByKeycloakId(String keycloakId);
|
|
||||||
|
List<Activite> findByTheme(String theme);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
package hackathon.FrisbYEE.jpa.service;
|
package hackathon.FrisbYEE.jpa.service;
|
||||||
|
|
||||||
import hackathon.FrisbYEE.jpa.metier.Activite;
|
|
||||||
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
public interface AthleteDAO extends JpaRepository<Athlete, Integer> {
|
public interface AthleteDAO extends JpaRepository<Athlete, Integer> {
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,10 @@
|
|||||||
package hackathon.FrisbYEE.jpa.service;
|
package hackathon.FrisbYEE.jpa.service;
|
||||||
|
|
||||||
public class CoachDAO {
|
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> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package hackathon.FrisbYEE.jpa.web;
|
|
||||||
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
@RequestMapping("/activite")
|
|
||||||
|
|
||||||
public class ActiviteController {
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
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 = mapToEntity(dto);
|
||||||
|
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("/{id}")
|
||||||
|
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
|
||||||
|
@ResponseBody
|
||||||
|
public ResponseEntity<ActiviteDTO> getActivityById(@PathVariable("id") int id) {
|
||||||
|
try {
|
||||||
|
Activite activite = activiteDAO.findById(id).get();
|
||||||
|
ActiviteDTO dto = mapToDTO(activite);
|
||||||
|
return ResponseEntity.ok(dto);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/all")
|
||||||
|
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
|
||||||
|
@ResponseBody
|
||||||
|
public ResponseEntity<List<ActiviteDTO>> getAllActivity() {
|
||||||
|
try {
|
||||||
|
List<Activite> activites = activiteDAO.findAll();
|
||||||
|
List<ActiviteDTO> dtos = activites.stream().map(this::mapToDTO).collect(Collectors.toList());
|
||||||
|
return ResponseEntity.ok(dtos);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/theme/{theme}")
|
||||||
|
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
|
||||||
|
@ResponseBody
|
||||||
|
public ResponseEntity<List<ActiviteDTO>> getActivityByTheme(@PathVariable("theme") String theme) {
|
||||||
|
try {
|
||||||
|
List<Activite> activites = activiteDAO.findByTheme(theme);
|
||||||
|
List<ActiviteDTO> dtos = activites.stream().map(this::mapToDTO).collect(Collectors.toList());
|
||||||
|
return ResponseEntity.ok(dtos);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Activite mapToEntity(ActiviteDTO dto) {
|
||||||
|
Activite activite = new Activite();
|
||||||
|
//ID géré par Postgre ?
|
||||||
|
activite.setName(dto.getName());
|
||||||
|
activite.setTheme(dto.getTheme());
|
||||||
|
activite.setDuree(dto.getDuree());
|
||||||
|
activite.setDataActivite(dto.getDataActivite());
|
||||||
|
return activite;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ActiviteDTO mapToDTO(Activite activite) {
|
||||||
|
ActiviteDTO dto = new ActiviteDTO();
|
||||||
|
dto.setName(activite.getName());
|
||||||
|
dto.setTheme(activite.getTheme());
|
||||||
|
dto.setDuree(activite.getDuree());
|
||||||
|
dto.setDataActivite(activite.getDataActivite());
|
||||||
|
dto.setSessionId(activite.getSession() != null ? activite.getSession().getId() : null);
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package hackathon.FrisbYEE.rest;
|
|
||||||
|
|
||||||
public class ActiviteResources {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,33 +1,219 @@
|
|||||||
package hackathon.FrisbYEE.rest;
|
package hackathon.FrisbYEE.rest;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.chrono.ChronoLocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
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.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import hackathon.FrisbYEE.jpa.dto.ActiviteDTO;
|
||||||
|
import hackathon.FrisbYEE.jpa.dto.AthleteDTO;
|
||||||
|
import hackathon.FrisbYEE.jpa.dto.SessionDTO;
|
||||||
|
import hackathon.FrisbYEE.jpa.metier.Activite;
|
||||||
|
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
||||||
|
import hackathon.FrisbYEE.jpa.metier.Session;
|
||||||
import hackathon.FrisbYEE.jpa.service.AthleteDAO;
|
import hackathon.FrisbYEE.jpa.service.AthleteDAO;
|
||||||
|
import hackathon.FrisbYEE.jpa.service.SessionDAO;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/athletes")
|
||||||
public class AthleteResource {
|
public class AthleteResource {
|
||||||
|
@Autowired
|
||||||
private AthleteDAO athleteDAO;
|
private AthleteDAO athleteDAO;
|
||||||
|
private SessionDAO sessionDAO;
|
||||||
|
|
||||||
@Operation(summary = "Récupère tous les utilisateurs")
|
@PostMapping("/create")
|
||||||
@ApiResponses(value = {
|
@PreAuthorize("hasRole('Admin')") // Only admin can create??
|
||||||
@ApiResponse(responseCode = "200", description = "Récupère le Joueur ayant l'identifiant correspondant",
|
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
|
||||||
content = @Content(mediaType = "application/json",
|
Athlete athlete = mapToEntity(dto);
|
||||||
schema = @Schema(implementation = JoueurDTO.class)))
|
athleteDAO.save(athlete);
|
||||||
})
|
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
|
||||||
@GetMapping("/joueur/{id}")
|
}
|
||||||
public JoueurDTO getJoueurById(@PathVariable Integer joueurId) {
|
|
||||||
|
@PostMapping("/all")
|
||||||
|
@PreAuthorize("hasRole('Admin') or hasRole('Coach') or hasRole('Athlete')")
|
||||||
|
public ResponseEntity<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());
|
||||||
|
|
||||||
|
// Relationship: sessionId → session
|
||||||
|
if (dto.getSessionIds() != null) {
|
||||||
|
List<Session> sessions = new ArrayList<>();
|
||||||
|
for (Integer sessionId : dto.getSessionIds()) {
|
||||||
|
Session session = sessionDAO.findById(sessionId)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Session not found"));
|
||||||
|
sessions.add(session);
|
||||||
|
}
|
||||||
|
athlete.setSessions(sessions);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Athlete mapToEntity(AthleteDTO dto) {
|
||||||
|
Athlete athlete = new Athlete();
|
||||||
|
athlete.setId(dto.getId());
|
||||||
|
athlete.setName(dto.getName());
|
||||||
|
athlete.setCategorie(dto.getCategorie());
|
||||||
|
athlete.setNiveau(dto.getNiveau());
|
||||||
|
return athlete;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/athlete/{id}/session")
|
||||||
|
public List<SessionDTO> getSessionsAthlete(@PathVariable Integer athleteId) {
|
||||||
// return pet
|
// return pet
|
||||||
System.out.println("ID A CHERCHER" + joueurId);
|
System.out.println("ID A CHERCHER" + athleteId);
|
||||||
IJoueur j = dao.findOne(joueurId);
|
java.util.Optional<Athlete> j = athleteDAO.findById(athleteId);
|
||||||
JoueurDTO jDTO = new JoueurDTO(null, null);
|
List<Session> sessions = sessionDAO.findAll();
|
||||||
|
List<SessionDTO> athleteSessions = new ArrayList<>();
|
||||||
|
for(Session s : sessions){
|
||||||
|
if(s.getAthletes().contains(j.get())){
|
||||||
|
SessionDTO dto = new SessionDTO();
|
||||||
|
dto.setId(s.getId());
|
||||||
|
dto.setName(s.getName());
|
||||||
|
// Map other fields as necessary
|
||||||
|
athleteSessions.add(dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
System.out.println(j);
|
System.out.println(j);
|
||||||
return jDTO;
|
return athleteSessions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/athletes/session")
|
||||||
|
public List<SessionDTO> getAllSessions() {
|
||||||
|
List<Session> sessions = sessionDAO.findAll();
|
||||||
|
System.out.println(sessions);
|
||||||
|
List<SessionDTO> sessionDTOs = new ArrayList<>();
|
||||||
|
for (Session session : sessions) {
|
||||||
|
SessionDTO dto = new SessionDTO();
|
||||||
|
dto.setId(session.getId());
|
||||||
|
dto.setName(session.getName());
|
||||||
|
// Map other fields as necessary
|
||||||
|
sessionDTOs.add(dto);
|
||||||
|
}
|
||||||
|
return sessionDTOs;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/athletes/session/{id}/activities")
|
||||||
|
public List<ActiviteDTO> getActivitiesForSession(@PathVariable Integer id) {
|
||||||
|
// Récupérer la session par ID
|
||||||
|
java.util.Optional<Session> sessionOpt = sessionDAO.findById(id);
|
||||||
|
if (sessionOpt.isPresent()) {
|
||||||
|
Session session = sessionOpt.get();
|
||||||
|
// Retourner les activités de la session
|
||||||
|
List<ActiviteDTO> activiteDTOs = new ArrayList<>();
|
||||||
|
for (Activite activite : session.getActivites()) {
|
||||||
|
ActiviteDTO dto = new ActiviteDTO();
|
||||||
|
dto.setId(activite.getId());
|
||||||
|
dto.setName(activite.getName());
|
||||||
|
// Map other fields as necessary
|
||||||
|
activiteDTOs.add(dto);
|
||||||
|
}
|
||||||
|
return activiteDTOs;
|
||||||
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/athletes/{id}/session/after/{date}")
|
||||||
|
public List<SessionDTO> getSessionsAfterDate(@PathVariable Integer id, @PathVariable String date) {
|
||||||
|
// Récupérer l'athlète par ID
|
||||||
|
java.util.Optional<Athlete> athleteOpt = athleteDAO.findById(id);
|
||||||
|
if (athleteOpt.isPresent()) {
|
||||||
|
Athlete athlete = athleteOpt.get();
|
||||||
|
// Récupérer les sessions de l'athlète après la date donnée
|
||||||
|
List<Session> sessions = sessionDAO.findAll();
|
||||||
|
List<SessionDTO> filteredSessions = new ArrayList<>();
|
||||||
|
for (Session session : sessions) {
|
||||||
|
if (session.getAthletes().contains(athlete) && session.getCreneau().isAfter(ChronoLocalDateTime.from(LocalDate.parse(date)))) { //WTF toujours sympa les dates
|
||||||
|
SessionDTO dto = new SessionDTO();
|
||||||
|
dto.setId(session.getId());
|
||||||
|
dto.setName(session.getName());
|
||||||
|
// Map other fields as necessary
|
||||||
|
filteredSessions.add(dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredSessions;
|
||||||
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/athletes/{id}/session/between/{startDate}/{endDate}")
|
||||||
|
public List<SessionDTO> getSessionsBetweenDates(@PathVariable Integer id, @PathVariable String startDate, @PathVariable String endDate) {
|
||||||
|
// Récupérer l'athlète par ID
|
||||||
|
java.util.Optional<Athlete> athleteOpt = athleteDAO.findById(id);
|
||||||
|
if (athleteOpt.isPresent()) {
|
||||||
|
Athlete athlete = athleteOpt.get();
|
||||||
|
// Récupérer les sessions de l'athlète entre les deux dates données
|
||||||
|
List<Session> sessions = sessionDAO.findAll();
|
||||||
|
List<SessionDTO> filteredSessions = new ArrayList<>();
|
||||||
|
for (Session session : sessions) {
|
||||||
|
if (session.getAthletes().contains(athlete) && session.getCreneau().isAfter(ChronoLocalDateTime.from(LocalDate.parse(startDate))) && session.getCreneau().isBefore(ChronoLocalDateTime.from(LocalDate.parse(endDate)))) {
|
||||||
|
SessionDTO dto = new SessionDTO();
|
||||||
|
dto.setId(session.getId());
|
||||||
|
dto.setName(session.getName());
|
||||||
|
// Map other fields as necessary
|
||||||
|
filteredSessions.add(dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredSessions;
|
||||||
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
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 = mapToEntity(dto);
|
||||||
|
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);
|
||||||
|
CoachDTO updatedDto = mapToDTO(coach);
|
||||||
|
return ResponseEntity.ok(updatedDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Coach mapToEntity(CoachDTO dto) {
|
||||||
|
Coach coach = new Coach();
|
||||||
|
coach.setId(dto.getId());
|
||||||
|
coach.setName(dto.getName());
|
||||||
|
return coach;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
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 {
|
||||||
|
Session session = maptoEntity(dto);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Session maptoEntity(SessionDTO dto) {
|
||||||
|
Session session = new Session();
|
||||||
|
session.setId(dto.getId());
|
||||||
|
session.setName(dto.getName());
|
||||||
|
session.setIsRecurrent(dto.getIsRecurrent());
|
||||||
|
session.setCreneau(dto.getCreneau());
|
||||||
|
session.setDuree(dto.getDuree());
|
||||||
|
session.setGroupe(dto.getGroupe());
|
||||||
|
// Coach
|
||||||
|
if (dto.getCoachId() != null) {
|
||||||
|
Coach coach = new Coach();
|
||||||
|
coach.setId(dto.getCoachId());
|
||||||
|
session.setCoach(coach);
|
||||||
|
}
|
||||||
|
// Athletes
|
||||||
|
if (dto.getAthleteIds() != null) {
|
||||||
|
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
|
||||||
|
session.setAthletes(athletes);
|
||||||
|
}
|
||||||
|
// Activites
|
||||||
|
if (dto.getActiviteIds() != null) {
|
||||||
|
List<Activite> activites = activiteDAO.findAllById(dto.getActiviteIds());
|
||||||
|
session.setActivites(activites);
|
||||||
|
}
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,27 @@
|
|||||||
package hackathon.FrisbYEE;
|
package hackathon.FrisbYEE;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.test.web.servlet.client.RestTestClient;
|
||||||
|
|
||||||
@SpringBootTest
|
import hackathon.FrisbYEE.rest.AthleteResource;
|
||||||
class FrisbYeeApplicationTests {
|
|
||||||
|
|
||||||
|
class FrisbYEEApplicationTests {
|
||||||
|
|
||||||
|
//Controller
|
||||||
|
private AthleteResource athleteResource;
|
||||||
|
private RestTestClient mockMvc;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
athleteResource = new AthleteResource();
|
||||||
|
mockMvc = RestTestClient.bindToController(athleteResource).build();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void shouldReturnUsers() throws Exception {
|
||||||
|
//mockMvc.perform(get("/api/users"))
|
||||||
|
// .andExpect(status().isOk());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user