package handlers import ( "Yimaru-Backend/internal/domain" "Yimaru-Backend/internal/services/examprep" "errors" "strconv" "github.com/gofiber/fiber/v2" ) // CreateExamPrepUnit godoc // @Summary Create exam-prep unit // @Description Unit under a catalog course (e.g. chapter title). Optional sort_order assigns position within that catalog course (siblings at or after that index are shifted); omit to append after the current highest sort_order in the catalog course. // @Tags exam-prep // @Accept json // @Produce json // @Param catalogCourseId path int true "Catalog course ID" // @Param body body domain.CreateExamPrepUnitInput true "Unit" // @Success 201 {object} domain.Response // @Router /api/v1/exam-prep/catalog-courses/{catalogCourseId}/units [post] func (h *Handler) CreateExamPrepUnit(c *fiber.Ctx) error { catalogCourseID, err := strconv.ParseInt(c.Params("catalogCourseId"), 10, 64) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: "Invalid catalog course id", Error: err.Error(), }) } var req domain.CreateExamPrepUnitInput 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), }) } out, err := h.examPrepSvc.CreateUnit(c.Context(), catalogCourseID, req) if err != nil { if errors.Is(err, examprep.ErrCatalogCourseNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Catalog course not found", Error: err.Error(), }) } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to create unit", Error: err.Error(), }) } return c.Status(fiber.StatusCreated).JSON(domain.Response{ Message: "Unit created successfully", Data: out, Success: true, StatusCode: fiber.StatusCreated, }) } // ListExamPrepUnitsByCatalogCourse godoc // @Summary List exam-prep units for a catalog course // @Tags exam-prep // @Param catalogCourseId path int true "Catalog course ID" // @Param limit query int false "Page size" default(20) // @Param offset query int false "Offset" default(0) // @Success 200 {object} domain.Response // @Router /api/v1/exam-prep/catalog-courses/{catalogCourseId}/units [get] func (h *Handler) ListExamPrepUnitsByCatalogCourse(c *fiber.Ctx) error { catalogCourseID, err := strconv.ParseInt(c.Params("catalogCourseId"), 10, 64) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: "Invalid catalog course id", Error: err.Error(), }) } limit, _ := strconv.Atoi(c.Query("limit", "20")) offset, _ := strconv.Atoi(c.Query("offset", "0")) items, total, err := h.examPrepSvc.ListUnitsByCatalogCourse(c.Context(), catalogCourseID, int32(limit), int32(offset)) if err != nil { if errors.Is(err, examprep.ErrCatalogCourseNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Catalog course not found", Error: err.Error(), }) } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to list units", Error: err.Error(), }) } return c.JSON(domain.Response{ Message: "Units retrieved successfully", Data: fiber.Map{ "units": items, "total_count": total, "limit": limit, "offset": offset, }, Success: true, StatusCode: fiber.StatusOK, }) } // ReorderExamPrepUnitsInCatalogCourse godoc // @Summary Reorder units within a catalog course // @Tags exam-prep // @Param catalogCourseId path int true "Catalog course ID" // @Param body body domain.ReorderIDsRequest true "ordered_ids: every unit id in this catalog course, new order" // @Router /api/v1/exam-prep/catalog-courses/{catalogCourseId}/units/reorder [put] func (h *Handler) ReorderExamPrepUnitsInCatalogCourse(c *fiber.Ctx) error { catalogCourseID, err := strconv.ParseInt(c.Params("catalogCourseId"), 10, 64) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: "Invalid catalog course 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 units)", Error: "missing ordered_ids", }) } if err := h.examPrepSvc.ReorderUnitsInCatalogCourse(c.Context(), catalogCourseID, req.OrderedIDs); err != nil { if errors.Is(err, examprep.ErrCatalogCourseNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Catalog course 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 units", Error: err.Error(), }) } return c.JSON(domain.Response{ Message: "Units reordered successfully", Success: true, StatusCode: fiber.StatusOK, }) } // GetExamPrepUnitByID godoc // @Summary Get exam-prep unit by ID // @Tags exam-prep // @Param id path int true "Unit ID" // @Router /api/v1/exam-prep/units/{id} [get] func (h *Handler) GetExamPrepUnitByID(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 unit id", Error: err.Error(), }) } out, err := h.examPrepSvc.GetUnitByID(c.Context(), id) if err != nil { if errors.Is(err, examprep.ErrUnitNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Unit not found", Error: err.Error(), }) } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to get unit", Error: err.Error(), }) } return c.JSON(domain.Response{ Message: "Unit retrieved successfully", Data: out, Success: true, StatusCode: fiber.StatusOK, }) } // UpdateExamPrepUnit godoc // @Summary Update exam-prep unit // @Tags exam-prep // @Param id path int true "Unit ID" // @Param body body domain.UpdateExamPrepUnitInput true "Fields to update" // @Router /api/v1/exam-prep/units/{id} [put] func (h *Handler) UpdateExamPrepUnit(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 unit id", Error: err.Error(), }) } var req domain.UpdateExamPrepUnitInput if err := c.BodyParser(&req); err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: "Invalid request body", Error: err.Error(), }) } out, err := h.examPrepSvc.UpdateUnit(c.Context(), id, req) if err != nil { if errors.Is(err, examprep.ErrUnitNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Unit not found", Error: err.Error(), }) } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to update unit", Error: err.Error(), }) } return c.JSON(domain.Response{ Message: "Unit updated successfully", Data: out, Success: true, StatusCode: fiber.StatusOK, }) } // DeleteExamPrepUnit godoc // @Summary Delete exam-prep unit // @Tags exam-prep // @Param id path int true "Unit ID" // @Router /api/v1/exam-prep/units/{id} [delete] func (h *Handler) DeleteExamPrepUnit(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 unit id", Error: err.Error(), }) } if err := h.examPrepSvc.DeleteUnit(c.Context(), id); err != nil { if errors.Is(err, examprep.ErrUnitNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Unit not found", Error: err.Error(), }) } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to delete unit", Error: err.Error(), }) } return c.JSON(domain.Response{ Message: "Unit deleted successfully", Success: true, StatusCode: fiber.StatusOK, }) }