136 lines
5.4 KiB
Go
136 lines
5.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
|
|
"Rezina-Backend/internal/domain"
|
|
"Rezina-Backend/internal/services/examprep"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// CreateChapterQuiz godoc
|
|
// @Summary Create a chapter quiz
|
|
// @Description First create a question set with set_type QUIZ. The chapter URL determines placement; omit all parent fields. Defaults to DRAFT. Learners require both the quiz and its question set to be published.
|
|
// @Tags quizzes
|
|
// @Security Bearer
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param chapterId path int true "Chapter ID"
|
|
// @Param body body createChapterQuizRequest true "Quiz shell"
|
|
// @Success 201 {object} domain.Response{data=domain.ExamPrepPractice}
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 401 {object} domain.ErrorResponse
|
|
// @Failure 403 {object} domain.ErrorResponse
|
|
// @Failure 404 {object} domain.ErrorResponse
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/admin/chapters/{chapterId}/quizzes [post]
|
|
func (h *Handler) CreateChapterQuiz(c *fiber.Ctx) error {
|
|
chapterID, err := parseAssessmentID(c.Params("chapterId"))
|
|
if err != nil {
|
|
return fiber.NewError(400, "Invalid chapter ID")
|
|
}
|
|
var input domain.CreateExamPrepPracticeInput
|
|
if err = c.BodyParser(&input); err != nil {
|
|
return fiber.NewError(400, "Invalid request body")
|
|
}
|
|
if errs, ok := h.validator.Validate(c, input); !ok {
|
|
return fiber.NewError(400, firstValidationError(errs))
|
|
}
|
|
quiz, err := h.examPrepSvc.CreateChapterQuiz(c.Context(), chapterID, input)
|
|
if err != nil {
|
|
if errors.Is(err, examprep.ErrExamPrepQuizRequiresQuizSet) {
|
|
return fiber.NewError(400, err.Error())
|
|
}
|
|
switch {
|
|
case errors.Is(err, examprep.ErrInvalidExamPrepPracticeParent):
|
|
return fiber.NewError(400, err.Error())
|
|
case errors.Is(err, examprep.ErrUnitNotFound), errors.Is(err, examprep.ErrQuestionSetNotFound), errors.Is(err, domain.ErrPersonaNotFound):
|
|
return fiber.NewError(404, err.Error())
|
|
}
|
|
|
|
return fiber.NewError(500, "Failed to create quiz")
|
|
}
|
|
h.recordActivity(c, domain.ActionExamPrepPracticeCreated, domain.ResourceExamPrepPractice, &quiz.ID, "Created chapter quiz: "+quiz.Title, nil)
|
|
return c.Status(201).JSON(domain.Response{Message: "Quiz created successfully", Data: quiz, Success: true, StatusCode: 201})
|
|
}
|
|
|
|
// ListChapterQuizzes godoc
|
|
// @Summary List quizzes in a chapter
|
|
// @Description Returns quizzes and pagination. Admins see drafts; learners see published quizzes with published question sets in published chapters and courses.
|
|
// @Tags quizzes
|
|
// @Security Bearer
|
|
// @Produce json
|
|
// @Param chapterId path int true "Chapter ID"
|
|
// @Param limit query int false "Page size" default(20)
|
|
// @Param offset query int false "Offset" default(0)
|
|
// @Success 200 {object} domain.Response{data=quizListData}
|
|
// @Failure 401 {object} domain.ErrorResponse
|
|
// @Failure 403 {object} domain.ErrorResponse
|
|
// @Failure 404 {object} domain.ErrorResponse
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/admin/chapters/{chapterId}/quizzes [get]
|
|
// @Router /api/v1/chapters/{chapterId}/quizzes [get]
|
|
func (h *Handler) ListChapterQuizzes(c *fiber.Ctx) error {
|
|
chapterID, err := parseAssessmentID(c.Params("chapterId"))
|
|
if err != nil {
|
|
return fiber.NewError(400, "Invalid chapter ID")
|
|
}
|
|
if err = h.visibleQuizChapter(c, chapterID); err != nil {
|
|
return err
|
|
}
|
|
limit, err := strconv.Atoi(c.Query("limit", "20"))
|
|
if err != nil || limit < 1 || limit > 200 {
|
|
return fiber.NewError(400, "limit must be between 1 and 200")
|
|
}
|
|
offset, err := strconv.Atoi(c.Query("offset", "0"))
|
|
if err != nil || offset < 0 {
|
|
return fiber.NewError(400, "offset must be non-negative")
|
|
}
|
|
items, total, err := h.examPrepSvc.ListChapterQuizzes(c.Context(), chapterID, !h.canManageExamPrepPractices(c), int32(limit), int32(offset))
|
|
if err != nil {
|
|
return fiber.NewError(500, "Failed to list quizzes")
|
|
}
|
|
return c.JSON(domain.Response{Message: "Quizzes retrieved successfully", Data: fiber.Map{"quizzes": items, "total_count": total, "limit": limit, "offset": offset}, Success: true, StatusCode: 200})
|
|
}
|
|
|
|
func (h *Handler) visibleQuizChapter(c *fiber.Ctx, chapterID int64) error {
|
|
chapter, err := h.examPrepSvc.GetUnitByID(c.Context(), chapterID)
|
|
if err != nil {
|
|
return fiber.NewError(404, "Chapter not found")
|
|
}
|
|
course, err := h.examPrepSvc.GetCatalogCourseByID(c.Context(), chapter.CatalogCourseID)
|
|
if err != nil {
|
|
return fiber.NewError(404, "Course not found")
|
|
}
|
|
if !h.canManageExamPrepPractices(c) && (!chapter.VisibleToLearners() || !course.VisibleToLearners()) {
|
|
return fiber.NewError(404, "Chapter not found")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// RequireChapterQuiz prevents practice IDs from being consumed via quiz routes.
|
|
func (h *Handler) RequireChapterQuiz(c *fiber.Ctx) error {
|
|
raw := c.Params("id", c.Params("practiceId"))
|
|
id, err := parseAssessmentID(raw)
|
|
if err != nil {
|
|
return fiber.NewError(400, "Invalid quiz ID")
|
|
}
|
|
quiz, err := h.examPrepSvc.GetExamPrepPracticeByID(c.Context(), id)
|
|
if err != nil {
|
|
return fiber.NewError(404, "Quiz not found")
|
|
}
|
|
set, err := h.questionsSvc.GetQuestionSetByID(c.Context(), quiz.QuestionSetID)
|
|
parents := domain.ExamPrepPracticeParentsDeref(quiz.Parents)
|
|
if err != nil || set.SetType != "QUIZ" || !examprep.ValidQuizParents(parents) {
|
|
return fiber.NewError(404, "Quiz not found")
|
|
}
|
|
if !h.canManageExamPrepPractices(c) && (!quiz.VisibleToLearners() || set.Status != "PUBLISHED") {
|
|
return fiber.NewError(404, "Quiz not found")
|
|
}
|
|
if err = h.visibleQuizChapter(c, parents[0].ID); err != nil {
|
|
return err
|
|
}
|
|
return c.Next()
|
|
}
|