143 lines
6.0 KiB
Java
143 lines
6.0 KiB
Java
package fr.istic.taa.jaxrs.rest;
|
|
|
|
import fr.istic.taa.jaxrs.DAO.QuestionDAO;
|
|
import fr.istic.taa.jaxrs.DAO.QuizzDAO;
|
|
import fr.istic.taa.jaxrs.DTO.QuestionDTO;
|
|
import fr.istic.taa.jaxrs.DTO.QuizzDTO;
|
|
import fr.istic.taa.jaxrs.Mapper.QuestionMapper;
|
|
import fr.istic.taa.jaxrs.Mapper.QuizzMapper;
|
|
import fr.istic.taa.jaxrs.metier.Question;
|
|
import fr.istic.taa.jaxrs.metier.Quizz;
|
|
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.*;
|
|
import jakarta.ws.rs.core.Response;
|
|
|
|
import java.util.List;
|
|
|
|
@Path("quizz")
|
|
@Consumes({"application/json", "application/xml"})
|
|
@Produces({"application/json", "application/xml"})
|
|
public class QuizzResource {
|
|
public final QuizzMapper mapper = QuizzMapper.INSTANCE;
|
|
public final QuizzDAO quizzDAO = new QuizzDAO();
|
|
public final QuestionDAO questionDAO = new QuestionDAO();
|
|
|
|
@GET
|
|
@Path("/{quizz_id}")
|
|
@Operation(summary = "Get a quizz by ID",
|
|
tags = {"Quizzes"},
|
|
description = "Get a quizz by ID",
|
|
responses = {
|
|
@ApiResponse(description = "The quizz by ID", content = @Content(
|
|
schema = @Schema(implementation = QuizzDTO.class))),
|
|
@ApiResponse(responseCode = "404", description = "Quizz not found")
|
|
}
|
|
)
|
|
public Response getQuizzById(
|
|
@Parameter(description = "ID of the quizz to fetch", required = true)
|
|
@PathParam("quizz_id") Integer quizzId) {
|
|
Quizz quizz = quizzDAO.findById(quizzId);
|
|
if (quizz == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
QuizzDTO dto = mapper.toDTO(quizz);
|
|
return Response.status(Response.Status.OK).entity(dto).build();
|
|
}
|
|
|
|
@GET
|
|
@Path("/{quizz_id}/questions")
|
|
@Operation(summary = "List all questions for a quizz",
|
|
tags = {"Quizzes"},
|
|
description = "List all questions by quizz ID.",
|
|
responses = {
|
|
@ApiResponse(description = "List of questions", content = @Content(
|
|
array = @ArraySchema(schema = @Schema(implementation = QuestionDTO.class)))),
|
|
@ApiResponse(responseCode = "404", description = "Quizz not found")
|
|
}
|
|
)
|
|
public Response getQuestions(
|
|
@Parameter(description = "ID of the quizz", required = true)
|
|
@PathParam("quizz_id") Integer quizzId) {
|
|
Quizz quizz = quizzDAO.findById(quizzId);
|
|
if (quizz == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
List<Question> questionList = quizz.getQuestions();
|
|
List<QuestionDTO> dtos = QuestionMapper.INSTANCE.toDTOs(questionList);
|
|
|
|
return Response.status(Response.Status.OK).entity(dtos).build();
|
|
}
|
|
|
|
@PUT
|
|
@Path("/{quizz_id}/add_question/{question_id}")
|
|
@Operation(summary = "Add a question to a quizz",
|
|
tags = {"Quizzes"},
|
|
description = "Add question to quizz by quizz ID and question ID.",
|
|
responses = {
|
|
@ApiResponse(responseCode = "200", description = "Question added successfully",
|
|
content = @Content(schema = @Schema(implementation = QuizzDTO.class))),
|
|
@ApiResponse(responseCode = "404", description = "Quizz or Question not found")
|
|
}
|
|
)
|
|
public Response addQuestion(
|
|
@Parameter(description = "ID of the quizz to update", required = true) @PathParam("quizz_id") Integer quizzId,
|
|
@Parameter(description = "ID of the question to add", required = true) @PathParam("question_id") Integer questionId) {
|
|
Quizz quizz = quizzDAO.findById(quizzId);
|
|
Question question = questionDAO.findById(questionId);
|
|
if (quizz == null || question == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
quizzDAO.addQuestion(quizzId, questionId);
|
|
quizzDAO.update(quizz);
|
|
QuizzDTO quizzDTO = mapper.toDTO(quizz);
|
|
return Response.status(Response.Status.OK).entity(quizzDTO).build();
|
|
}
|
|
|
|
@PUT
|
|
@Path("/{quizz_id}/deleteQ")
|
|
@Operation(summary = "Remove all questions from a quizz",
|
|
tags = {"Quizzes"},
|
|
description = "Removes all questions by ID of quizz.",
|
|
responses = {
|
|
@ApiResponse(responseCode = "200", description = "Removed all questions"),
|
|
@ApiResponse(responseCode = "404", description = "Quizz not found")}
|
|
)
|
|
public Response deleteQuestion(
|
|
@Parameter(description = "ID of the quizz", required = true)
|
|
@PathParam("quizz_id") Integer quizzId) {
|
|
Quizz quizz = quizzDAO.findById(quizzId);
|
|
if (quizz == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
quizzDAO.deleteAllQustion(quizzId);
|
|
quizzDAO.update(quizz);
|
|
return Response.status(Response.Status.OK).build();
|
|
}
|
|
|
|
@DELETE
|
|
@Path("/{quizz_id}/delete")
|
|
@Operation(summary = "Delete a Quizz",
|
|
tags = {"Quizzes"},
|
|
description = "Deletes a Quizz by ID.",
|
|
responses = {
|
|
@ApiResponse(responseCode = "200", description = "Quizz deleted."),
|
|
@ApiResponse(responseCode = "404", description = "Quizz not found.")
|
|
}
|
|
)
|
|
public Response deleteQuizz(
|
|
@Parameter(description = "ID of the Quizz to delete", required = true)
|
|
@PathParam("quizz_id") Integer quizzId) {
|
|
Quizz quizz = quizzDAO.findById(quizzId);
|
|
if (quizz == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
quizzDAO.delete(quizz);
|
|
return Response.status(Response.Status.OK).build();
|
|
}
|
|
}
|