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

41 lines
1.4 KiB
Go

package handlers
import (
"strconv"
"github.com/gofiber/fiber/v2"
)
// ResolveChapterLessonContainer adapts the legacy persistence layer to the
// public course -> chapter -> lesson hierarchy.
func (h *Handler) ResolveChapterLessonContainer(c *fiber.Ctx) error {
chapterID, err := parseAssessmentID(c.Params("chapterId"))
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid chapter ID")
}
chapter, err := h.examPrepSvc.GetUnitByID(c.Context(), chapterID)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, "Chapter not found")
}
course, err := h.examPrepSvc.GetCatalogCourseByID(c.Context(), chapter.CatalogCourseID)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, "Course not found")
}
if !h.canManageExamPrepLessons(c) && (!chapter.VisibleToLearners() || !course.VisibleToLearners()) {
return fiber.NewError(fiber.StatusNotFound, "Chapter not found")
}
moduleID, err := h.examPrepSvc.GetChapterLessonContainer(c.Context(), chapterID)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to resolve chapter lessons")
}
c.Locals("chapter_lesson_module_id", moduleID)
return c.Next()
}
func chapterLessonModuleID(c *fiber.Ctx) (int64, error) {
if id, ok := c.Locals("chapter_lesson_module_id").(int64); ok {
return id, nil
}
return strconv.ParseInt(c.Params("moduleId"), 10, 64)
}