223 lines
9.4 KiB
Java
223 lines
9.4 KiB
Java
package fr.istic.taa.jaxrs.rest;
|
|
|
|
import java.util.List;
|
|
|
|
import fr.istic.taa.jaxrs.DAO.QuestionDAO;
|
|
import fr.istic.taa.jaxrs.DTO.QuestionDTO;
|
|
import fr.istic.taa.jaxrs.Mapper.QuestionMapper;
|
|
import fr.istic.taa.jaxrs.metier.Choix;
|
|
import fr.istic.taa.jaxrs.metier.Question;
|
|
import fr.istic.taa.jaxrs.metier.Reponse;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
import io.swagger.v3.oas.annotations.media.ArraySchema;
|
|
import io.swagger.v3.oas.annotations.media.Content;
|
|
import io.swagger.v3.oas.annotations.media.Schema;
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
import jakarta.ws.rs.Consumes;
|
|
import jakarta.ws.rs.GET;
|
|
import jakarta.ws.rs.PUT;
|
|
import jakarta.ws.rs.POST;
|
|
import jakarta.ws.rs.DELETE;
|
|
import jakarta.ws.rs.Path;
|
|
import jakarta.ws.rs.PathParam;
|
|
import jakarta.ws.rs.Produces;
|
|
import jakarta.ws.rs.core.Response;
|
|
|
|
|
|
@Path("question")
|
|
@Consumes({"application/json", "application/xml"})
|
|
@Produces({"application/json", "application/xml"})
|
|
public class QuestionResource {
|
|
private final QuestionDAO questionDAO = new QuestionDAO();
|
|
private final QuestionMapper mapper = QuestionMapper.INSTANCE;
|
|
|
|
@GET
|
|
@Operation(summary = "List all questions",
|
|
tags = {"Questions"},
|
|
description = "List all questions.",
|
|
responses = {
|
|
@ApiResponse(description = "List of all questions", content = @Content(
|
|
array = @ArraySchema(schema = @Schema(implementation = Question.class))))
|
|
}
|
|
)
|
|
public List<QuestionDTO> listQuestion() {
|
|
List<Question> questions = questionDAO.findAll();
|
|
return mapper.toDTOs(questions);
|
|
}
|
|
|
|
@POST
|
|
@Path("/addQuestion")
|
|
@Operation(summary = "Create a new question",
|
|
tags = {"Questions"},
|
|
description = "Create a new question",
|
|
responses = {@ApiResponse(responseCode = "201", description = "Question added.")}
|
|
)
|
|
public Response addQuestion(
|
|
@Parameter(description = "The question details to be added", required = true) QuestionDTO dto) {
|
|
Question question = mapper.toEntity(dto);
|
|
questionDAO.create(question);
|
|
return Response.status(Response.Status.CREATED).entity("Question ajouté avec Succès : \"" + dto.getQuestion() + "\"").build();
|
|
}
|
|
|
|
@PUT
|
|
@Path("/{question_id}/changeQuestion/")
|
|
@Operation(summary = "Update the text of a question",
|
|
tags = {"Questions"},
|
|
description = "Update the text of a question.",
|
|
responses = {
|
|
@ApiResponse(responseCode = "201", description = "Question answer updated."),
|
|
@ApiResponse(responseCode = "404", description = "Question not found")
|
|
}
|
|
)
|
|
public Response changeQuestion(
|
|
@Parameter(description = "ID of the question to modify", required = true) @PathParam("question_id") Integer id,
|
|
@Parameter(description = "New answer for the question", required = true) String newQuestion) {
|
|
Question question = questionDAO.findById(id);
|
|
if (question == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
questionDAO.changeEnonce(newQuestion, id);
|
|
return Response.status(Response.Status.CREATED).entity("Enonce de la question mis à jour : " + newQuestion).build();
|
|
|
|
}
|
|
|
|
@GET
|
|
@Path("/{question_id}/getReponse/")
|
|
@Operation(summary = "Get the correct answer for question",
|
|
tags = {"Questions"},
|
|
description = "Get the correct answer for question by ID of question.",
|
|
responses = {
|
|
@ApiResponse(description = "The correct answer", content = @Content(
|
|
schema = @Schema(implementation = Reponse.class))),
|
|
@ApiResponse(responseCode = "404", description = "Question not found")
|
|
}
|
|
)
|
|
public Response getReponse(
|
|
@Parameter(description = "ID of the question", required = true)
|
|
@PathParam("question_id") Integer id) {
|
|
|
|
Question question = questionDAO.findById(id);
|
|
if (question == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
Reponse result = question.getReponse();
|
|
return Response.ok(result).build();
|
|
|
|
}
|
|
|
|
@PUT
|
|
@Path("/{question_id}/addReponse/")
|
|
@Operation(summary = "Add/Update the correct answer for question",
|
|
tags = {"Questions"},
|
|
description = "Add/Update the correct answer for question",
|
|
responses = {
|
|
@ApiResponse(responseCode = "201", description = "Answer text updated."),
|
|
@ApiResponse(responseCode = "404", description = "Question not found")
|
|
}
|
|
)
|
|
public Response addReponse(
|
|
@Parameter(description = "ID of the question", required = true) @PathParam("question_id") Integer id,
|
|
@Parameter(description = "The correct answer", required = true) String reponse) {
|
|
|
|
Question question = questionDAO.findById(id);
|
|
if (question == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
questionDAO.addReponse(reponse, id);
|
|
return Response.status(Response.Status.CREATED).entity("Reponse correct \"" + reponse + "\" ajouté à la question " + id).build();
|
|
|
|
}
|
|
|
|
@DELETE
|
|
@Path("/{question_id}/deletReponses/")
|
|
@Operation(summary = "Removes the correct response.",
|
|
tags = {"Questions"},
|
|
description = "Removes the correct response.",
|
|
responses = {
|
|
@ApiResponse(responseCode = "201", description = "Response deleted."),
|
|
@ApiResponse(responseCode = "404", description = "Question not found")
|
|
}
|
|
)
|
|
public Response deleteReponses(
|
|
@Parameter(description = "ID of the question", required = true)
|
|
@PathParam("question_id") Integer id) {
|
|
Question question = questionDAO.findById(id);
|
|
if (question == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
questionDAO.deleteReponse(id);
|
|
return Response.status(Response.Status.CREATED).entity("Reponses supprimé de la question " + id).build();
|
|
|
|
}
|
|
|
|
@PUT
|
|
@Path("/{question_id}/setReponse/choix")
|
|
@Operation(summary = "Set question type to multiple choice",
|
|
tags = {"Questions"},
|
|
description = "Set question type to multiple choice type. It could override old answers",
|
|
responses = {
|
|
@ApiResponse(responseCode = "201", description = "Question type set to multiple choice."),
|
|
@ApiResponse(responseCode = "404", description = "Question not found")
|
|
}
|
|
)
|
|
public Response setChoix(
|
|
@Parameter(description = "ID of the question", required = true)
|
|
@PathParam("question_id") Integer id) {
|
|
Question question = questionDAO.findById(id);
|
|
if (question == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
questionDAO.setChoix(id);
|
|
return Response.status(Response.Status.CREATED).entity("Reponses à choix multiple mise sur la question " + id).build();
|
|
}
|
|
|
|
@PUT
|
|
@Path("/{question_id}/setReponse/reponseCourte")
|
|
@Operation(summary = "Set question type to short answer",
|
|
tags = {"Questions"},
|
|
description = "Set question type to multiple short answer. It could override old answers",
|
|
responses = {
|
|
@ApiResponse(responseCode = "201", description = "Question type set to short answer."),
|
|
@ApiResponse(responseCode = "404", description = "Question not found")
|
|
}
|
|
)
|
|
public Response setReponseCourte(
|
|
@Parameter(description = "ID of the question", required = true)
|
|
@PathParam("question_id") Integer id) {
|
|
Question question = questionDAO.findById(id);
|
|
if (question == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
questionDAO.setReponseCourte(id);
|
|
return Response.status(Response.Status.CREATED).entity("Reponses courte mise sur la question " + id).build();
|
|
|
|
}
|
|
|
|
@PUT
|
|
@Path("/{question_id}/AddChoix")
|
|
@Operation(summary = "Add a choice option to a multiple-choice question",
|
|
tags = {"Questions"},
|
|
description = "Adds a new choice option to the question (Type Multiple Choice obligatoire)",
|
|
responses = {
|
|
@ApiResponse(responseCode = "201", description = "Choice added."),
|
|
@ApiResponse(responseCode = "404", description = "Question not found"),
|
|
@ApiResponse(responseCode = "400", description = "Wrong type")
|
|
}
|
|
)
|
|
public Response addChoix(
|
|
@Parameter(description = "ID of the question", required = true) @PathParam("question_id") Integer id,
|
|
@Parameter(description = "The choice", required = true) String choix){
|
|
Question question = questionDAO.findById(id);
|
|
if (question == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
if (question.getReponse() instanceof Choix) {
|
|
return Response.status(Response.Status.EXPECTATION_FAILED).build();
|
|
}
|
|
questionDAO.addChoix(id, choix);
|
|
return Response.status(Response.Status.CREATED).entity("Reponses courte mise sur la question " + id).build();
|
|
|
|
}
|
|
}
|