RezinaLMS-BackEnd/internal/web_server/handlers/exam_prep_lesson_handler.go

358 lines
13 KiB
Go

package handlers
import (
"Rezina-Backend/internal/domain"
"Rezina-Backend/internal/services/examprep"
"errors"
"fmt"
"strconv"
"github.com/gofiber/fiber/v2"
)
// CreateExamPrepLesson godoc
// @Summary Create a lesson in a chapter
// @Tags learning
// @Param chapterId path int true "Chapter ID"
// @Param body body domain.CreateExamPrepLessonInput true "Lesson"
// @Router /api/v1/admin/chapters/{chapterId}/lessons [post]
// @Security Bearer
// @Success 201 {object} domain.Response{data=domain.ExamPrepLesson}
// @Produce json
// @Failure 401 {string} string "Missing, invalid, or expired Bearer token"
// @Failure 403 {string} string "Account audience, status, permission, or subscription does not allow this operation"
// @Failure 400 {object} domain.ErrorResponse "Invalid request"
// @Failure 404 {object} domain.ErrorResponse "Resource not found"
// @Failure 500 {object} domain.ErrorResponse "Internal server error"
func (h *Handler) CreateExamPrepLesson(c *fiber.Ctx) error {
moduleID, err := chapterLessonModuleID(c)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid chapter id",
Error: err.Error(),
})
}
var req domain.CreateExamPrepLessonInput
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid request body",
Error: err.Error(),
})
}
if valErrs, ok := h.validator.Validate(c, req); !ok {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Validation failed",
Error: firstValidationError(valErrs),
})
}
les, err := h.examPrepSvc.CreateLesson(c.Context(), moduleID, req)
if err != nil {
if errors.Is(err, examprep.ErrModuleNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Chapter not found",
Error: err.Error(),
})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to create lesson",
Error: err.Error(),
})
}
h.recordActivity(c, domain.ActionExamPrepLessonCreated, domain.ResourceExamPrepLesson, &les.ID, "Created lesson: "+les.Title, nil)
return c.Status(fiber.StatusCreated).JSON(domain.Response{
Message: "Lesson created successfully",
Data: les,
Success: true,
StatusCode: fiber.StatusCreated,
})
}
// ListExamPrepLessonsByUnitModule godoc
// @Summary List lessons for a chapter
// @Tags learning
// @Param chapterId path int true "Chapter ID"
// @Param limit query int false "Page size" default(20)
// @Param offset query int false "Offset" default(0)
// @Router /api/v1/admin/chapters/{chapterId}/lessons [get]
// @Router /api/v1/chapters/{chapterId}/lessons [get]
// @Security Bearer
// @Success 200 {object} domain.Response{data=lessonListData}
// @Produce json
// @Failure 401 {string} string "Missing, invalid, or expired Bearer token"
// @Failure 403 {string} string "Account audience, status, permission, or subscription does not allow this operation"
// @Failure 400 {object} domain.ErrorResponse "Invalid request"
// @Failure 404 {object} domain.ErrorResponse "Resource not found"
// @Failure 500 {object} domain.ErrorResponse "Internal server error"
func (h *Handler) ListExamPrepLessonsByUnitModule(c *fiber.Ctx) error {
moduleID, err := chapterLessonModuleID(c)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid chapter id",
Error: err.Error(),
})
}
limit, _ := strconv.Atoi(c.Query("limit", "20"))
offset, _ := strconv.Atoi(c.Query("offset", "0"))
publishedOnly := !h.canManageExamPrepLessons(c)
if publishedOnly {
// Draft modules hide their lessons from non-managers.
m, err := h.examPrepSvc.GetModuleByID(c.Context(), moduleID)
if err == nil && !m.VisibleToLearners() {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Chapter not found",
Error: examprep.ErrModuleNotFound.Error(),
})
}
}
items, total, err := h.examPrepSvc.ListLessonsByUnitModule(c.Context(), moduleID, publishedOnly, int32(limit), int32(offset))
if err != nil {
if errors.Is(err, examprep.ErrModuleNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Chapter not found",
Error: err.Error(),
})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to list lessons",
Error: err.Error(),
})
}
if err := h.annotateExamPrepLessonsEffectiveAccessTier(c.Context(), c.Locals("role").(domain.Role), moduleID, items); err != nil {
if status, msg := mapExamPrepEffectiveAccessTierError(err); status != 0 {
return c.Status(status).JSON(domain.ErrorResponse{Message: msg, Error: err.Error()})
}
}
if err := h.applyExamPrepAccessLessons(c.Context(), c, items); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to build lesson list",
Error: err.Error(),
})
}
return c.JSON(domain.Response{
Message: "Lessons retrieved successfully",
Data: fiber.Map{
"lessons": items,
"total_count": total,
"limit": limit,
"offset": offset,
},
Success: true,
StatusCode: fiber.StatusOK,
})
}
// ReorderExamPrepLessonsInUnitModule godoc
// @Summary Reorder lessons within a chapter
// @Tags learning
// @Router /api/v1/admin/chapters/{chapterId}/lessons/reorder [put]
// @Security Bearer
// @Param chapterId path int64 true "Chapter id"
// @Accept json
// @Param body body domain.ReorderIDsRequest true "Request payload"
// @Success 200 {object} domain.Response
// @Produce json
// @Failure 401 {string} string "Missing, invalid, or expired Bearer token"
// @Failure 403 {string} string "Account audience, status, permission, or subscription does not allow this operation"
// @Failure 400 {object} domain.ErrorResponse "Invalid request"
// @Failure 404 {object} domain.ErrorResponse "Resource not found"
// @Failure 500 {object} domain.ErrorResponse "Internal server error"
func (h *Handler) ReorderExamPrepLessonsInUnitModule(c *fiber.Ctx) error {
moduleID, err := chapterLessonModuleID(c)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid chapter id",
Error: err.Error(),
})
}
var req domain.ReorderIDsRequest
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid request body",
Error: err.Error(),
})
}
if req.OrderedIDs == nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "ordered_ids is required (use an empty array if there are no lessons)",
Error: "missing ordered_ids",
})
}
if err := h.examPrepSvc.ReorderLessonsInUnitModule(c.Context(), moduleID, req.OrderedIDs); err != nil {
if errors.Is(err, examprep.ErrModuleNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Chapter not found",
Error: err.Error(),
})
}
if errors.Is(err, domain.ErrReorderInvalidIDSet) {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: err.Error(),
Error: "INVALID_REORDER",
})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to reorder lessons",
Error: err.Error(),
})
}
h.recordActivity(c, domain.ActionExamPrepLessonReordered, domain.ResourceExamPrepLesson, &moduleID, "Reordered exam prep lessons in module", nil)
return c.JSON(domain.Response{
Message: "Lessons reordered successfully",
Success: true,
StatusCode: fiber.StatusOK,
})
}
// GetExamPrepLessonByID godoc
// @Summary Get lesson by ID
// @Tags learning
// @Router /api/v1/admin/lessons/{id} [get]
// @Router /api/v1/lessons/{id} [get]
// @Security Bearer
// @Param id path int64 true "Id"
// @Success 200 {object} domain.Response{data=domain.ExamPrepLesson}
// @Produce json
// @Failure 401 {string} string "Missing, invalid, or expired Bearer token"
// @Failure 403 {string} string "Account audience, status, permission, or subscription does not allow this operation"
// @Failure 400 {object} domain.ErrorResponse "Invalid request"
// @Failure 404 {object} domain.ErrorResponse "Resource not found"
// @Failure 500 {object} domain.ErrorResponse "Internal server error"
func (h *Handler) GetExamPrepLessonByID(c *fiber.Ctx) error {
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid lesson id",
Error: err.Error(),
})
}
les, err := h.examPrepSvc.GetLessonByID(c.Context(), id)
if err != nil {
if errors.Is(err, examprep.ErrLessonNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Lesson not found",
Error: err.Error(),
})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to get lesson",
Error: err.Error(),
})
}
if !les.VisibleToLearners() && !h.canManageExamPrepLessons(c) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Lesson not found",
Error: examprep.ErrLessonNotFound.Error(),
})
}
if err := h.annotateExamPrepLessonEffectiveAccessTier(c.Context(), c.Locals("role").(domain.Role), &les); err != nil {
if status, msg := mapExamPrepEffectiveAccessTierError(err); status != 0 {
return c.Status(status).JSON(domain.ErrorResponse{Message: msg, Error: err.Error()})
}
}
if err := h.applyExamPrepAccessLesson(c.Context(), c, &les); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to build lesson",
Error: err.Error(),
})
}
return c.JSON(domain.Response{
Message: "Lesson retrieved successfully",
Data: les,
Success: true,
StatusCode: fiber.StatusOK,
})
}
// UpdateExamPrepLesson godoc
// @Summary Update lesson
// @Tags learning
// @Router /api/v1/admin/lessons/{id} [put]
// @Security Bearer
// @Param id path int64 true "Id"
// @Accept json
// @Param body body domain.UpdateExamPrepLessonInput true "Request payload"
// @Success 200 {object} domain.Response{data=domain.ExamPrepLesson}
// @Produce json
// @Failure 401 {string} string "Missing, invalid, or expired Bearer token"
// @Failure 403 {string} string "Account audience, status, permission, or subscription does not allow this operation"
// @Failure 400 {object} domain.ErrorResponse "Invalid request"
// @Failure 404 {object} domain.ErrorResponse "Resource not found"
// @Failure 500 {object} domain.ErrorResponse "Internal server error"
func (h *Handler) UpdateExamPrepLesson(c *fiber.Ctx) error {
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid lesson id",
Error: err.Error(),
})
}
var req domain.UpdateExamPrepLessonInput
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid request body",
Error: err.Error(),
})
}
les, err := h.examPrepSvc.UpdateLesson(c.Context(), id, req)
if err != nil {
if errors.Is(err, examprep.ErrLessonNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Lesson not found",
Error: err.Error(),
})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to update lesson",
Error: err.Error(),
})
}
h.recordActivity(c, domain.ActionExamPrepLessonUpdated, domain.ResourceExamPrepLesson, &les.ID, "Updated exam prep lesson: "+les.Title, nil)
return c.JSON(domain.Response{
Message: "Lesson updated successfully",
Data: les,
Success: true,
StatusCode: fiber.StatusOK,
})
}
// DeleteExamPrepLesson godoc
// @Summary Delete lesson
// @Tags learning
// @Router /api/v1/admin/lessons/{id} [delete]
// @Security Bearer
// @Param id path int64 true "Id"
// @Success 200 {object} domain.Response
// @Produce json
// @Failure 401 {string} string "Missing, invalid, or expired Bearer token"
// @Failure 403 {string} string "Account audience, status, permission, or subscription does not allow this operation"
// @Failure 400 {object} domain.ErrorResponse "Invalid request"
// @Failure 404 {object} domain.ErrorResponse "Resource not found"
// @Failure 500 {object} domain.ErrorResponse "Internal server error"
func (h *Handler) DeleteExamPrepLesson(c *fiber.Ctx) error {
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid lesson id",
Error: err.Error(),
})
}
if err := h.examPrepSvc.DeleteLesson(c.Context(), id); err != nil {
if errors.Is(err, examprep.ErrLessonNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Lesson not found",
Error: err.Error(),
})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to delete lesson",
Error: err.Error(),
})
}
h.recordActivity(c, domain.ActionExamPrepLessonDeleted, domain.ResourceExamPrepLesson, &id, fmt.Sprintf("Deleted exam prep lesson ID: %d", id), nil)
return c.JSON(domain.Response{
Message: "Lesson deleted successfully",
Success: true,
StatusCode: fiber.StatusOK,
})
}