From 8874f386967b081b60fd3b995d07af1af4a926bd Mon Sep 17 00:00:00 2001 From: trochas Date: Wed, 15 Oct 2025 22:31:16 +0200 Subject: [PATCH] =?UTF-8?q?controller=20fini,=20non=20test=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/sample/data/jpa/metier/Choix.java | 15 ++ .../java/sample/data/jpa/metier/Reponse.java | 4 + .../sample/data/jpa/metier/ReponseCourte.java | 10 + .../sample/data/jpa/metier/Utilisateur.java | 4 +- .../sample/data/jpa/service/ReponseDao.java | 2 + .../sample/data/jpa/service/SessionDao.java | 3 +- .../data/jpa/service/UtilisateurDao.java | 2 +- .../data/jpa/web/QuestionController.java | 131 ++++--------- .../sample/data/jpa/web/QuizzController.java | 67 +++++-- .../data/jpa/web/ReponseController.java | 174 +++++++++++++++--- .../data/jpa/web/SessionController.java | 171 ++++++++++++++++- .../data/jpa/web/UtilisateurController.java | 138 +++++++++++++- 12 files changed, 576 insertions(+), 145 deletions(-) diff --git a/src/main/java/sample/data/jpa/metier/Choix.java b/src/main/java/sample/data/jpa/metier/Choix.java index 3ab5b06..d299a93 100644 --- a/src/main/java/sample/data/jpa/metier/Choix.java +++ b/src/main/java/sample/data/jpa/metier/Choix.java @@ -26,4 +26,19 @@ public class Choix extends Reponse{ } return res; } + + @Override + public String toString(){ + String res ="choix : ["; + for (int i = 0; i session= new ArrayList<>();; + private List session= new ArrayList<>(); @OneToMany(mappedBy = "utilisateur") - private List quizzs; + private List quizzs = new ArrayList<>(); } \ No newline at end of file diff --git a/src/main/java/sample/data/jpa/service/ReponseDao.java b/src/main/java/sample/data/jpa/service/ReponseDao.java index 63e08f1..76234a2 100644 --- a/src/main/java/sample/data/jpa/service/ReponseDao.java +++ b/src/main/java/sample/data/jpa/service/ReponseDao.java @@ -1,9 +1,11 @@ package sample.data.jpa.service; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.transaction.annotation.Transactional; import sample.data.jpa.metier.Reponse; +@Transactional public interface ReponseDao extends JpaRepository { } diff --git a/src/main/java/sample/data/jpa/service/SessionDao.java b/src/main/java/sample/data/jpa/service/SessionDao.java index b7dd1d9..4793fd3 100644 --- a/src/main/java/sample/data/jpa/service/SessionDao.java +++ b/src/main/java/sample/data/jpa/service/SessionDao.java @@ -2,8 +2,7 @@ package sample.data.jpa.service; import org.springframework.data.jpa.repository.JpaRepository; -import sample.data.jpa.domain.User; import sample.data.jpa.metier.Session; -public interface SessionDao extends JpaRepository { +public interface SessionDao extends JpaRepository { } diff --git a/src/main/java/sample/data/jpa/service/UtilisateurDao.java b/src/main/java/sample/data/jpa/service/UtilisateurDao.java index 769af88..b0c51c7 100644 --- a/src/main/java/sample/data/jpa/service/UtilisateurDao.java +++ b/src/main/java/sample/data/jpa/service/UtilisateurDao.java @@ -4,5 +4,5 @@ import org.springframework.data.jpa.repository.JpaRepository; import sample.data.jpa.metier.Utilisateur; -public interface UtilisateurDao extends JpaRepository { +public interface UtilisateurDao extends JpaRepository { } diff --git a/src/main/java/sample/data/jpa/web/QuestionController.java b/src/main/java/sample/data/jpa/web/QuestionController.java index adbd93e..5836a10 100644 --- a/src/main/java/sample/data/jpa/web/QuestionController.java +++ b/src/main/java/sample/data/jpa/web/QuestionController.java @@ -14,11 +14,10 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; -import sample.data.jpa.metier.Choix; import sample.data.jpa.metier.Question; import sample.data.jpa.metier.Reponse; -import sample.data.jpa.metier.ReponseCourte; import sample.data.jpa.service.QuestionDao; +import sample.data.jpa.service.ReponseDao; @@ -28,6 +27,18 @@ public class QuestionController { @Autowired private QuestionDao qDao; + @Autowired + private ReponseDao rDao; + + /* + Post /question/create param : body Jsp, "enonce" + Put /question/update/{id} param : body Jsp, "enonce" + Get /question/getAll + Get /question/getReponse/{id} + Put /question/removeReponse + Put /question/setReponse/{id}/{idReponse} + Delete /question/delete/{id} + */ /* * Utiliser un Json pour mettre : String enonce @@ -52,7 +63,7 @@ public class QuestionController { } /* - * Utiliser un Json pour mettre : String enonce + * Utiliser un Json pour mettre le String "enonce" * l'id se met dans l'url */ @PutMapping("/update/{id}") @@ -75,14 +86,13 @@ public class QuestionController { public String getAll(){ try { List questions = qDao.findAll(); - String res = "["; - for(int i = 0; i < questions.size(); i++){ - res+=questions.get(i).getId(); - if(i body) { - try { - Question question = qDao.findById(id).get(); - Reponse rep = question.getReponse(); - if(rep != null){ - String reponse = body.get("reponse"); - rep.getReponses().add(reponse); - qDao.save(question); - return "Réponse correcte \"" + reponse + "\" ajoutée à la question " + id; - } - else return "Reponse non itialisée pour la question : " + id; - } catch (Exception ex) { - return "Erreur lors de l'ajout de la réponse : " + ex.toString(); - } - } - @PutMapping("/deleteReponse/{id}") + @PutMapping("/removeReponse/{id}") @ResponseBody - public String deleteReponses(@PathVariable("id") int id, @RequestBody Map body) { + public String removeReponse(@PathVariable("id") int id) { try { Question question = qDao.findById(id).get(); - Reponse rep = question.getReponse(); - if(rep != null){ - String reponse = body.get("reponse"); - rep.getReponses().remove(reponse); - qDao.save(question); - return "Réponses \"" + reponse + "\" supprimées de la question " + id; - } - else return "Rien à supprimer, reponse non itialisée pour la question : " + id; - } catch (Exception ex) { - return "Erreur lors de la suppression des réponses : " + ex.toString(); - } - } - - @PutMapping("/setReponseChoix/{id}") - @ResponseBody - public String setReponseChoix(@PathVariable("id") int id) { - try { - Question question = qDao.findById(id).get(); - question.setReponse(new Choix()); + Reponse reponse = question.getReponse(); + reponse.setQuestion(null); + question.setReponse(null); qDao.save(question); - return "Réponses à choix multiple mises sur la question " + id; + rDao.save(reponse); + return "Reponses retiré de la question " + id; } catch (Exception ex) { - return "Erreur lors de la mise en place du type 'choix' : " + ex.toString(); + return "Erreur lors de la mise en place de la Reponse : " + ex.toString(); } } - @PutMapping("/setReponseCourte/{id}") + @PutMapping("/setReponse/{id}/{idR}") @ResponseBody - public String setReponseCourte(@PathVariable("id") int id) { + public String setReponse(@PathVariable("id") int id,@PathVariable("idR") int idR) { try { Question question = qDao.findById(id).get(); - question.setReponse(new ReponseCourte()); + Reponse reponse = rDao.findById(idR).get(); + question.setReponse(reponse); + reponse.setQuestion(question); qDao.save(question); - return "Réponses courtes mises sur la question " + id; + rDao.save(reponse); + return "Reponses "+idR+" mises sur la question " + id; } catch (Exception ex) { - return "Erreur lors de la mise en place du type 'réponse courte' : " + ex.toString(); + return "Erreur lors de la mise en place de la Reponse : " + ex.toString(); } } - @PutMapping("/addChoix/{id}") - @ResponseBody - public String addChoix(@PathVariable("id") int id, @RequestBody Map body) { - try { - Question question = qDao.findById(id).get(); - if (question.getReponse() instanceof Choix) { - String choix = body.get("choix"); - ((Choix) question.getReponse()).getChoix().add(choix); - qDao.save(question); - return "Choix \"" + choix + "\" ajouté à la question " + id; - } - else return "Erreur : la réponse doit être a choix multiple."; - } catch (Exception ex) { - return "Erreur lors de l'ajout du choix : " + ex.toString(); - } - } - - @GetMapping("/getChoix/{id}") - @ResponseBody - public String getChoix(@PathVariable("id") int id) { - try { - Question question = qDao.findById(id).get(); - if (question.getReponse() instanceof Choix) { - List choix = ((Choix) question.getReponse()).getChoix(); - String res = "["; - for(int i = 0; i < choix.size(); i++){ - res+=choix.get(i); - if(i quizzs = qDao.findAll(); for (Quizz quizz : quizzs) { - res+=quizz.getId() + " nbQuestion:" + quizz.getQuestions().size() + " \n"; + res+="id: " + quizz.getId() + " , "; + res+=" nbQuestion:" + quizz.getQuestions().size() + "]\n"; } } catch (Exception ex) { return "Error get all Quizz :" + ex.toString(); } - return "Quizz : \n" + res; + return "Quizzs : \n" + res; } + @GetMapping("/getQuestions/{id}") + @ResponseBody + public String getQuestion(@PathVariable("id") int id){ + String res = ""; + try {; + Quizz quizz = qDao.findById(id).get(); + for(Question question : quizz.getQuestions()){ + res+="id: " + question.getId() + " , "; + res+="enonce: \"" + question.getEnonce() + "\"\n"; + } + + } + catch (Exception ex) { + return "Error get question of the Quizz :" + ex.toString(); + } + return "Questions : \n" + res; + } + + } \ No newline at end of file diff --git a/src/main/java/sample/data/jpa/web/ReponseController.java b/src/main/java/sample/data/jpa/web/ReponseController.java index 93ea8a6..fbf65c3 100644 --- a/src/main/java/sample/data/jpa/web/ReponseController.java +++ b/src/main/java/sample/data/jpa/web/ReponseController.java @@ -1,63 +1,185 @@ package sample.data.jpa.web; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import sample.data.jpa.metier.Choix; -import sample.data.jpa.metier.Question; -import sample.data.jpa.metier.Quizz; import sample.data.jpa.metier.Reponse; import sample.data.jpa.metier.ReponseCourte; import sample.data.jpa.service.ReponseDao; @Controller -@RequestMapping("/RepCourte") +@RequestMapping("/reponse") public class ReponseController { + + @Autowired private ReponseDao rDao; - @RequestMapping("/createCourte") + /* + Post /reponse/create/choix + Post /reponse/create/courte + Get /reponse/getAll + Get /reponse/get/{id} + Delete /reponse/delete/{id} + Put /question/removeReponse/{id} param : body Jsp, "reponse" + Put /question/addReponse/{id} param : body Jsp, "reponse" + Put /reponse/addChoix/{id} param : body Jsp, "choix" + Put /reponse/removeChoix/{id} param : body Jsp, "choix" + + */ + + /* + * Créer une réponse de type Choix + */ + @PostMapping("/create/choix") @ResponseBody - public String createCourte() { - String rId = ""; + public String createReponseChoix() { try { - Reponse r = new ReponseCourte(); + Choix r = new Choix(); rDao.save(r); - rId = String.valueOf(r.getId()); + return "Réponses à choix multiple céée avec l'id : " + r.getId(); + } catch (Exception ex) { + return "Erreur lors de la création d'une réponse à choix multiple : " + ex.toString(); } - catch (Exception ex) { - return "Error creating the Quizz : " + ex.toString(); - } - return "Quizz succesfully created with id = " + rId; } - @RequestMapping("/createChoix") + /* + * Créer une réponse de type ReponseCourte + */ + @PostMapping("/create/courte") @ResponseBody - public String createChoix() { - String rId = ""; + public String createReponseCourte() { try { - Reponse r = new Choix(); + ReponseCourte r = new ReponseCourte(); rDao.save(r); - rId = String.valueOf(r.getId()); + return "Réponses courte céée "; + } catch (Exception ex) { + return "Erreur lors de la création d'une réponse courte : " + ex.toString(); } - catch (Exception ex) { - return "Error creating the Quizz : " + ex.toString(); - } - return "Quizz succesfully created with id = " + rId; } - @RequestMapping("/addReponse") + @GetMapping("/getAll") @ResponseBody - public String addReponse(int id, String reponse) { + public String getAll() { + try { + String res = ""; + List reponses = rDao.findAll(); + for (Reponse reponse : reponses) { + res+="id : " + reponse.getId() + " , " + reponse.toSring() + "\n"; + } + return res; + } catch (Exception ex) { + return "Erreur lors de la récupréation de toutes les réponses : " + ex.toString(); + } + } + + @GetMapping("/get/{id}") + @ResponseBody + public String get(@PathVariable("id") int id) { + try { + String res = ""; + Reponse reponse = rDao.findById(id).get(); + res+="id : " + reponse.getId() + " , " + reponse.toSring(); + return res; + } catch (Exception ex) { + return "Erreur lors de la récupréation de la réponse : "+id+" : " + ex.toString(); + } + } + + + @DeleteMapping("/delete/{id}") + @ResponseBody + public String delete(@PathVariable("id") int id) { + try { Reponse r = rDao.findById(id).get(); - r.getReponses().add(reponse); - rDao.save(r); - return "Réponse correcte \"" + reponse + "\" ajoutée " + id; + rDao.delete(r); + } + catch (Exception ex) { + return "Erreur pendant la suppression de la Reponse " + id + " :" + ex.toString(); + } + return "Reponse " + id + " supprimée avec succès"; + } + + + @PutMapping("/addReponse/{id}") + @ResponseBody + public String addReponse(@PathVariable("id") int id, @RequestBody Map body) { + try { + if(body.containsKey("reponse")){ + Reponse rep = rDao.findById(id).get(); + String reponse = body.get("reponse"); + rep.getReponses().add(reponse); + rDao.save(rep); + return "Réponse correcte \"" + reponse + "\" ajoutée à la Reponse " + id; + } else return "Erreur : mettre \"reponse\" dans le JSOIN"; } catch (Exception ex) { return "Erreur lors de l'ajout de la réponse : " + ex.toString(); } } + @PutMapping("/removeReponse/{id}") + @ResponseBody + public String removeReponses(@PathVariable("id") int id, @RequestBody Map body) { + try { + Reponse rep = rDao.findById(id).get(); + String reponse = body.get("reponse"); + rep.getReponses().remove(reponse); + rDao.save(rep); + return "Réponses correct \"" + reponse + "\" supprimée de la Reponse " + id; + } catch (Exception ex) { + return "Erreur lors de la suppression de la réponse : " + ex.toString(); + } + } + + + + @PutMapping("/addChoix/{id}") + @ResponseBody + public String addChoix(@PathVariable("id") int id, @RequestBody Map body) { + try { + Reponse reponse = rDao.findById(id).get(); + if (reponse instanceof Choix) { + String choix = body.get("choix"); + ((Choix) reponse).getChoix().add(choix); + rDao.save(reponse); + return "Choix \"" + choix + "\" ajouté à la question " + id; + } + else return "Erreur : la réponse doit être a choix multiple."; + + } catch (Exception ex) { + return "Erreur lors de l'ajout du choix : " + ex.toString(); + } + } + + + @PutMapping("/removeChoix/{id}") + @ResponseBody + public String removeChoix(@PathVariable("id") int id, @RequestBody Map body) { + try { + Reponse reponse = rDao.findById(id).get(); + if (reponse instanceof Choix) { + String choix = body.get("choix"); + ((Choix) reponse).getChoix().remove(choix); + rDao.save(reponse); + return "Choix \"" + choix + "\" supprimé à la question " + id; + } + else return "Erreur : la réponse doit être a choix multiple."; + + } catch (Exception ex) { + return "Erreur lors de la suppression du choix : " + ex.toString(); + } + } } \ No newline at end of file diff --git a/src/main/java/sample/data/jpa/web/SessionController.java b/src/main/java/sample/data/jpa/web/SessionController.java index cbc3b6b..c0839a6 100644 --- a/src/main/java/sample/data/jpa/web/SessionController.java +++ b/src/main/java/sample/data/jpa/web/SessionController.java @@ -1,16 +1,181 @@ package sample.data.jpa.web; +import java.util.List; +import java.util.Map; + import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RequestMapping; +import sample.data.jpa.metier.Quizz; +import sample.data.jpa.metier.Session; +import sample.data.jpa.metier.Utilisateur; +import sample.data.jpa.service.QuizzDao; import sample.data.jpa.service.SessionDao; +import sample.data.jpa.service.UtilisateurDao; @RequestMapping("/session") public class SessionController { - private SessionDao sessionDao; + + @Autowired + private SessionDao sDao; + @Autowired + private QuizzDao qDao; + @Autowired + private UtilisateurDao uDao; + + /* + Post /session/create param : body Jsp, "theme" + Delete /session/delete/{id} + Get /session/getAll + Put /session/addQuizz/{id} + Put /session/removeQuizz/{id} + Put /session/update/{id} param : body Jsp, "theme" + Put /session/addUtilisateur/{id}/{idU} + Put /session/removeUtilisateur/{id}/{idU} + */ + + + @PostMapping("/create") + @ResponseBody + public String create(@RequestBody Map body) { + String sId = ""; + Session q = new Session(); + try { + if(body.containsKey("theme")){ + q.setTheme(body.get("theme")); + } + else q.setTheme(""); + sDao.save(q); + sId = String.valueOf(q.getId()); + } + catch (Exception ex) { + return "Error creating the Session : " + ex.toString(); + } + return "Session \""+q.getTheme()+"\" succesfully created with id = " + sId; + } + + @DeleteMapping("/delete/{id}") + @ResponseBody + public String delete(@PathVariable("id") int id) { + + try { + Session q = sDao.findById(id).get(); + sDao.delete(q); + } + catch (Exception ex) { + return "Error deleting the session " + id + " :" + ex.toString(); + } + return "Session " + id + " succesfully deleted!"; + } + + @GetMapping("/getAll") + @ResponseBody + public String getAll(){ + try { + List sessions = sDao.findAll(); + String res = ""; + for(Session session : sessions){ + res+="id: " + session.getId() + " , "; + res+="theme: \"" + session.getTheme() + "\"\n"; + + } + + return res; + } catch (Exception ex) { + return "Error get all session : " + ex.toString(); + } + } + + @PutMapping("/addQuizz/{id}/{qid}") + @ResponseBody + public String addQuizz(@PathVariable("id") int id, @PathVariable("qid") int qId) { + try { + Session s = sDao.findById(id).get(); + Quizz q = qDao.findById(qId).get(); + s.getQuizzs().add(q); + q.setSession(s); + sDao.save(s); + qDao.save(q); + } catch (Exception ex) { + return "Error adding the quizz from the session : " + ex.toString(); + } + return "Quizz " + qId + " add in Session " + id; + } + + @PutMapping("/removeQuizz/{id}/{qid}") + @ResponseBody + public String removeReponse(@PathVariable("id") int id, @PathVariable("qid") int qId) { + try { + Session s = sDao.findById(id).get(); + Quizz q = qDao.findById(qId).get(); + s.getQuizzs().remove(q); + q.setSession(null); + sDao.save(s); + qDao.save(q); + } catch (Exception ex) { + return "Error removing the quizz from the session : " + ex.toString(); + } + return "Quizz " + qId + " remove from Session " + id; + } + + + /* + * Utiliser un Json pour mettre le String "theme" + * l'id se met dans l'url + */ + @PutMapping("/update/{id}") + @ResponseBody + public String updateQuestion(@PathVariable("id") int id, @RequestBody Map body) { + Session s; + try { + s = sDao.findById(id).get(); + s.setTheme(body.get("theme")); + sDao.save(s); + } + catch (Exception ex) { + return "Error updating the Session: " + ex.toString(); + } + return "Session "+id+" succesfully updated! : " + s.getTheme(); + } + + + @PutMapping("/addUtilisateur/{id}/{idU}") + @ResponseBody + public String addSession(@PathVariable("id") int id, @PathVariable("qid") int idU) { + try { + Utilisateur u = uDao.findById(idU).get(); + Session s = sDao.findById(id).get(); + u.getSession().add(s); + s.getUtilisateurs().add(u); + sDao.save(s); + uDao.save(u); + } catch (Exception ex) { + return "Erreur pendant l'ajout de l'utilisateur dans la session : " + ex.toString(); + } + return "Utilisateur " + id + " ajouté à la Session " + id; + } + + @PutMapping("/removeUtilisateur/{id}/{idU}") + @ResponseBody + public String removeSession(@PathVariable("id") int id, @PathVariable("idU") int idU) { + try { + Utilisateur u = uDao.findById(idU).get(); + Session s = sDao.findById(id).get(); + u.getSession().remove(s); + s.getUtilisateurs().remove(u); + sDao.save(s); + uDao.save(u); + } catch (Exception ex) { + return "Erreur pendant la suppression de l'utilisateur de la session : " + ex.toString(); + } + return "Utilisateur " + id + " retiré de la Session " + id; + } } \ No newline at end of file diff --git a/src/main/java/sample/data/jpa/web/UtilisateurController.java b/src/main/java/sample/data/jpa/web/UtilisateurController.java index 2c3a973..6db8bf6 100644 --- a/src/main/java/sample/data/jpa/web/UtilisateurController.java +++ b/src/main/java/sample/data/jpa/web/UtilisateurController.java @@ -1,15 +1,145 @@ package sample.data.jpa.web; -import org.springframework.stereotype.Controller; +import java.util.List; +import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; +import sample.data.jpa.metier.Quizz; +import sample.data.jpa.metier.Session; +import sample.data.jpa.metier.Utilisateur; +import sample.data.jpa.service.SessionDao; import sample.data.jpa.service.UtilisateurDao; -@RequestMapping("/session") +@RequestMapping("/utilisateur") public class UtilisateurController { - private UtilisateurDao utilisateurDao; + + @Autowired + private UtilisateurDao uDao; + @Autowired + private SessionDao sDao; + + + /* + Post /utilisateur/create param : body Jsp, "name", "email", "password" + Delete /utilisateur/delete/{id} + Get /utilisateur/getAll + Put /utilisateur/addSession/{idUtilisateur}/{idSession} + Put /utilisateur/removeSession/{idUtilisateur}/{idSession} + Put /utilisateur/update/{id} param : body Jsp, "name", "email", "oldpassword, "password" + */ + + + @PostMapping("/create") + @ResponseBody + public String create(@RequestBody Map body) { + try { + if(body.containsKey("name") && body.containsKey("email") && body.containsKey("password")){ + Utilisateur u = new Utilisateur(); + u.setName(body.get("name")); + u.setEmail(body.get("email")); + u.setPassword(body.get("password")); + uDao.save(u); + String uId = String.valueOf(u.getId()); + return "Utilisateur \""+u.getName()+"\" créé avec succès avec l'id = " + uId; + } + else return "Erreur, besoin de name, email et password dans le JSON"; + } + catch (Exception ex) { + return "Erreur durant la création de l'Utilisateur : " + ex.toString(); + } + } + + + @DeleteMapping("/delete/{id}") + @ResponseBody + public String delete(@PathVariable("id") int id) { + + try { + Utilisateur u = uDao.findById(id).get(); + uDao.delete(u); + } + catch (Exception ex) { + return "Erreur pendant la suppression de l'utilisateur " + id + " :" + ex.toString(); + } + return "Utilisateur " + id + " supprimé avec succès !"; + } + + @GetMapping("/getAll") + @ResponseBody + public String getAll(){ + try { + List utilisateurs = uDao.findAll(); + String res = ""; + for(Utilisateur utilisateur : utilisateurs){ + res+="id: " + utilisateur.getId() + " , "; + res+="name: " + utilisateur.getName() + " , "; + res+="email: \"" + utilisateur.getEmail() + "\"\n"; + } + + return res; + } catch (Exception ex) { + return "Error get all session : " + ex.toString(); + } + } + + @PutMapping("/addSession/{id}/{qid}") + @ResponseBody + public String addSession(@PathVariable("id") int id, @PathVariable("qid") int sId) { + try { + Utilisateur u = uDao.findById(id).get(); + Session s = sDao.findById(sId).get(); + u.getSession().add(s); + s.getUtilisateurs().add(u); + sDao.save(s); + uDao.save(u); + } catch (Exception ex) { + return "Erreur pendant l'ajout de l'utilisateur dans la session : " + ex.toString(); + } + return "Utilisateur " + id + " ajouté à la Session " + id; + } + + + @PutMapping("/removeSession/{id}/{qid}") + @ResponseBody + public String removeSession(@PathVariable("id") int id, @PathVariable("qid") int sId) { + try { + Utilisateur u = uDao.findById(id).get(); + Session s = sDao.findById(sId).get(); + u.getSession().remove(s); + s.getUtilisateurs().remove(u); + sDao.save(s); + uDao.save(u); + } catch (Exception ex) { + return "Erreur pendant la suppression de l'utilisateur de la session : " + ex.toString(); + } + return "Utilisateur " + id + " retiré de la Session " + id; + } + + + @PostMapping("/update/{id}") + @ResponseBody + public String update(@PathVariable("id") int id, @RequestBody Map body) { + try { + if(body.containsKey("name") && body.containsKey("email") && body.containsKey("password")){ + Utilisateur u = uDao.findById(id).get(); + u.setName(body.get("name")); + u.setEmail(body.get("email")); + u.setPassword(body.get("password")); + uDao.save(u); + return "Utilisateur \""+u.getName()+"\" mis à jour avec succès"; + } + else return "Erreur, besoin de name, email et password dans le JSON"; + } + catch (Exception ex) { + return "Erreur durant la mise à jour de de l'Utilisateur : " + ex.toString(); + } + } } \ No newline at end of file