Max/Backend/Curriculum #106

Merged
tonitch merged 6 commits from Max/Backend/Curriculum into master 2024-03-15 22:33:25 +01:00
15 changed files with 295 additions and 35 deletions
Showing only changes of commit 12846ed83d - Show all commits

View File

@ -0,0 +1,71 @@
package ovh.herisson.Clyde.EndPoints;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.CursusCourseService;
import ovh.herisson.Clyde.Services.CursusService;
import ovh.herisson.Clyde.Tables.Cursus;
import ovh.herisson.Clyde.Tables.CursusCourse;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;
import java.util.Map;
@RestController
public class CursusController {
private final CursusService cursusServ;
private final AuthenticatorService authServ;
private final CursusCourseService cursusCourseServ;
public CursusController(CursusService cursusServ, AuthenticatorService authServ, CursusCourseService cursusCourseServ){
this.cursusServ = cursusServ;
this.authServ = authServ;
this.cursusCourseServ = cursusCourseServ;
}
@GetMapping("/cursus/{id}")
public ResponseEntity<Cursus> findById(@PathVariable long id){
return new ResponseEntity<>(cursusServ.findById(id), HttpStatus.OK);
}
@GetMapping("/curriculums")
public ResponseEntity<Iterable<Map<String, Object>>> findAllindDepth(){
return new ResponseEntity<>(cursusCourseServ.getAllDepthCursus(),HttpStatus.OK);
}
@GetMapping("/curriculum")
public ResponseEntity<Iterable<CursusCourse>> findAll(){
return new ResponseEntity<>(cursusCourseServ.findAll(),HttpStatus.OK);
}
Maxime marked this conversation as resolved
Review

Si je comprends bien, la différence entre GET /curriculums et GET /curriculum c'est que l'un retourne la liste des cours avec et l'autre non. Je trouve ça un peu abstrait, peut être est-il possible de faire un nom plus descriptif ?

Si je comprends bien, la différence entre `GET /curriculums` et `GET /curriculum` c'est que l'un retourne la liste des cours avec et l'autre non. Je trouve ça un peu abstrait, peut être est-il possible de faire un nom plus descriptif ?
/**@PostMapping("/cursus")
public ResponseEntity<String> postCursus(@RequestHeader("Authorization") String token,@RequestBody Cursus cursus){
if (!isSecretaryOrAdmin(token)){
return new UnauthorizedResponse<>("you're not allowed to post a cursus");
}
cursusServ.save(cursus);
return new ResponseEntity<>("created !",HttpStatus.CREATED);
}**/
private boolean isSecretaryOrAdmin(String authorization){
if (authorization ==null)
return false;
User poster = authServ.getUserFromToken(authorization);
if (poster == null) return false;
return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin;
}
}

View File

@ -1,19 +1,14 @@
package ovh.herisson.Clyde.EndPoints;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.Column;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Tables.Cursus;
import ovh.herisson.Clyde.Tables.CursusType;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.User;
import java.util.Date;
import java.util.HashMap;
@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
@ -35,6 +30,7 @@ public class LoginController {
public LoginController(AuthenticatorService authServ){
this.authServ = authServ;
}
@PostMapping(value = "/login")
public ResponseEntity<String> login(@RequestBody RequestLogin requestLogin){
@ -50,7 +46,6 @@ public class LoginController {
@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody InscriptionRequest inscriptionRequest){
authServ.register(inscriptionRequest);
return new ResponseEntity<>("Is OK", HttpStatus.OK);
}

View File

@ -7,10 +7,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import ovh.herisson.Clyde.Repositories.TokenRepository;
import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Services.TokenService;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User;
import ovh.herisson.Clyde.Services.*;
import ovh.herisson.Clyde.Tables.*;
import java.util.ArrayList;
import java.util.Arrays;
@ -26,13 +24,22 @@ public class MockController {
public final UserRepository userRepo;
public final TokenRepository tokenRepo;
public final TokenService tokenService;
public final CursusCourseService cursusCourseService;
public final CursusService cursusService;
public final CourseService courseService;
ArrayList<User> mockUsers;
public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService){
public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CursusCourseService cursusCourseService, CursusService cursusService, CourseService courseService){
this.tokenRepo = tokenRepo;
this.userRepo = userRepo;
this.tokenService = tokenService;
this.cursusCourseService = cursusCourseService;
this.cursusService = cursusService;
this.courseService = courseService;
}
/** Saves an example of each user type by :
@ -44,6 +51,9 @@ public class MockController {
@PostMapping("/mock")
public void postMock(){
// user part
User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), null,Role.Admin,passwordEncoder.encode("admin"));
User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), null,Role.Student,passwordEncoder.encode("student"));
User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), null,Role.Teacher,passwordEncoder.encode("secretary"));
@ -52,6 +62,42 @@ public class MockController {
mockUsers = new ArrayList<User>(Arrays.asList(herobrine,joe,meh,joke));
userRepo.saveAll(mockUsers);
// Course / Curriculum part
Cursus infoBab1 = new Cursus(1,"info");
Cursus chemistryBab1 = new Cursus(1,"chemistry");
Cursus psychologyBab1 = new Cursus(1,"psychology");
cursusService.save(infoBab1);
cursusService.save(chemistryBab1);
cursusService.save(psychologyBab1);
Course progra1 = new Course(5,"Programmation et algorithimque 1","TODO DELETE");
Course chemistry1 = new Course(12, "Thermochimie","TODO DELETE");
Course psycho1 = new Course(21, "rien faire t'as cru c'est psycho", "TODO DELETE");
Course commun = new Course(2, "cours commun","TODO DELETE");
courseService.save(progra1);
courseService.save(chemistry1);
courseService.save(psycho1);
courseService.save(commun);
cursusCourseService.save(new CursusCourse(infoBab1,progra1));
cursusCourseService.save(new CursusCourse(infoBab1,commun));
cursusCourseService.save(new CursusCourse(psychologyBab1,psycho1));
cursusCourseService.save(new CursusCourse(psychologyBab1,commun));
cursusCourseService.save(new CursusCourse(chemistryBab1,commun));
cursusCourseService.save(new CursusCourse(chemistryBab1,chemistry1));
}
@DeleteMapping("/mock")

View File

@ -41,7 +41,5 @@ public class UserController {
public Iterable<User> getAllUsers(){
return userService.getAll();
}
}

View File

@ -0,0 +1,8 @@
package ovh.herisson.Clyde.Repositories;
import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.Course;
public interface CourseRepository extends CrudRepository<Course,Long> {
Course findById(long id);
}

View File

@ -0,0 +1,20 @@
package ovh.herisson.Clyde.Repositories;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Cursus;
import ovh.herisson.Clyde.Tables.CursusCourse;
public interface CursusCourseRepository extends CrudRepository<CursusCourse,Long> {
//todo faire custom query pour trouver tous les cours d'un cursus par un cursusId
@Query("select distinct cc.course from CursusCourse cc where cc.cursus = ?1")
Iterable<Course> findCoursesByCursus(Cursus cursus);
@Query("select distinct cc.cursus from CursusCourse cc")
Iterable<Cursus> findDistinctCursuses();
}

View File

@ -0,0 +1,8 @@
package ovh.herisson.Clyde.Repositories;
import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.Cursus;
public interface CursusRepository extends CrudRepository<Cursus,Long> {
Cursus findById(long id);
}

View File

@ -0,0 +1,23 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.CourseRepository;
import ovh.herisson.Clyde.Tables.Course;
@Service
public class CourseService {
private final CourseRepository courseRepo;
public CourseService(CourseRepository courseRepo) {
this.courseRepo = courseRepo;
}
public void save(Course course){
courseRepo.save(course);
}
public Course findById(long id){
return courseRepo.findById(id);
}
}

View File

@ -0,0 +1,68 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.CourseRepository;
import ovh.herisson.Clyde.Repositories.CursusCourseRepository;
import ovh.herisson.Clyde.Repositories.CursusRepository;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Cursus;
import ovh.herisson.Clyde.Tables.CursusCourse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@Service
public class CursusCourseService {
private final CursusCourseRepository cursusCourseRepo;
private final CourseRepository courseRepo;
private final CursusRepository cursusRepo;
public CursusCourseService(CursusCourseRepository cursusCourseRepo, CourseRepository courseRepo, CursusRepository cursusRepo) {
this.cursusCourseRepo = cursusCourseRepo;
this.courseRepo = courseRepo;
this.cursusRepo = cursusRepo;
}
public void save(CursusCourse cursusCourse){
cursusCourseRepo.save(cursusCourse);
}
public Iterable<CursusCourse> findAll(){
return cursusCourseRepo.findAll();
}
public Map<String, Object> getDepthCursus(Cursus cursus){
HashMap<String ,Object> toReturn = new HashMap<>();
ArrayList<Course> courses = new ArrayList<>();
for (Course c: cursusCourseRepo.findCoursesByCursus(cursus)){
courses.add(c);
}
toReturn.put("courses",courses);
toReturn.put("cursusId",cursus.getCursusId());
toReturn.put("year",cursus.getYear());
toReturn.put("option",cursus.getOption());
return toReturn;
}
public Iterable<Map<String, Object>> getAllDepthCursus(){
ArrayList<Map<String,Object>> toReturn = new ArrayList<>();
for (Cursus cursus : cursusCourseRepo.findDistinctCursuses()){
toReturn.add(getDepthCursus(cursus));
}
return toReturn;
}
}

View File

@ -0,0 +1,36 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.CourseRepository;
import ovh.herisson.Clyde.Repositories.CursusRepository;
import ovh.herisson.Clyde.Tables.Cursus;
import java.util.HashMap;
import java.util.Map;
@Service
public class CursusService {
private final CursusRepository cursusRepo;
private final CourseRepository courseRepo;
public CursusService(CursusRepository cursusRepo, CourseRepository courseRepo){
this.cursusRepo = cursusRepo;
this.courseRepo = courseRepo;
}
public void save(Cursus cursus){
cursusRepo.save(cursus);
}
public Cursus findById(long id){
return cursusRepo.findById(id);
}
public Iterable<Cursus> findAll(){
return cursusRepo.findAll();
}
}
Maxime marked this conversation as resolved
Review

juste pour l'example du commentaire suivants, ce service complet pourrait est dispensable puisqu'il ne fait que délivrer des fonctions déjà existante. Mais je comprends que pour la structure du projet cette class existe so is ok.

juste pour l'example du commentaire suivants, ce service complet pourrait est dispensable puisqu'il ne fait que délivrer des fonctions déjà existante. Mais je comprends que pour la structure du projet cette class existe so is ok.

View File

@ -12,13 +12,6 @@ public class Cursus {
private int cursusId;
private int year;
private String option;
public static Cursus infoBab1 = new Cursus(1,"info");
public static Cursus chemistryBab1 = new Cursus(1,"chemistry");
public static Cursus psychologyBab1 = new Cursus(1,"psychology");
public Cursus(int year, String option){
this.year = year;
this.option = option;

View File

@ -8,11 +8,11 @@ public class CursusCourse {
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Cursus")
private Cursus cursus;
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Course")
private Course course;

View File

@ -1,8 +0,0 @@
package ovh.herisson.Clyde.Tables;
public enum CursusType {
infoBab1,
chemistryBab1,
psychologyBab1;
}

View File

@ -4,7 +4,10 @@ import jakarta.persistence.*;
import java.util.Date;
@Entity
public class InscriptionRequest {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String firstName;

View File

@ -1,16 +1,15 @@
package ovh.herisson.Clyde.Tables;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;
@Entity
public class ReinscriptionRequest {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne
@JoinColumn(name = "User")
@JoinColumn(name = "Users")
private User user;
@ManyToOne