package handlers import ( "Yimaru-Backend/internal/domain" "Yimaru-Backend/internal/services/examprep" "errors" "fmt" "strconv" "github.com/gofiber/fiber/v2" ) // CreateExamPrepPractice godoc // @Summary Create exam-prep practice (under a lesson; uses shared question_sets) // @Tags exam-prep // @Param lessonId path int true "Exam prep lesson ID (unit_module_lessons.id)" // @Param body body domain.CreateExamPrepPracticeInput true "Practice" // @Router /api/v1/exam-prep/lessons/{lessonId}/practices [post] func (h *Handler) CreateExamPrepPractice(c *fiber.Ctx) error { lessonID, err := strconv.ParseInt(c.Params("lessonId"), 10, 64) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: "Invalid lesson id", Error: err.Error(), }) } var req domain.CreateExamPrepPracticeInput 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), }) } p, err := h.examPrepSvc.CreateExamPrepPractice(c.Context(), lessonID, req) if err != nil { if resp := h.mapCreateExamPrepPracticeError(c, err); resp != nil { return resp } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to create practice", Error: err.Error(), }) } h.recordActivity(c, domain.ActionExamPrepPracticeCreated, domain.ResourceExamPrepPractice, &p.ID, "Created exam prep practice: "+p.Title, nil) return c.Status(fiber.StatusCreated).JSON(domain.Response{ Message: "Practice created successfully", Data: p, Success: true, StatusCode: fiber.StatusCreated, }) } // ListExamPrepPracticesByLesson godoc // @Summary List exam-prep practices for a lesson // @Tags exam-prep // @Param lessonId path int true "Exam prep lesson ID" // @Param limit query int false "Page size" default(20) // @Param offset query int false "Offset" default(0) // @Router /api/v1/exam-prep/lessons/{lessonId}/practices [get] func (h *Handler) ListExamPrepPracticesByLesson(c *fiber.Ctx) error { lessonID, err := strconv.ParseInt(c.Params("lessonId"), 10, 64) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: "Invalid lesson id", Error: err.Error(), }) } limit, _ := strconv.Atoi(c.Query("limit", "20")) offset, _ := strconv.Atoi(c.Query("offset", "0")) publishedOnly := !h.canManageExamPrepPractices(c) items, total, err := h.examPrepSvc.ListExamPrepPracticesByLesson(c.Context(), lessonID, publishedOnly, int32(limit), int32(offset)) if err != nil { if resp := h.listExamPrepPracticesResponse(c, nil, 0, limit, offset, err); resp != nil { return resp } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to list practices", Error: err.Error(), }) } return h.listExamPrepPracticesResponse(c, items, total, limit, offset, nil) } // GetExamPrepPracticeByID godoc // @Summary Get exam-prep practice by ID // @Tags exam-prep // @Param id path int true "Exam prep practice ID" // @Router /api/v1/exam-prep/practices/{id} [get] func (h *Handler) GetExamPrepPracticeByID(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 practice id", Error: err.Error(), }) } p, err := h.examPrepSvc.GetExamPrepPracticeByID(c.Context(), id) if err != nil { if errors.Is(err, examprep.ErrPracticeNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Practice not found", Error: err.Error(), }) } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to load practice", Error: err.Error(), }) } if !p.VisibleToLearners() && !h.canManageExamPrepPractices(c) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found"}) } if err := h.enforceExamPrepPracticeSequenceForStudent(c, p.QuestionSetID); err != nil { return practiceSequenceErrorResponse(c, err) } return c.JSON(domain.Response{ Message: "Practice retrieved successfully", Data: p, Success: true, StatusCode: fiber.StatusOK, }) } // UpdateExamPrepPractice godoc // @Summary Update exam-prep practice // @Tags exam-prep // @Param id path int true "Exam prep practice ID" // @Param body body domain.UpdateExamPrepPracticeInput true "Fields to update" // @Router /api/v1/exam-prep/practices/{id} [put] func (h *Handler) UpdateExamPrepPractice(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 practice id", Error: err.Error(), }) } var req domain.UpdateExamPrepPracticeInput if err := c.BodyParser(&req); err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: "Invalid request body", Error: err.Error(), }) } p, err := h.examPrepSvc.UpdateExamPrepPractice(c.Context(), id, req) if err != nil { if errors.Is(err, examprep.ErrPracticeNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Practice not found", Error: err.Error(), }) } if errors.Is(err, domain.ErrPersonaNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Persona not found", Error: err.Error(), }) } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to update practice", Error: err.Error(), }) } h.recordActivity(c, domain.ActionExamPrepPracticeUpdated, domain.ResourceExamPrepPractice, &p.ID, "Updated exam prep practice: "+p.Title, nil) return c.JSON(domain.Response{ Message: "Practice updated successfully", Data: p, Success: true, StatusCode: fiber.StatusOK, }) } // DeleteExamPrepPractice godoc // @Summary Delete exam-prep practice // @Tags exam-prep // @Param id path int true "Exam prep practice ID" // @Router /api/v1/exam-prep/practices/{id} [delete] func (h *Handler) DeleteExamPrepPractice(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 practice id", Error: err.Error(), }) } if err := h.examPrepSvc.DeleteExamPrepPractice(c.Context(), id); err != nil { if errors.Is(err, examprep.ErrPracticeNotFound) { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Practice not found", Error: err.Error(), }) } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to delete practice", Error: err.Error(), }) } h.recordActivity(c, domain.ActionExamPrepPracticeDeleted, domain.ResourceExamPrepPractice, &id, fmt.Sprintf("Deleted exam prep practice ID: %d", id), nil) return c.JSON(domain.Response{ Message: "Practice deleted successfully", Success: true, StatusCode: fiber.StatusOK, }) } func (h *Handler) mapCreateExamPrepPracticeError(c *fiber.Ctx, err error) error { switch { case errors.Is(err, examprep.ErrLessonNotFound), errors.Is(err, examprep.ErrUnitNotFound), errors.Is(err, examprep.ErrCatalogCourseNotFound): return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Parent not found", Error: err.Error(), }) case errors.Is(err, domain.ErrPersonaNotFound): return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ Message: "Persona not found", Error: err.Error(), }) case errors.Is(err, examprep.ErrExamPrepPracticeParentLimitExceeded): return c.Status(fiber.StatusConflict).JSON(domain.ErrorResponse{ Message: "This parent already has a practice", Error: err.Error(), }) default: return nil } } func (h *Handler) listExamPrepPracticesResponse(c *fiber.Ctx, items []domain.ExamPrepPractice, total int64, limit, offset int, notFound error) error { if notFound != nil { switch { case errors.Is(notFound, examprep.ErrLessonNotFound): return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Lesson not found", Error: notFound.Error()}) case errors.Is(notFound, examprep.ErrUnitNotFound): return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Unit not found", Error: notFound.Error()}) case errors.Is(notFound, examprep.ErrCatalogCourseNotFound): return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Catalog course not found", Error: notFound.Error()}) } } return c.JSON(domain.Response{ Message: "Practices retrieved successfully", Data: fiber.Map{ "practices": items, "total_count": total, "limit": limit, "offset": offset, }, Success: true, StatusCode: fiber.StatusOK, }) } // CreateExamPrepPracticeForCatalogCourse godoc // @Summary Create exam-prep practice under a catalog course // @Tags exam-prep // @Param catalogCourseId path int true "Exam prep catalog course ID" // @Param body body domain.CreateExamPrepPracticeInput true "Practice" // @Router /api/v1/exam-prep/catalog-courses/{catalogCourseId}/practices [post] func (h *Handler) CreateExamPrepPracticeForCatalogCourse(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.CreateExamPrepPracticeInput 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)}) } p, err := h.examPrepSvc.CreateExamPrepPracticeForCatalogCourse(c.Context(), catalogCourseID, req) if err != nil { if resp := h.mapCreateExamPrepPracticeError(c, err); resp != nil { return resp } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to create practice", Error: err.Error()}) } h.recordActivity(c, domain.ActionExamPrepPracticeCreated, domain.ResourceExamPrepPractice, &p.ID, "Created exam prep practice: "+p.Title, nil) return c.Status(fiber.StatusCreated).JSON(domain.Response{ Message: "Practice created successfully", Data: p, Success: true, StatusCode: fiber.StatusCreated, }) } // ListExamPrepPracticesByCatalogCourse godoc // @Summary List exam-prep practices for a catalog course // @Tags exam-prep // @Param catalogCourseId path int true "Exam prep catalog course ID" // @Router /api/v1/exam-prep/catalog-courses/{catalogCourseId}/practices [get] func (h *Handler) ListExamPrepPracticesByCatalogCourse(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")) publishedOnly := !h.canManageExamPrepPractices(c) items, total, err := h.examPrepSvc.ListExamPrepPracticesByCatalogCourse(c.Context(), catalogCourseID, publishedOnly, int32(limit), int32(offset)) if err != nil { if resp := h.listExamPrepPracticesResponse(c, nil, 0, limit, offset, err); resp != nil { return resp } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to list practices", Error: err.Error()}) } return h.listExamPrepPracticesResponse(c, items, total, limit, offset, nil) } // CreateExamPrepPracticeForUnit godoc // @Summary Create exam-prep practice under a unit // @Tags exam-prep // @Param unitId path int true "Exam prep unit ID" // @Param body body domain.CreateExamPrepPracticeInput true "Practice" // @Router /api/v1/exam-prep/units/{unitId}/practices [post] func (h *Handler) CreateExamPrepPracticeForUnit(c *fiber.Ctx) error { unitID, err := strconv.ParseInt(c.Params("unitId"), 10, 64) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "Invalid unit id", Error: err.Error()}) } var req domain.CreateExamPrepPracticeInput 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)}) } p, err := h.examPrepSvc.CreateExamPrepPracticeForUnit(c.Context(), unitID, req) if err != nil { if resp := h.mapCreateExamPrepPracticeError(c, err); resp != nil { return resp } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to create practice", Error: err.Error()}) } h.recordActivity(c, domain.ActionExamPrepPracticeCreated, domain.ResourceExamPrepPractice, &p.ID, "Created exam prep practice: "+p.Title, nil) return c.Status(fiber.StatusCreated).JSON(domain.Response{ Message: "Practice created successfully", Data: p, Success: true, StatusCode: fiber.StatusCreated, }) } // ListExamPrepPracticesByUnit godoc // @Summary List exam-prep practices for a unit // @Tags exam-prep // @Param unitId path int true "Exam prep unit ID" // @Router /api/v1/exam-prep/units/{unitId}/practices [get] func (h *Handler) ListExamPrepPracticesByUnit(c *fiber.Ctx) error { unitID, err := strconv.ParseInt(c.Params("unitId"), 10, 64) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "Invalid unit id", Error: err.Error()}) } limit, _ := strconv.Atoi(c.Query("limit", "20")) offset, _ := strconv.Atoi(c.Query("offset", "0")) publishedOnly := !h.canManageExamPrepPractices(c) items, total, err := h.examPrepSvc.ListExamPrepPracticesByUnit(c.Context(), unitID, publishedOnly, int32(limit), int32(offset)) if err != nil { if resp := h.listExamPrepPracticesResponse(c, nil, 0, limit, offset, err); resp != nil { return resp } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to list practices", Error: err.Error()}) } return h.listExamPrepPracticesResponse(c, items, total, limit, offset, nil) }