controller fini, non testé
This commit is contained in:
@@ -26,4 +26,19 @@ public class Choix extends Reponse{
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
String res ="choix : [";
|
||||
for (int i = 0; i<this.choix.size(); i++) {
|
||||
res += this.choix.get(i);
|
||||
if(i<this.choix.size()-1) res+= ", ";
|
||||
}
|
||||
res += "] , reponses : [";
|
||||
for (int i = 0; i<this.reponses.size(); i++) {
|
||||
res += this.reponses.get(i);
|
||||
if(i<this.reponses.size()-1) res+= ", ";
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,4 +28,8 @@ public abstract class Reponse implements Serializable {
|
||||
public String valHTML(){
|
||||
return "";
|
||||
}
|
||||
|
||||
public String toSring(){
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -17,4 +17,14 @@ public class ReponseCourte extends Reponse{
|
||||
public String valHTML(){
|
||||
return "INPUT";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
String res = "INPUT , reponses : [";
|
||||
for (int i = 0; i<this.reponses.size(); i++) {
|
||||
res += this.reponses.get(i);
|
||||
if(i<this.reponses.size()-1) res+= ", ";
|
||||
}
|
||||
return res+"]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ public class Utilisateur implements Serializable {
|
||||
joinColumns = @JoinColumn(name="utilisateur_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "session_id")
|
||||
)
|
||||
private List<Session> session= new ArrayList<>();;
|
||||
private List<Session> session= new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy = "utilisateur")
|
||||
private List<Quizz> quizzs;
|
||||
private List<Quizz> quizzs = new ArrayList<>();
|
||||
}
|
||||
@@ -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<Reponse, Integer> {
|
||||
|
||||
}
|
||||
|
||||
@@ -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<Session, Long> {
|
||||
public interface SessionDao extends JpaRepository<Session, Integer> {
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import sample.data.jpa.metier.Utilisateur;
|
||||
|
||||
public interface UtilisateurDao extends JpaRepository<Utilisateur, Long> {
|
||||
public interface UtilisateurDao extends JpaRepository<Utilisateur, Integer> {
|
||||
}
|
||||
|
||||
@@ -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<Question> questions = qDao.findAll();
|
||||
String res = "[";
|
||||
for(int i = 0; i < questions.size(); i++){
|
||||
res+=questions.get(i).getId();
|
||||
if(i<questions.size()-1){
|
||||
res+=",";
|
||||
}
|
||||
String res = "";
|
||||
for(Question question : questions){
|
||||
res+="id: " + question.getId() + " , ";
|
||||
res+="enonce: \"" + question.getEnonce() + "\"\n";
|
||||
|
||||
}
|
||||
res+="]";
|
||||
|
||||
return res;
|
||||
} catch (Exception ex) {
|
||||
return "Erreur lors de la récupération des question : " + ex.toString();
|
||||
@@ -115,109 +125,40 @@ public class QuestionController {
|
||||
}
|
||||
}
|
||||
|
||||
@PutMapping("/addReponse/{id}")
|
||||
@ResponseBody
|
||||
public String addReponse(@PathVariable("id") int id, @RequestBody Map<String, String> 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<String, String> 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<String, String> 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<String> choix = ((Choix) question.getReponse()).getChoix();
|
||||
String res = "[";
|
||||
for(int i = 0; i < choix.size(); i++){
|
||||
res+=choix.get(i);
|
||||
if(i<choix.size()-1){
|
||||
res+=",";
|
||||
}
|
||||
}
|
||||
res+="]";
|
||||
return "Choix de la question " + id + " : " + res;
|
||||
}
|
||||
else return "Erreur : la question "+id+" n'est pas a choix multiple.";
|
||||
|
||||
} catch (Exception ex) {
|
||||
return "Erreur lors de l'ajout du choix : " + ex.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@ResponseBody
|
||||
|
||||
@@ -2,8 +2,10 @@ package sample.data.jpa.web;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
@@ -12,12 +14,27 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import sample.data.jpa.metier.Question;
|
||||
import sample.data.jpa.metier.Quizz;
|
||||
import sample.data.jpa.service.QuestionDao;
|
||||
import sample.data.jpa.service.QuizzDao;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/quizz")
|
||||
public class QuizzController {
|
||||
|
||||
@Autowired
|
||||
private QuizzDao qDao;
|
||||
@Autowired
|
||||
private QuestionDao qtDao;
|
||||
|
||||
|
||||
/*
|
||||
Post /quizz/create
|
||||
Delete /quizz/delete/{id}
|
||||
Put /quizz/addQuestion/{idQuestion}/{idQuizz}
|
||||
Put /quizz/removeQuestion/{idQuestin}/{idQuizz}
|
||||
Get /quizz/getAll
|
||||
Get /quizz/getQuestions/{id}
|
||||
*/
|
||||
|
||||
|
||||
@PostMapping("/create")
|
||||
@@ -48,26 +65,32 @@ public class QuizzController {
|
||||
return "Quizz " + id + " succesfully deleted!";
|
||||
}
|
||||
|
||||
@PutMapping("/addQuestion/{id}")
|
||||
|
||||
@PutMapping("/addQuestion/{id}/{idQ}")
|
||||
@ResponseBody
|
||||
public String delete(@PathVariable("id") int id, Question question) {
|
||||
public String addQuestion(@PathVariable("id") int id, @PathVariable("idQ") int qId) {
|
||||
try {
|
||||
Quizz q = qDao.findById(id).get();
|
||||
q.getQuestions().add(question);
|
||||
Question qt = qtDao.findById(qId).get();
|
||||
q.getQuestions().add(qt);
|
||||
qt.setQuizz(q);
|
||||
qDao.save(q);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return "Error Question adding from quizz " + id + " :" + ex.toString();
|
||||
return "Error adding question from the quizz " + id + " :" + ex.toString();
|
||||
}
|
||||
return "Question add from Quizz " + id;
|
||||
return "Question " + qId + " add in Quizz " + id;
|
||||
}
|
||||
|
||||
@PutMapping("/deletQuestion/{id}")
|
||||
|
||||
@PutMapping("/removeQuestion/{id}/{idQ}")
|
||||
@ResponseBody
|
||||
public String delete(@PathVariable("id") int id, int qId) {
|
||||
public String removeQuestion(@PathVariable("id") int id, @PathVariable("idQ") int qId) {
|
||||
try {
|
||||
Quizz q = qDao.findById(id).get();
|
||||
q.getQuestions().remove(qId);
|
||||
Question qt = qtDao.findById(qId).get();
|
||||
q.getQuestions().remove(qt);
|
||||
qt.setQuizz(null);
|
||||
qDao.save(q);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
@@ -76,22 +99,42 @@ public class QuizzController {
|
||||
return "Question remove from Quizz " + id;
|
||||
}
|
||||
|
||||
@PutMapping("/getAll")
|
||||
|
||||
@GetMapping("/getAll")
|
||||
@ResponseBody
|
||||
public String getAll(){
|
||||
String res = "";
|
||||
|
||||
try {
|
||||
List<Quizz> 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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<Reponse> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<String, String> 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<Session> 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<String, String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String, String> 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<Utilisateur> 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<String, String> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user