Finished and tested
This commit is contained in:
@@ -8,6 +8,12 @@ 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;
|
||||
|
||||
@@ -18,14 +24,25 @@ import java.util.List;
|
||||
@Produces({"application/json", "application/xml"})
|
||||
public class QuizzResource {
|
||||
public final QuizzMapper mapper = QuizzMapper.INSTANCE;
|
||||
public final QuizzDAO quizzDAO= new QuizzDAO();
|
||||
public final QuizzDAO quizzDAO = new QuizzDAO();
|
||||
public final QuestionDAO questionDAO = new QuestionDAO();
|
||||
|
||||
@GET
|
||||
@Path("/{quizz_id}")
|
||||
public Response getQuizzById(@PathParam("quizz_id") Integer quizzId) {
|
||||
@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) {
|
||||
if (quizz == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
QuizzDTO dto = mapper.toDTO(quizz);
|
||||
@@ -34,12 +51,23 @@ public class QuizzResource {
|
||||
|
||||
@GET
|
||||
@Path("/{quizz_id}/questions")
|
||||
public Response getQuestions(@PathParam("quizz_id") Integer quizzId) {
|
||||
@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<Question> questionList = quizz.getQuestions();
|
||||
List<QuestionDTO> dtos = QuestionMapper.INSTANCE.toDTOs(questionList);
|
||||
|
||||
return Response.status(Response.Status.OK).entity(dtos).build();
|
||||
@@ -47,27 +75,68 @@ public class QuizzResource {
|
||||
|
||||
@PUT
|
||||
@Path("/{quizz_id}/add_question/{question_id}")
|
||||
public Response addQuestion(@PathParam("quizz_id") Integer quizzId, @PathParam("question_id") Integer questionId) {
|
||||
@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.addQuestion(quizzId, questionId);
|
||||
quizzDAO.update(quizz);
|
||||
QuizzDTO quizzDTO = mapper.toDTO(quizz);
|
||||
return Response.status(Response.Status.OK).entity(quizzDTO).build();
|
||||
return Response.status(Response.Status.OK).entity(quizzDTO).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{quizz_id}/deleteQ")
|
||||
public Response deleteQuestion(@PathParam("quizz_id") Integer quizzId) {
|
||||
@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();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user