From 1b908742570a3be631a7702148528cd184cab917 Mon Sep 17 00:00:00 2001 From: louay belhassen Date: Thu, 10 Feb 2022 12:17:27 +0100 Subject: [PATCH 1/6] update gitignore --- .gitignore | 1 + src/main/java/Model/Controllers/QuestionController.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4c8fa6d..0fddff5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ **/*~ .idea +.gitignore **/*.class *.iml target/* diff --git a/src/main/java/Model/Controllers/QuestionController.java b/src/main/java/Model/Controllers/QuestionController.java index c5feec2..24536e5 100644 --- a/src/main/java/Model/Controllers/QuestionController.java +++ b/src/main/java/Model/Controllers/QuestionController.java @@ -18,7 +18,7 @@ @CrossOrigin(origins = "*", maxAge = 3600) @RestController -@RequestMapping("/api/module/questionaire") +@RequestMapping("/api/module/questionnaire") public class QuestionController { @Autowired RessourcesRepository ressourcesRepository; From 17ec3a6a0d3b3ee2926e5731f5505c1ea480886f Mon Sep 17 00:00:00 2001 From: louay belhassen Date: Thu, 10 Feb 2022 12:20:12 +0100 Subject: [PATCH 2/6] update gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0fddff5..a6b31c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ **/*~ .idea -.gitignore +*.properties **/*.class *.iml target/* From 6508246e8658b2b6843f1896cacada4fe2cefc6a Mon Sep 17 00:00:00 2001 From: louay belhassen Date: Thu, 10 Feb 2022 12:23:31 +0100 Subject: [PATCH 3/6] update properties --- src/main/resources/application.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 98520ec..32e9bb0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ spring.datasource.url= jdbc:postgresql://localhost:5432/postgres spring.datasource.username= postgres -spring.datasource.password= Einstein06 -#logging.level.root=error +spring.datasource.password= gestionduprojet +logging.level.root=error spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect From a054b4e26952c528257e2efaec5c45852e427400 Mon Sep 17 00:00:00 2001 From: louay belhassen Date: Thu, 10 Feb 2022 15:02:55 +0100 Subject: [PATCH 4/6] create Get.feature --- .../Model/Controllers/CourseController.java | 36 +++++++++++++-- .../Model/Controllers/ModuleController.java | 18 +++++--- .../Controllers/QuestionnaireController.java | 45 +++++++++++++++++-- src/test/resources/features/Get.feature | 10 +++++ 4 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 src/test/resources/features/Get.feature diff --git a/src/main/java/Model/Controllers/CourseController.java b/src/main/java/Model/Controllers/CourseController.java index 8da35f2..698f416 100644 --- a/src/main/java/Model/Controllers/CourseController.java +++ b/src/main/java/Model/Controllers/CourseController.java @@ -1,9 +1,12 @@ package Model.Controllers; import Model.Documents.Cours; +import Model.Documents.Module; import Model.Documents.Ressource; import Model.Payload.response.MessageResponse; import Model.Repositories.CoursesRepository; +import Model.Repositories.ModuleRepository; import Model.Repositories.RessourcesRepository; +import Model.Repositories.UserRepository; import Model.User.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -11,9 +14,11 @@ import org.springframework.web.bind.annotation.*; import java.security.Principal; +import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; @CrossOrigin(origins = "*", maxAge = 3600) @RestController @@ -23,6 +28,12 @@ public class CourseController { @Autowired RessourcesRepository ressourcesRepository; + @Autowired + ModuleRepository moduleRepository; + + @Autowired + UserRepository userRepository; + @Autowired CoursesRepository coursesRepository; @@ -116,10 +127,29 @@ public ResponseEntity deletRessource(Principal principal, @PathVariable long * verif that the student have acces to the courses in this module * */ - @GetMapping("/api/{idStudent}/module/courses") - public List getCourses(){ + @GetMapping("/api/{idStudent}/module/{idModule}/courses") + public List getCourses(@PathVariable Long idStudent, @PathVariable Long idModule ){ + boolean find =false; + Optional m = moduleRepository.findById(idModule); + if (m.isPresent()) { + Module moduleCurr = m.get(); + for (User participant : moduleCurr.getParticipants()) { + + if(participant.getId() == idStudent) + { + find =true; + } + + } + if (find) { + + // verifier si un cours ou pas + return moduleCurr.getRessources().stream().filter(r -> r.getClass().equals(Cours.class)).collect(Collectors.toList()); + } + + } - return coursesRepository.findAll(); + return null; } diff --git a/src/main/java/Model/Controllers/ModuleController.java b/src/main/java/Model/Controllers/ModuleController.java index a67dda0..5b7ab37 100644 --- a/src/main/java/Model/Controllers/ModuleController.java +++ b/src/main/java/Model/Controllers/ModuleController.java @@ -25,7 +25,7 @@ @CrossOrigin(origins = "*", maxAge = 3600) @RestController -@RequestMapping("/api/module") +@RequestMapping("/api/modules") public class ModuleController { @Autowired AuthenticationManager authenticationManager; @@ -49,10 +49,10 @@ public class ModuleController { JwtUtils jwtUtils; - @GetMapping("/{name}/getModules") - public ArrayList getmodules(@PathVariable String name){ - ArrayList strings = new ArrayList<>(); - Optional ouser = userRepository.findByUsername(name); + @GetMapping("/{idUser}") + public ArrayList getmodules(@PathVariable Long idUser){ + ArrayList userModules = new ArrayList<>(); + Optional ouser = userRepository.findById(idUser); if (!ouser.isPresent()) { return null; } @@ -60,11 +60,15 @@ public ArrayList getmodules(@PathVariable String name){ Set modules = user.getModules(); for (Module module:modules) { - strings.add(module.name + "/" + module.id); + userModules.add(module.name + "/" + module.id); } - return strings; + return userModules; } + + + + @GetMapping("/{id}/getRessources") public ArrayList getRessources(@PathVariable long id){ ArrayList strings = new ArrayList<>(); diff --git a/src/main/java/Model/Controllers/QuestionnaireController.java b/src/main/java/Model/Controllers/QuestionnaireController.java index 4f71909..18033aa 100644 --- a/src/main/java/Model/Controllers/QuestionnaireController.java +++ b/src/main/java/Model/Controllers/QuestionnaireController.java @@ -1,12 +1,18 @@ package Model.Controllers; +import Model.Documents.Cours; +import Model.Documents.Module; import Model.Documents.Questionnaire; +import Model.Repositories.ModuleRepository; import Model.Repositories.QuestionnaireRepository; +import Model.Repositories.UserRepository; +import Model.User.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Optional; +import java.util.stream.Collectors; @CrossOrigin(origins = "*", maxAge = 3600) @RestController @@ -16,6 +22,13 @@ public class QuestionnaireController { @Autowired QuestionnaireRepository questionnaireRepository ; + @Autowired + ModuleRepository moduleRepository ; + + @Autowired + UserRepository userRepository ; + + /** * Read - Get all questions of a questionnaire of a student @@ -27,11 +40,37 @@ public class QuestionnaireController { * verif that the student have acces to the list of questions in this module * */ - @GetMapping("/api/{idStudent}/module/questionnaires/{idQuestionnaire}") - public Optional getQuestionnaire(final Long idQuestionnaire){ + @GetMapping("/api/{idStudent}/module/{idModule}/questionnaires/{idQuestionnaire}") + public Optional getQuestionnaire(final Long idQuestionnaire,final Long idModule,final Long idUser){ - return questionnaireRepository.findById(idQuestionnaire) ; + if (isHaveAcces(idModule,idUser)) + { + return questionnaireRepository.findById(idQuestionnaire) ; + + } + return null; + + + } + + + + boolean isHaveAcces(Long idModule ,Long idUser) + { + + boolean find =false; + Optional m = moduleRepository.findById(idModule); + if (m.isPresent()) { + Module moduleCurr = m.get(); + for (User participant : moduleCurr.getParticipants()) { + + if(participant.getId() == idUser) + { + find =true; + } + } + return find ; } /*** diff --git a/src/test/resources/features/Get.feature b/src/test/resources/features/Get.feature new file mode 100644 index 0000000..10a7a76 --- /dev/null +++ b/src/test/resources/features/Get.feature @@ -0,0 +1,10 @@ +Feature: Get + + Background: + Given a user with login "steve" + And a module named "Gestion de projet" + And a cours named "Gestion" + + Scenario: user check all modules that he subsccribe to + When "steve" check his modules + Then return all modules names \ No newline at end of file From 70d0459b4433be03ba4aa6325c83515bfa044888 Mon Sep 17 00:00:00 2001 From: louay belhassen Date: Fri, 11 Feb 2022 10:06:35 +0100 Subject: [PATCH 5/6] update module controller --- .../Model/Controllers/AuthController.java | 2 + .../Model/Controllers/CourseController.java | 4 +- .../Model/Controllers/ModuleController.java | 12 +++++ .../Controllers/QuestionnaireController.java | 44 ++++++------------- src/main/resources/application.properties | 2 +- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/main/java/Model/Controllers/AuthController.java b/src/main/java/Model/Controllers/AuthController.java index a8cb1c2..c018951 100644 --- a/src/main/java/Model/Controllers/AuthController.java +++ b/src/main/java/Model/Controllers/AuthController.java @@ -61,6 +61,8 @@ public String generateJwt(String userName, String password) { } + + @PostMapping("/a") public ResponseEntity authenticateUser(@RequestBody SignupRequest signupRequest){ diff --git a/src/main/java/Model/Controllers/CourseController.java b/src/main/java/Model/Controllers/CourseController.java index 698f416..d86ab95 100644 --- a/src/main/java/Model/Controllers/CourseController.java +++ b/src/main/java/Model/Controllers/CourseController.java @@ -37,8 +37,10 @@ public class CourseController { @Autowired CoursesRepository coursesRepository; - @PutMapping("/{name}/content/{text}") + + + @PutMapping("/{name}/content/{text}") @PreAuthorize("hasRole('TEACHER')") public ResponseEntity modifierCours(Principal principal, @PathVariable String name, @PathVariable String text){ // Vérifier si ce resource existe diff --git a/src/main/java/Model/Controllers/ModuleController.java b/src/main/java/Model/Controllers/ModuleController.java index 5b7ab37..0d23f85 100644 --- a/src/main/java/Model/Controllers/ModuleController.java +++ b/src/main/java/Model/Controllers/ModuleController.java @@ -49,9 +49,21 @@ public class ModuleController { JwtUtils jwtUtils; + @PreAuthorize("hasRole('TEACHER')") + @GetMapping("/who") + public ArrayList getPersonne(Principal principal) { + ArrayList data = new ArrayList<>(); + data.add("louay"); + data.add(principal.getName()); + return data ; + + } + + @GetMapping("/{idUser}") public ArrayList getmodules(@PathVariable Long idUser){ ArrayList userModules = new ArrayList<>(); + userModules.add("test1"); Optional ouser = userRepository.findById(idUser); if (!ouser.isPresent()) { return null; diff --git a/src/main/java/Model/Controllers/QuestionnaireController.java b/src/main/java/Model/Controllers/QuestionnaireController.java index 18033aa..15b7104 100644 --- a/src/main/java/Model/Controllers/QuestionnaireController.java +++ b/src/main/java/Model/Controllers/QuestionnaireController.java @@ -20,32 +20,30 @@ public class QuestionnaireController { @Autowired - QuestionnaireRepository questionnaireRepository ; + QuestionnaireRepository questionnaireRepository; @Autowired - ModuleRepository moduleRepository ; + ModuleRepository moduleRepository; @Autowired - UserRepository userRepository ; - + UserRepository userRepository; /** * Read - Get all questions of a questionnaire of a student + * * @return - An Iterable object of Questions * * - * + *

* Les utilisateurs peuvent connaitre la liste des questions * à ajouter: * verif that the student have acces to the list of questions in this module - * */ @GetMapping("/api/{idStudent}/module/{idModule}/questionnaires/{idQuestionnaire}") - public Optional getQuestionnaire(final Long idQuestionnaire,final Long idModule,final Long idUser){ + public Optional getQuestionnaire(final Long idQuestionnaire, final Long idModule, final Long idUser) { - if (isHaveAcces(idModule,idUser)) - { - return questionnaireRepository.findById(idQuestionnaire) ; + if (isHaveAcces(idModule, idUser)) { + return questionnaireRepository.findById(idQuestionnaire); } return null; @@ -54,38 +52,24 @@ public Optional getQuestionnaire(final Long idQuestionnaire,final } + boolean isHaveAcces(Long idModule, Long idUser) { - boolean isHaveAcces(Long idModule ,Long idUser) - { - - boolean find =false; + boolean find = false; Optional m = moduleRepository.findById(idModule); if (m.isPresent()) { Module moduleCurr = m.get(); for (User participant : moduleCurr.getParticipants()) { - if(participant.getId() == idUser) - { - find =true; + if (participant.getId() == idUser) { + find = true; } } - return find ; - } - /*** - * valider un questionnaire - * @param idQuestionnaire - * @return - */ - - - @PostMapping("/api/{idStudent}/module/questionnaires/{idQuestionnaire}") - public Optional validateQuestionnaire(final Long idQuestionnaire){ + } + return find; - return questionnaireRepository.findById(idQuestionnaire) ; } - } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 32e9bb0..7723645 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ spring.datasource.url= jdbc:postgresql://localhost:5432/postgres spring.datasource.username= postgres spring.datasource.password= gestionduprojet -logging.level.root=error +#logging.level.root=error spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect From 7fa8d8b45226619e0428bdc521f72399c27bd796 Mon Sep 17 00:00:00 2001 From: louay belhassen Date: Fri, 11 Feb 2022 11:19:38 +0100 Subject: [PATCH 6/6] update method getmodules --- .../Model/Controllers/ModuleController.java | 23 ++++++++++--------- src/main/java/Model/Documents/Module.java | 4 ++-- .../Model/Repositories/ModuleRepository.java | 1 - 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/Model/Controllers/ModuleController.java b/src/main/java/Model/Controllers/ModuleController.java index ad83408..2123bda 100644 --- a/src/main/java/Model/Controllers/ModuleController.java +++ b/src/main/java/Model/Controllers/ModuleController.java @@ -53,8 +53,7 @@ public class ModuleController { @GetMapping("/who") public ArrayList getPersonne(Principal principal) { ArrayList data = new ArrayList<>(); - data.add("louay"); - data.add(principal.getName()); + data.add("la personne connectée est " +principal.getName()); return data ; } @@ -63,7 +62,6 @@ public ArrayList getPersonne(Principal principal) { @GetMapping("/{idUser}") public ArrayList getmodules(@PathVariable Long idUser){ ArrayList userModules = new ArrayList<>(); - userModules.add("test1"); Optional ouser = userRepository.findById(idUser); if (!ouser.isPresent()) { return null; @@ -71,8 +69,9 @@ public ArrayList getmodules(@PathVariable Long idUser){ User user= ouser.get(); Set modules = user.getModules(); + for (Module module:modules) { - userModules.add(module.name + "/" + module.id); + userModules.add("module name : "+module.name + "| id : " + module.id); } return userModules; } @@ -179,22 +178,24 @@ public ResponseEntity addUser(Principal principal, @PathVariable long id, @Pa User actor = userRepository.findByUsername(principal.getName()).get(); Set participants = module.getParticipants(); - if ((participants.isEmpty() && actor.equals(user)) - || participants.contains(actor)) { +// if ((participants.isEmpty() && actor.equals(user)) +// || participants.contains(actor)) { // verifie si user n'apartient pas déjà à participants if(!participants.contains(user)) { participants.add(user); + user.getModules().add(module); }else{ return ResponseEntity .badRequest() .body(new MessageResponse("Error: User y apartient deja !")); } - } else { - return ResponseEntity - .badRequest() - .body(new MessageResponse("Error: Not allowed to add user!")); - } +// } else { +// return ResponseEntity +// .badRequest() +// .body(new MessageResponse("Error: Not allowed to add user!")); +// } moduleRepository.save(module); + userRepository.save(user); return ResponseEntity.ok(new MessageResponse("User successfully added to module!")); } diff --git a/src/main/java/Model/Documents/Module.java b/src/main/java/Model/Documents/Module.java index fd21d7b..d9fcbbf 100644 --- a/src/main/java/Model/Documents/Module.java +++ b/src/main/java/Model/Documents/Module.java @@ -24,11 +24,11 @@ public class Module { inverseJoinColumns = @JoinColumn(name = "ressource_id")) public Set ressources = new HashSet<>(); - @ManyToMany(fetch = FetchType.LAZY) + @ManyToMany(fetch = FetchType.EAGER) @JoinTable( name = "module_user", joinColumns = @JoinColumn(name = "module_id"), inverseJoinColumns = @JoinColumn(name = "user_id")) - public Set users; + public Set users = new HashSet<>(); public Module() { } diff --git a/src/main/java/Model/Repositories/ModuleRepository.java b/src/main/java/Model/Repositories/ModuleRepository.java index 29fe975..ded3d02 100644 --- a/src/main/java/Model/Repositories/ModuleRepository.java +++ b/src/main/java/Model/Repositories/ModuleRepository.java @@ -10,5 +10,4 @@ public interface ModuleRepository extends JpaRepository { Optional findByName(String name); - Optional findById(String name); } \ No newline at end of file