Controller question fini et testé avec postman
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package sample.data.jpa.metier;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -14,7 +15,7 @@ import lombok.Setter;
|
||||
@NoArgsConstructor
|
||||
@PrimaryKeyJoinColumn(name = "Choix_Id")
|
||||
public class Choix extends Reponse{
|
||||
List<String> choix;
|
||||
List<String> choix = new ArrayList<String>();;
|
||||
|
||||
@Override
|
||||
public String valHTML(){
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sample.data.jpa.metier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
@@ -22,7 +23,7 @@ public abstract class Reponse implements Serializable {
|
||||
|
||||
@OneToOne
|
||||
private Question question;
|
||||
public List<String> reponses;
|
||||
public List<String> reponses = new ArrayList<String>();
|
||||
|
||||
public String valHTML(){
|
||||
return "";
|
||||
|
||||
@@ -5,6 +5,8 @@ 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;
|
||||
@@ -14,6 +16,7 @@ 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;
|
||||
|
||||
@@ -67,57 +70,90 @@ public class QuestionController {
|
||||
return "Question "+id+" succesfully updated! : " + q.getEnonce();
|
||||
}
|
||||
|
||||
@RequestMapping("/getReponse")
|
||||
@GetMapping("/getAll")
|
||||
@ResponseBody
|
||||
public String getReponse(int id) {
|
||||
public String getAll(){
|
||||
try {
|
||||
Question question = qDao.findById(id).get();
|
||||
List<String> results = question.getReponse().getReponses();
|
||||
List<Question> questions = qDao.findAll();
|
||||
String res = "[";
|
||||
for(int i = 0; i < results.size(); i++){
|
||||
res+=results.get(i);
|
||||
if(i<results.size()-1){
|
||||
for(int i = 0; i < questions.size(); i++){
|
||||
res+=questions.get(i).getId();
|
||||
if(i<questions.size()-1){
|
||||
res+=",";
|
||||
}
|
||||
else res+="]";
|
||||
}
|
||||
return (results != null)
|
||||
? "Réponse de la question " + id + " : " + res
|
||||
: "Aucune réponse trouvée pour la question " + id;
|
||||
res+="]";
|
||||
return res;
|
||||
} catch (Exception ex) {
|
||||
return "Erreur lors de la récupération des question : " + ex.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/getReponses/{id}")
|
||||
@ResponseBody
|
||||
public String getReponses(@PathVariable("id") int id) {
|
||||
try {
|
||||
Question question = qDao.findById(id).get();
|
||||
Reponse rep = question.getReponse();
|
||||
if(rep != null){
|
||||
List<String> reponses = rep.getReponses();
|
||||
String res = "[";
|
||||
for(int i = 0; i < reponses.size(); i++){
|
||||
res+=reponses.get(i);
|
||||
if(i<reponses.size()-1){
|
||||
res+=",";
|
||||
}
|
||||
}
|
||||
res+="]";
|
||||
return "Réponse de la question " + id + " : " + res;
|
||||
}
|
||||
else return "Réponse non initialisée pour la question : " + id;
|
||||
} catch (Exception ex) {
|
||||
return "Erreur lors de la récupération de la réponse : " + ex.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/addReponse")
|
||||
@PutMapping("/addReponse/{id}")
|
||||
@ResponseBody
|
||||
public String addReponse(int id, String reponse) {
|
||||
public String addReponse(@PathVariable("id") int id, @RequestBody Map<String, String> body) {
|
||||
try {
|
||||
Question question = qDao.findById(id).get();
|
||||
question.getReponse().getReponses().add(reponse);
|
||||
qDao.save(question);
|
||||
return "Réponse correcte \"" + reponse + "\" ajoutée à la question " + id;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/deleteReponse")
|
||||
@PutMapping("/deleteReponse/{id}")
|
||||
@ResponseBody
|
||||
public String deleteReponses(int id, String reponse) {
|
||||
public String deleteReponses(@PathVariable("id") int id, @RequestBody Map<String, String> body) {
|
||||
try {
|
||||
Question question = qDao.findById(id).get();
|
||||
question.getReponse().getReponses().remove(reponse);
|
||||
qDao.save(question);
|
||||
return "Réponses supprimées de la question " + id;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/setReponseChoix")
|
||||
@PutMapping("/setReponseChoix/{id}")
|
||||
@ResponseBody
|
||||
public String setReponseChoix(int id) {
|
||||
public String setReponseChoix(@PathVariable("id") int id) {
|
||||
try {
|
||||
Question question = qDao.findById(id).get();
|
||||
question.setReponse(new Choix());
|
||||
@@ -128,9 +164,9 @@ public class QuestionController {
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/setReponseCourte")
|
||||
@PutMapping("/setReponseCourte/{id}")
|
||||
@ResponseBody
|
||||
public String setReponseCourte(int id) {
|
||||
public String setReponseCourte(@PathVariable("id") int id) {
|
||||
try {
|
||||
Question question = qDao.findById(id).get();
|
||||
question.setReponse(new ReponseCourte());
|
||||
@@ -141,27 +177,51 @@ public class QuestionController {
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/updateChoix")
|
||||
@PutMapping("/addChoix/{id}")
|
||||
@ResponseBody
|
||||
public String addChoix(int id, String choix) {
|
||||
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.";
|
||||
|
||||
|
||||
return "Choix \"" + choix + "\" ajouté à la question " + id;
|
||||
} catch (Exception ex) {
|
||||
return "Erreur lors de l'ajout du choix : " + ex.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/delete")
|
||||
@GetMapping("/getChoix/{id}")
|
||||
@ResponseBody
|
||||
public String delete(int id) {
|
||||
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
|
||||
public String delete(@PathVariable("id") int id) {
|
||||
|
||||
try {
|
||||
Question q = qDao.findById(id).get();
|
||||
|
||||
@@ -3,6 +3,10 @@ package sample.data.jpa.web;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@@ -16,7 +20,7 @@ public class QuizzController {
|
||||
private QuizzDao qDao;
|
||||
|
||||
|
||||
@RequestMapping("/create")
|
||||
@PostMapping("/create")
|
||||
@ResponseBody
|
||||
public String create() {
|
||||
String qId = "";
|
||||
@@ -31,9 +35,9 @@ public class QuizzController {
|
||||
return "Quizz succesfully created with id = " + qId;
|
||||
}
|
||||
|
||||
@RequestMapping("/delete")
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@ResponseBody
|
||||
public String delete(int id) {
|
||||
public String delete(@PathVariable("id") int id) {
|
||||
try {
|
||||
Quizz q = qDao.findById(id).get();
|
||||
qDao.delete(q);
|
||||
@@ -44,9 +48,9 @@ public class QuizzController {
|
||||
return "Quizz " + id + " succesfully deleted!";
|
||||
}
|
||||
|
||||
@RequestMapping("/addQuestion")
|
||||
@PutMapping("/addQuestion/{id}")
|
||||
@ResponseBody
|
||||
public String delete(int id, Question question) {
|
||||
public String delete(@PathVariable("id") int id, Question question) {
|
||||
try {
|
||||
Quizz q = qDao.findById(id).get();
|
||||
q.getQuestions().add(question);
|
||||
@@ -58,9 +62,9 @@ public class QuizzController {
|
||||
return "Question add from Quizz " + id;
|
||||
}
|
||||
|
||||
@RequestMapping("/deletQuestion")
|
||||
@PutMapping("/deletQuestion/{id}")
|
||||
@ResponseBody
|
||||
public String delete(int id, int qId) {
|
||||
public String delete(@PathVariable("id") int id, int qId) {
|
||||
try {
|
||||
Quizz q = qDao.findById(id).get();
|
||||
q.getQuestions().remove(qId);
|
||||
@@ -72,7 +76,7 @@ public class QuizzController {
|
||||
return "Question remove from Quizz " + id;
|
||||
}
|
||||
|
||||
@RequestMapping("/getAll")
|
||||
@PutMapping("/getAll")
|
||||
@ResponseBody
|
||||
public String getAll(){
|
||||
String res = "";
|
||||
|
||||
Reference in New Issue
Block a user