diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java index 77b2f3e..20cd8d4 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java @@ -30,7 +30,6 @@ public class ApplicationsController { */ @GetMapping("/apps") public ResponseEntity> getAuthorizedApps(@RequestHeader("Authorization") String token){ - return new ResponseEntity<>(getAuthorizedApplications(token), HttpStatus.OK); } @@ -46,24 +45,29 @@ public class ApplicationsController { public ArrayList getAuthorizedApplications(String token){ ArrayList authorizedApps = new ArrayList<>(); + //if unAuthed authorizedApps.add(Applications.Login); - authorizedApps.add(Applications.Profile); User user = authServ.getUserFromToken(token); if(user == null) - return authorizedApps; + return authorizedApps; + // if authed + authorizedApps.add(Applications.Profile); - Role posterRole = user.getRole(); + Role posterRole = user.getRole(); - if (posterRole == Role.Teacher || posterRole == Role.Student || posterRole == Role.Admin){ + if (!authServ.IsNotIn(new Role[]{Role.Teacher,Role.Student,Role.Admin},token)) { authorizedApps.add(Applications.Msg); authorizedApps.add(Applications.Forum); authorizedApps.add(Applications.Rdv); } - if (posterRole == Role.Teacher || posterRole == Role.Secretary || posterRole == Role.Admin) authorizedApps.add(Applications.ManageCourses); + //if Teacher or Secretary or Admin add ManageCourses App + if (!authServ.IsNotIn(new Role[]{Role.Teacher,Role.Secretary,Role.Admin},token)) + authorizedApps.add(Applications.ManageCourses); - if (posterRole == Role.InscriptionService || posterRole == Role.Admin) authorizedApps.add(Applications.Inscription); + if (!authServ.IsNotIn(new Role[]{Role.InscriptionService,Role.Admin},token)) + authorizedApps.add(Applications.Inscription); return authorizedApps; } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java index 011689f..2a1af6e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java @@ -9,10 +9,6 @@ import ovh.herisson.Clyde.Services.CourseService; import ovh.herisson.Clyde.Services.TeacherCourseService; import ovh.herisson.Clyde.Tables.Course; import ovh.herisson.Clyde.Tables.Role; -import ovh.herisson.Clyde.Tables.TeacherCourse; -import ovh.herisson.Clyde.Tables.User; - -import java.util.ArrayList; import java.util.Map; @RestController @@ -36,13 +32,21 @@ public class CourseController { if (authServ.getUserFromToken(token) == null) return new UnauthorizedResponse<>(null); - return new ResponseEntity<>(courseServ.findById(id), HttpStatus.OK); + Course foundCourse = courseServ.findById(id); + + if (foundCourse == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(foundCourse, HttpStatus.OK); } @PostMapping("/course") - public ResponseEntity postCourse(@RequestHeader("Authorization") String token, @RequestBody Course course){ - if (authServ.isNotSecretaryOrAdmin(token)) + public ResponseEntity postCourse(@RequestHeader("Authorization") String token, + @RequestBody Course course) + { + + if (authServ.IsNotIn(new Role[]{Role.Secretary,Role.Admin},token)) return new UnauthorizedResponse<>(null); return new ResponseEntity<>(courseServ.save(course), HttpStatus.CREATED); @@ -55,11 +59,15 @@ public class CourseController { @PathVariable long id) { - if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Teacher,Role.Secretary}, token)){ + if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Teacher,Role.Secretary}, token)) return new UnauthorizedResponse<>(null); - } - return new ResponseEntity<>(courseServ.modifyData(id, updates, authServ.getUserFromToken(token).getRole()), HttpStatus.OK); + Course modifiedCourse = courseServ.modifyData(id,updates,authServ.getUserFromToken(token).getRole()); + + if (modifiedCourse == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(modifiedCourse, HttpStatus.OK); } @PostMapping("/course/{id}") @@ -67,14 +75,14 @@ public class CourseController { @RequestBody Iterable teacherIds, @PathVariable Long id) { + if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Secretary}, token)) return new UnauthorizedResponse<>(null); + if (!teacherCourseServ.saveAll(teacherIds,courseServ.findById(id))) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - teacherCourseServ.saveAll(teacherIds,courseServ.findById(id)); return new ResponseEntity<>(HttpStatus.OK); - } - } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java index 1892d6c..0b0abb8 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java @@ -4,13 +4,12 @@ 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.CurriculumCourseService; import ovh.herisson.Clyde.Services.CurriculumService; import ovh.herisson.Clyde.Tables.Curriculum; -import ovh.herisson.Clyde.Tables.CurriculumCourse; import ovh.herisson.Clyde.Tables.Role; -import ovh.herisson.Clyde.Tables.User; import java.util.Map; @@ -32,29 +31,25 @@ public class CurriculumController { @GetMapping("/curriculum/{id}") public ResponseEntity findById(@PathVariable long id){ - return new ResponseEntity<>(curriculumServ.findById(id), HttpStatus.OK); + Curriculum foundCurriculum = curriculumServ.findById(id); + + if (foundCurriculum == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(foundCurriculum, HttpStatus.OK); } @GetMapping("/curriculums") - public ResponseEntity>> findAllindDepth(){ + public ResponseEntity>> findAllIndDepth(){ return new ResponseEntity<>(curriculumCourseServ.getAllDepthCurriculum(),HttpStatus.OK); } - @GetMapping("/curriculum") - public ResponseEntity> findAll(){ - return new ResponseEntity<>(curriculumCourseServ.findAll(),HttpStatus.OK); + @PostMapping("/curriculum") + public ResponseEntity postCurriculum(@RequestHeader("Authorization") String token,@RequestBody Curriculum curriculum){ + + if (authServ.IsNotIn(new Role[]{Role.Secretary,Role.Admin},token)) + return new UnauthorizedResponse<>(null); + + return new ResponseEntity<>(curriculumServ.save(curriculum),HttpStatus.CREATED); } - - /**@PostMapping("/curriculum") //todo now - public ResponseEntity postCurriculum(@RequestHeader("Authorization") String token,@RequestBody Curriculum curriculum){ - - if (!isSecretaryOrAdmin(token)){ - return new UnauthorizedResponse<>("you're not allowed to post a Curriculum"); - } - - CurriculumServ.save(Curriculum); - - return new ResponseEntity<>("created !",HttpStatus.CREATED); - }**/ - } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java index 36946b5..42d6551 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -9,14 +9,12 @@ import ovh.herisson.Clyde.Services.InscriptionService; import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.RequestState; import ovh.herisson.Clyde.Tables.Role; -import ovh.herisson.Clyde.Tables.User; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @RestController @CrossOrigin(originPatterns = "*", allowCredentials = "true") - public class InscriptionController { @@ -32,7 +30,8 @@ public class InscriptionController { @GetMapping("/requests/register") public ResponseEntity>> getAllRequests(@RequestHeader("Authorization") String token){ - if (authServ.isNotSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);} + if (authServ.IsNotIn(new Role[]{Role.Admin,Role.InscriptionService},token)) + return new UnauthorizedResponse<>(null); Iterable inscriptionRequests = inscriptionServ.getAll(); ArrayList> toReturn = new ArrayList<>(); @@ -46,41 +45,64 @@ public class InscriptionController { @GetMapping("/request/register/{id}") - public ResponseEntity> getById(@PathVariable long id){ - InscriptionRequest inscriptionRequest = inscriptionServ.getById(id); - if (inscriptionRequest == null) {return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);} + public ResponseEntity> getById(@RequestHeader("Authorization") String token, @PathVariable long id){ - return new ResponseEntity<>(requestWithoutPassword(inscriptionRequest), HttpStatus.OK); + if (authServ.IsNotIn(new Role[]{Role.Admin,Role.InscriptionService},token)) + return new UnauthorizedResponse<>(null); + + InscriptionRequest foundInscriptionRequest = inscriptionServ.getById(id); + + if (foundInscriptionRequest == null) + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(requestWithoutPassword(foundInscriptionRequest), HttpStatus.OK); } - @GetMapping("request/user/{id}") - public ResponseEntity getUserInscriptionRequest(@PathVariable long id, @RequestHeader("Authorize") String token){ + /** + @GetMapping("request/user") + public ResponseEntity getUserInscriptionRequest(@RequestHeader("Authorization") String token){ //todo return l'inscriptionRequest ACTUELLE du user (check si le poster est bien le même que id target ou secretariat) + + if (authServ.IsNotIn(new Role[]{Role.Student,Role.Admin},token)) + return new UnauthorizedResponse<>(null); + + User poster = authServ.getUserFromToken(token); + + inscriptionServ.getById() + + return null; - } + } **/ @PatchMapping("/request/register/{id}") public ResponseEntity changeRequestState(@PathVariable long id, - @RequestHeader("Authorize") String token, + @RequestHeader("Authorization") String token, @RequestBody RequestState requestState) { - if (authServ.isNotSecretaryOrAdmin(token)) return new UnauthorizedResponse<>(null); - inscriptionServ.modifyState(id, requestState); - return null; + + if (authServ.IsNotIn(new Role[]{Role.InscriptionService,Role.Admin},token)) + return new UnauthorizedResponse<>(null); + + if (!inscriptionServ.modifyState(id, requestState)) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(HttpStatus.OK); } private Map requestWithoutPassword(InscriptionRequest inscriptionRequest) { Map toReturn = new HashMap<>(); toReturn.put("id", inscriptionRequest.getId()); - toReturn.put("firstName", inscriptionRequest.getFirstName()); toReturn.put("lastName", inscriptionRequest.getLastName()); + toReturn.put("firstName", inscriptionRequest.getFirstName()); toReturn.put("address", inscriptionRequest.getAddress()); + toReturn.put("email",inscriptionRequest.getEmail()); toReturn.put("birthDate", inscriptionRequest.getBirthDate()); toReturn.put("country", inscriptionRequest.getCountry()); toReturn.put("curriculum", inscriptionRequest.getCurriculum()); - toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); toReturn.put("state", inscriptionRequest.getState()); + toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); + return toReturn; } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java index 2be125d..6e0b4fa 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java @@ -1,4 +1,5 @@ package ovh.herisson.Clyde.EndPoints; + import com.fasterxml.jackson.annotation.JsonFormat; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -7,7 +8,6 @@ import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Tables.InscriptionRequest; - import java.util.Date; @RestController @@ -45,8 +45,7 @@ public class LoginController { } @PostMapping("/request/register") - public ResponseEntity register(@RequestBody InscriptionRequest inscriptionRequest){ - authServ.register(inscriptionRequest); - return new ResponseEntity<>("Is OK", HttpStatus.CREATED); + public ResponseEntity register(@RequestBody InscriptionRequest inscriptionRequest){ + return new ResponseEntity<>(authServ.register(inscriptionRequest), HttpStatus.CREATED); } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index 1750889..358602b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -6,7 +6,6 @@ import ovh.herisson.Clyde.Repositories.TokenRepository; import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Services.*; import ovh.herisson.Clyde.Tables.*; - import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -51,12 +50,11 @@ public class MockController { 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.Secretary,passwordEncoder.encode("secretary")); User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher")); - User lena = new User("Louille","Lena","inscriptionService@InscriptionService.com","no","yes",new Date(0), null,Role.Teacher,passwordEncoder.encode("inscriptionService")); - mockUsers = new ArrayList<>(Arrays.asList(herobrine,joe,meh,joke)); + User lena = new User("Louille","Lena","inscriptionService@InscriptionService.com","no","yes",new Date(0), null,Role.InscriptionService,passwordEncoder.encode("inscriptionService")); + mockUsers = new ArrayList<>(Arrays.asList(herobrine,joe,meh,joke,lena)); userRepo.saveAll(mockUsers); - // Course / Curriculum part Curriculum infoBab1 = new Curriculum(1,"info"); @@ -68,7 +66,7 @@ public class MockController { curriculumService.save(psychologyBab1); - Course progra1 = new Course(5,"Programmation et algorithimque 1",joke); + Course progra1 = new Course(5,"Programmation et algorithmique 1",joke); Course chemistry1 = new Course(12, "Thermochimie",joke); Course psycho1 = new Course(21, "rien faire t'as cru c'est psycho",joke); Course commun = new Course(2, "cours commun",joke); @@ -89,16 +87,6 @@ public class MockController { CurriculumCourseService.save(new CurriculumCourse(chemistryBab1,commun)); CurriculumCourseService.save(new CurriculumCourse(chemistryBab1,chemistry1)); - - - } - - @DeleteMapping("/mock") - public void deleteMock(){ - for (User user:mockUsers){ - tokenRepo.deleteAll(tokenRepo.getByUser(user)); - } - userRepo.deleteAll(mockUsers); } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java index 724ae10..d715087 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java @@ -22,12 +22,13 @@ public class StorageController { @PostMapping("/upload/{fileType}") public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable FileType fileType) { - StorageFile fileEntry = null; + StorageFile fileEntry; try { fileEntry = storageServ.store(file,fileType); } catch(Exception e){ - e.printStackTrace(); + e.printStackTrace(); + return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/TokenController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/TokenController.java index ba07fca..1d18881 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/TokenController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/TokenController.java @@ -1,11 +1,15 @@ package ovh.herisson.Clyde.EndPoints; - - +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RestController; +import ovh.herisson.Clyde.Responses.UnauthorizedResponse; +import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.TokenService; +import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Token; @RestController @@ -14,13 +18,20 @@ public class TokenController { private final TokenService tokenServ; - public TokenController(TokenService tokenServ){ + private final AuthenticatorService authServ; + + public TokenController(TokenService tokenServ, AuthenticatorService authServ){ this.tokenServ = tokenServ; + this.authServ = authServ; } @GetMapping("/tokens") - public Iterable getTokens(){ - return tokenServ.getAllTokens(); + public ResponseEntity> getTokens(@RequestHeader("Authorization")String token){ + + if (authServ.IsNotIn(new Role[]{Role.Admin},token)) + return new UnauthorizedResponse<>(null); + + return new ResponseEntity<>(tokenServ.getAllTokens(), HttpStatus.OK); } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java index 8437b81..fd6151e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -1,6 +1,5 @@ package ovh.herisson.Clyde.EndPoints; - import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -9,8 +8,6 @@ import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.UserService; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; - -import java.security.Key; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -27,30 +24,33 @@ public class UserController { this.authServ = authServ; } + /** returns information about the connected user + * + * @param token the session token of the user + * @return the user information except his password + */ @GetMapping("/user") - public ResponseEntity> getUser(@RequestHeader("Authorization") String authorization){ + public ResponseEntity> getUser(@RequestHeader("Authorization") String token){ - if (authorization == null) return new UnauthorizedResponse<>(null); - User user = authServ.getUserFromToken(authorization); - if (user == null) return new UnauthorizedResponse<>(null); + User user = authServ.getUserFromToken(token); + if (user == null) return new UnauthorizedResponse<>(null); return new ResponseEntity<>(userWithoutPassword(user), HttpStatus.OK); } @PostMapping("/user") - public ResponseEntity postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){ + public ResponseEntity> postUser(@RequestBody User user,@RequestHeader("Authorization") String token){ - if (authServ.isNotSecretaryOrAdmin(authorization)) + if (authServ.IsNotIn(new Role[]{Role.Admin,Role.InscriptionService,Role.Secretary},token)) return new UnauthorizedResponse<>(null); - userService.save(user); - return new ResponseEntity<>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED); + return new ResponseEntity<>(userWithoutPassword(userService.save(user)),HttpStatus.CREATED); } @GetMapping("/users") - public ResponseEntity>> getAllUsers(@RequestHeader("Authorization") String authorization){ + public ResponseEntity>> getAllUsers(@RequestHeader("Authorization") String token){ - if (authServ.isNotSecretaryOrAdmin(authorization)) + if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Secretary},token)) return new UnauthorizedResponse<>(null); Iterable users = userService.getAll(); @@ -61,24 +61,36 @@ public class UserController { } return new ResponseEntity<>(withoutPassword, HttpStatus.OK); } - @PatchMapping("/user") - public ResponseEntity patchUser(@RequestBody Map updates, @RequestHeader("Authorization") String authorization) { - if (authorization == null) return new UnauthorizedResponse<>(null); + /** changes the specified user's information + * + * @param updates the changes to be made + * @param token the session token of the user posting the change + * @param id the id of the user to change + * @return a string clarifying the issue (if there is any) + */ + @PatchMapping("/user/{id}") + public ResponseEntity patchUser(@RequestHeader("Authorization") String token, + @RequestBody Map updates, + @PathVariable Long id) { - User poster = authServ.getUserFromToken(authorization); - if (poster == null) {return new UnauthorizedResponse<>("bad authorization");} + if (token == null) return new UnauthorizedResponse<>(null); - if (!userService.modifyData(poster, updates, poster)) + User poster = authServ.getUserFromToken(token); + if (poster == null) {return new UnauthorizedResponse<>("bad token");} + + if (!userService.modifyData(id, updates, poster)) return new UnauthorizedResponse<>("there was an issue with the updates requested"); - return new ResponseEntity<>("data modified", HttpStatus.OK); + return new ResponseEntity<>(null, HttpStatus.OK); } @GetMapping("/teachers") public ResponseEntity>> getAllTeachers(@RequestHeader("Authorization") String token){ + if (authServ.getUserFromToken(token) == null) return new UnauthorizedResponse<>(null); + Iterable teachers = userService.getAllTeachers(); ArrayList> withoutPassword = new ArrayList<>(); @@ -98,11 +110,13 @@ public class UserController { private HashMap userWithoutPassword(User user){ HashMap toReturn = new HashMap<>(); toReturn.put("regNo",user.getRegNo()); - toReturn.put("firstName",user.getFirstName()); toReturn.put("lastName",user.getLastName()); + toReturn.put("firstName",user.getFirstName()); + toReturn.put("email", user.getEmail()); + toReturn.put("address",user.getAddress()); toReturn.put("birthDate",user.getBirthDate()); toReturn.put("country",user.getCountry()); - toReturn.put("address",user.getAddress()); + toReturn.put("profilePictureUrl",user.getProfilePictureUrl()); toReturn.put("role",user.getRole()); return toReturn; } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java index 63ef3c1..a665096 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -35,8 +35,8 @@ public class AuthenticatorService { return token; } - public void register(InscriptionRequest inscriptionRequest) { - inscriptionService.save(inscriptionRequest); + public InscriptionRequest register(InscriptionRequest inscriptionRequest) { + return inscriptionService.save(inscriptionRequest); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java index 8278bbe..483e865 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java @@ -28,6 +28,9 @@ public class CourseService { public Course modifyData(long id, Map updates, Role role) { Course target = courseRepo.findById(id); + if (target == null) + return null; + if (role == Role.Teacher){ for (Map.Entry entry : updates.entrySet()){ if (entry.getKey().equals("title")){ @@ -52,4 +55,8 @@ public class CourseService { } return courseRepo.save(target); } + + public void delete(Long id) { + courseRepo.deleteById(id); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumService.java index 6f6d89b..04c6ab2 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumService.java @@ -1,7 +1,6 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; -import ovh.herisson.Clyde.Repositories.CourseRepository; import ovh.herisson.Clyde.Repositories.CurriculumRepository; import ovh.herisson.Clyde.Tables.Curriculum; @@ -10,23 +9,18 @@ public class CurriculumService { private final CurriculumRepository curriculumRepo; - private final CourseRepository courseRepo; - - public CurriculumService(CurriculumRepository curriculumRepo, CourseRepository courseRepo){ + public CurriculumService(CurriculumRepository curriculumRepo){ this.curriculumRepo = curriculumRepo; - this.courseRepo = courseRepo; } - - - public void save(Curriculum curriculum){ - curriculumRepo.save(curriculum); + public Curriculum save(Curriculum curriculum){ + return curriculumRepo.save(curriculum); } public Curriculum findById(long id){ return curriculumRepo.findById(id); } - public Iterable findAll(){ - return curriculumRepo.findAll(); + public void delete(Long id) { + curriculumRepo.deleteById(id); } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java index 6130fe8..31b18a6 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java @@ -14,8 +14,8 @@ public class InscriptionService { this.inscriptionRepo = inscriptionRepo; } - public void save(InscriptionRequest inscriptionRequest){ - inscriptionRepo.save(inscriptionRequest); + public InscriptionRequest save(InscriptionRequest inscriptionRequest){ + return inscriptionRepo.save(inscriptionRequest); } public InscriptionRequest getById(long id){ @@ -26,9 +26,14 @@ public class InscriptionService { return inscriptionRepo.findAll(); } - public void modifyState(long id, RequestState requestState) { + public boolean modifyState(long id, RequestState requestState) { InscriptionRequest inscriptionRequest = getById(id); + + if (inscriptionRequest == null) + return false; + inscriptionRequest.setState(requestState); save(inscriptionRequest); + return true; } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/TeacherCourseService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/TeacherCourseService.java index 0996adf..83135ea 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/TeacherCourseService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/TeacherCourseService.java @@ -22,6 +22,9 @@ public class TeacherCourseService { public boolean saveAll(Iterable teacherIds, Course course){ + if (course == null) + return false; + ArrayList addedIds = new ArrayList<>(); for (Long teacherId : teacherIds){ User teacher = userRepo.findById((long) teacherId); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java index a561512..ee45d90 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -36,7 +36,11 @@ public class UserService { * @param target the user to update * @return if the changes were done or not */ - public boolean modifyData(User poster, Map updates, User target){ + public boolean modifyData(long targetId, Map updates, User poster){ + + User target = userRepo.findById(targetId); + if (target == null) + return false; if (poster.getRegNo().equals(target.getRegNo())){ for (Map.Entry entry : updates.entrySet()){ @@ -80,7 +84,7 @@ public class UserService { if ( !entry.getKey().equals("role")) {return false;} - if (entry.getValue() == Role.Admin){return false;} + if (entry.getValue() == Role.Admin) {return false;} target.setRole((Role) entry.getValue()); userRepo.save(target); @@ -95,9 +99,9 @@ public class UserService { return passwordEncoder.matches(tryingPassword, user.getPassword()); } - public void save(User user){ + public User save(User user){ user.setPassword(passwordEncoder.encode(user.getPassword())); - userRepo.save(user); + return userRepo.save(user); } public Iterable getAll(){ diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java index dfbf7ed..bc781be 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java @@ -1,7 +1,6 @@ package ovh.herisson.Clyde.Tables; import jakarta.persistence.*; - import java.util.Date; @@ -25,13 +24,14 @@ public class InscriptionRequest { private String password; public InscriptionRequest(){} - public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate, RequestState state, String profilePicture, String password){ + public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate,Curriculum curriculum, RequestState state, String profilePicture, String password){ this.lastName = lastName; this.firstName = firstName; this.address = address; this.email = email; this.country = country; this.birthDate = birthDate; + this.curriculum = curriculum; this.state = state; this.profilePicture = profilePicture; this.password = password;