97 lines
2.8 KiB
Java
97 lines
2.8 KiB
Java
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;
|
|
|
|
import sample.data.jpa.metier.Question;
|
|
import sample.data.jpa.metier.Quizz;
|
|
import sample.data.jpa.service.QuizzDao;
|
|
|
|
@Controller
|
|
@RequestMapping("/quizz")
|
|
public class QuizzController {
|
|
private QuizzDao qDao;
|
|
|
|
|
|
@PostMapping("/create")
|
|
@ResponseBody
|
|
public String create() {
|
|
String qId = "";
|
|
try {
|
|
Quizz q = new Quizz();
|
|
qDao.save(q);
|
|
qId = String.valueOf(q.getId());
|
|
}
|
|
catch (Exception ex) {
|
|
return "Error creating the Quizz : " + ex.toString();
|
|
}
|
|
return "Quizz succesfully created with id = " + qId;
|
|
}
|
|
|
|
@DeleteMapping("/delete/{id}")
|
|
@ResponseBody
|
|
public String delete(@PathVariable("id") int id) {
|
|
try {
|
|
Quizz q = qDao.findById(id).get();
|
|
qDao.delete(q);
|
|
}
|
|
catch (Exception ex) {
|
|
return "Error deleting the quizz " + id + " :" + ex.toString();
|
|
}
|
|
return "Quizz " + id + " succesfully deleted!";
|
|
}
|
|
|
|
@PutMapping("/addQuestion/{id}")
|
|
@ResponseBody
|
|
public String delete(@PathVariable("id") int id, Question question) {
|
|
try {
|
|
Quizz q = qDao.findById(id).get();
|
|
q.getQuestions().add(question);
|
|
qDao.save(q);
|
|
}
|
|
catch (Exception ex) {
|
|
return "Error Question adding from quizz " + id + " :" + ex.toString();
|
|
}
|
|
return "Question add from Quizz " + id;
|
|
}
|
|
|
|
@PutMapping("/deletQuestion/{id}")
|
|
@ResponseBody
|
|
public String delete(@PathVariable("id") int id, int qId) {
|
|
try {
|
|
Quizz q = qDao.findById(id).get();
|
|
q.getQuestions().remove(qId);
|
|
qDao.save(q);
|
|
}
|
|
catch (Exception ex) {
|
|
return "Error removing question from the quizz " + id + " :" + ex.toString();
|
|
}
|
|
return "Question remove from Quizz " + id;
|
|
}
|
|
|
|
@PutMapping("/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";
|
|
}
|
|
}
|
|
catch (Exception ex) {
|
|
return "Error get all Quizz :" + ex.toString();
|
|
}
|
|
return "Quizz : \n" + res;
|
|
}
|
|
|
|
|
|
} |