diff --git a/db/query/exam_prep_lesson_practices.sql b/db/query/exam_prep_lesson_practices.sql index 3590e168..e671026c 100644 --- a/db/query/exam_prep_lesson_practices.sql +++ b/db/query/exam_prep_lesson_practices.sql @@ -44,8 +44,8 @@ WHERE pp.parent_kind = sqlc.arg('parent_kind')::varchar OR p.publish_status = 'PUBLISHED'::TEXT ) ORDER BY p.created_at DESC -LIMIT $1 -OFFSET $2; +LIMIT sqlc.narg('limit')::INT +OFFSET sqlc.narg('offset')::INT; -- name: ExamPrepUpdateLessonPractice :one UPDATE exam_prep.lesson_practices @@ -93,4 +93,5 @@ WHERE ( ) ) ORDER BY p.created_at DESC -LIMIT $1 OFFSET $2; +LIMIT sqlc.narg('limit')::INT +OFFSET sqlc.narg('offset')::INT; diff --git a/gen/db/exam_prep_lesson_practices.sql.go b/gen/db/exam_prep_lesson_practices.sql.go index a7e96817..eeec422d 100644 --- a/gen/db/exam_prep_lesson_practices.sql.go +++ b/gen/db/exam_prep_lesson_practices.sql.go @@ -138,23 +138,23 @@ SELECT p.updated_at FROM exam_prep.lesson_practices p INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id -WHERE pp.parent_kind = $3::varchar - AND pp.parent_id = $4::bigint +WHERE pp.parent_kind = $1::varchar + AND pp.parent_id = $2::bigint AND ( - $5::boolean = FALSE + $3::boolean = FALSE OR p.publish_status = 'PUBLISHED'::TEXT ) ORDER BY p.created_at DESC -LIMIT $1 -OFFSET $2 +LIMIT $5::INT +OFFSET $4::INT ` type ExamPrepListPracticesByParentParams struct { - Limit int32 `json:"limit"` - Offset int32 `json:"offset"` - ParentKind string `json:"parent_kind"` - ParentID int64 `json:"parent_id"` - PublishedOnly bool `json:"published_only"` + ParentKind string `json:"parent_kind"` + ParentID int64 `json:"parent_id"` + PublishedOnly bool `json:"published_only"` + Offset pgtype.Int4 `json:"offset"` + Limit pgtype.Int4 `json:"limit"` } type ExamPrepListPracticesByParentRow struct { @@ -173,11 +173,11 @@ type ExamPrepListPracticesByParentRow struct { func (q *Queries) ExamPrepListPracticesByParent(ctx context.Context, arg ExamPrepListPracticesByParentParams) ([]ExamPrepListPracticesByParentRow, error) { rows, err := q.db.Query(ctx, ExamPrepListPracticesByParent, - arg.Limit, - arg.Offset, arg.ParentKind, arg.ParentID, arg.PublishedOnly, + arg.Offset, + arg.Limit, ) if err != nil { return nil, err @@ -281,11 +281,11 @@ SELECT p.updated_at FROM exam_prep.lesson_practices p WHERE ( - $3::boolean = FALSE + $1::boolean = FALSE OR p.publish_status = 'PUBLISHED'::TEXT ) AND ( - $4::boolean = FALSE + $2::boolean = FALSE OR NOT EXISTS ( SELECT 1 FROM exam_prep.practice_parents pp @@ -293,14 +293,15 @@ WHERE ( ) ) ORDER BY p.created_at DESC -LIMIT $1 OFFSET $2 +LIMIT $4::INT +OFFSET $3::INT ` type ListAllExamPrepPracticesParams struct { - Limit int32 `json:"limit"` - Offset int32 `json:"offset"` - PublishedOnly bool `json:"published_only"` - UnlinkedOnly bool `json:"unlinked_only"` + PublishedOnly bool `json:"published_only"` + UnlinkedOnly bool `json:"unlinked_only"` + Offset pgtype.Int4 `json:"offset"` + Limit pgtype.Int4 `json:"limit"` } type ListAllExamPrepPracticesRow struct { @@ -319,10 +320,10 @@ type ListAllExamPrepPracticesRow struct { func (q *Queries) ListAllExamPrepPractices(ctx context.Context, arg ListAllExamPrepPracticesParams) ([]ListAllExamPrepPracticesRow, error) { rows, err := q.db.Query(ctx, ListAllExamPrepPractices, - arg.Limit, - arg.Offset, arg.PublishedOnly, arg.UnlinkedOnly, + arg.Offset, + arg.Limit, ) if err != nil { return nil, err diff --git a/internal/repository/exam_prep_lesson_practices.go b/internal/repository/exam_prep_lesson_practices.go index da55b5a4..c251d75f 100644 --- a/internal/repository/exam_prep_lesson_practices.go +++ b/internal/repository/exam_prep_lesson_practices.go @@ -204,8 +204,8 @@ func (s *Store) TryGetExamPrepLessonPracticeByQuestionSetID(ctx context.Context, func (s *Store) ListExamPrepPracticesByParent(ctx context.Context, parent domain.ExamPrepPracticeParent, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) { rows, err := s.queries.ExamPrepListPracticesByParent(ctx, dbgen.ExamPrepListPracticesByParentParams{ - Limit: limit, - Offset: offset, + Limit: optionalPositiveInt4(limit), + Offset: optionalNonNegativeInt4(offset), ParentKind: string(parent.Kind), ParentID: parent.ID, PublishedOnly: publishedOnly, @@ -231,8 +231,8 @@ func (s *Store) ListAllExamPrepPractices(ctx context.Context, publishedOnly, unl rows, err := s.queries.ListAllExamPrepPractices(ctx, dbgen.ListAllExamPrepPracticesParams{ PublishedOnly: publishedOnly, UnlinkedOnly: unlinkedOnly, - Limit: limit, - Offset: offset, + Limit: optionalPositiveInt4(limit), + Offset: optionalNonNegativeInt4(offset), }) if err != nil { return nil, 0, err @@ -331,3 +331,19 @@ func (s *Store) UpdateExamPrepLessonPractice(ctx context.Context, id int64, inpu func (s *Store) DeleteExamPrepLessonPractice(ctx context.Context, id int64) error { return s.queries.ExamPrepDeleteLessonPractice(ctx, id) } + +// optionalPositiveInt4 maps values <= 0 to SQL NULL (no LIMIT → return all rows). +func optionalPositiveInt4(v int32) pgtype.Int4 { + if v <= 0 { + return pgtype.Int4{} + } + return pgtype.Int4{Int32: v, Valid: true} +} + +// optionalNonNegativeInt4 maps values < 0 to SQL NULL; 0+ are sent as-is (NULL OFFSET ≡ 0). +func optionalNonNegativeInt4(v int32) pgtype.Int4 { + if v < 0 { + return pgtype.Int4{} + } + return pgtype.Int4{Int32: v, Valid: true} +} diff --git a/internal/web_server/handlers/exam_prep_practice_handler.go b/internal/web_server/handlers/exam_prep_practice_handler.go index 3999017d..5b6ef76e 100644 --- a/internal/web_server/handlers/exam_prep_practice_handler.go +++ b/internal/web_server/handlers/exam_prep_practice_handler.go @@ -51,26 +51,31 @@ func (h *Handler) CreateExamPrepPracticeFromInput(c *fiber.Ctx) error { // ListAllExamPrepPractices godoc // @Summary List exam-prep practices globally // @Tags exam-prep -// @Param limit query int false "Page size" default(20) -// @Param offset query int false "Offset" default(0) +// @Param limit query int false "Page size. Omit (with offset) to return all practices." +// @Param offset query int false "Offset. Defaults to 0 when limit is provided; ignored semantics when both omitted (all rows)." // @Param unlinked_only query bool false "When true, return only practices with no parent links (admin)" // @Router /api/v1/exam-prep/practices [get] func (h *Handler) ListAllExamPrepPractices(c *fiber.Ctx) error { - limit, _ := strconv.Atoi(c.Query("limit", "20")) - offset, _ := strconv.Atoi(c.Query("offset", "0")) + limit, offset, err := parseOptionalExamPrepPracticePagination(c) + if err != nil { + return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ + Message: "Invalid pagination", + Error: err.Error(), + }) + } publishedOnly := !h.canManageExamPrepPractices(c) unlinkedOnly := false if h.canManageExamPrepPractices(c) { unlinkedOnly, _ = strconv.ParseBool(c.Query("unlinked_only", "false")) } - items, total, err := h.examPrepSvc.ListAllExamPrepPractices(c.Context(), publishedOnly, unlinkedOnly, int32(limit), int32(offset)) + items, total, err := h.examPrepSvc.ListAllExamPrepPractices(c.Context(), publishedOnly, unlinkedOnly, limit, offset) if err != nil { 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) + return h.listExamPrepPracticesResponse(c, items, total, int(limit), int(offset), nil) } // CreateExamPrepPractice godoc @@ -123,8 +128,8 @@ func (h *Handler) CreateExamPrepPractice(c *fiber.Ctx) error { // @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) +// @Param limit query int false "Page size. Omit to return all practices for the lesson." +// @Param offset query int false "Offset (used when limit is provided)" // @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) @@ -134,12 +139,17 @@ func (h *Handler) ListExamPrepPracticesByLesson(c *fiber.Ctx) error { 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)) + limit, offset, err := parseOptionalExamPrepPracticePagination(c) if err != nil { - if resp := h.listExamPrepPracticesResponse(c, nil, 0, limit, offset, err); resp != nil { + return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ + Message: "Invalid pagination", + Error: err.Error(), + }) + } + publishedOnly := !h.canManageExamPrepPractices(c) + items, total, err := h.examPrepSvc.ListExamPrepPracticesByLesson(c.Context(), lessonID, publishedOnly, limit, offset) + if err != nil { + if resp := h.listExamPrepPracticesResponse(c, nil, 0, int(limit), int(offset), err); resp != nil { return resp } return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ @@ -147,7 +157,7 @@ func (h *Handler) ListExamPrepPracticesByLesson(c *fiber.Ctx) error { Error: err.Error(), }) } - return h.listExamPrepPracticesResponse(c, items, total, limit, offset, nil) + return h.listExamPrepPracticesResponse(c, items, total, int(limit), int(offset), nil) } // GetExamPrepPracticeByID godoc @@ -469,23 +479,30 @@ func (h *Handler) CreateExamPrepPracticeForCatalogCourse(c *fiber.Ctx) error { // @Summary List exam-prep practices for a catalog course // @Tags exam-prep // @Param catalogCourseId path int true "Exam prep catalog course ID" +// @Param limit query int false "Page size. Omit to return all practices for the catalog course." +// @Param offset query int false "Offset (used when limit is provided)" // @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)) + limit, offset, err := parseOptionalExamPrepPracticePagination(c) if err != nil { - if resp := h.listExamPrepPracticesResponse(c, nil, 0, limit, offset, err); resp != nil { + return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ + Message: "Invalid pagination", + Error: err.Error(), + }) + } + publishedOnly := !h.canManageExamPrepPractices(c) + items, total, err := h.examPrepSvc.ListExamPrepPracticesByCatalogCourse(c.Context(), catalogCourseID, publishedOnly, limit, offset) + if err != nil { + if resp := h.listExamPrepPracticesResponse(c, nil, 0, int(limit), int(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) + return h.listExamPrepPracticesResponse(c, items, total, int(limit), int(offset), nil) } // CreateExamPrepPracticeForUnit godoc @@ -523,21 +540,49 @@ func (h *Handler) CreateExamPrepPracticeForUnit(c *fiber.Ctx) error { // @Summary List exam-prep practices for a unit // @Tags exam-prep // @Param unitId path int true "Exam prep unit ID" +// @Param limit query int false "Page size. Omit to return all practices for the unit." +// @Param offset query int false "Offset (used when limit is provided)" // @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)) + limit, offset, err := parseOptionalExamPrepPracticePagination(c) if err != nil { - if resp := h.listExamPrepPracticesResponse(c, nil, 0, limit, offset, err); resp != nil { + return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ + Message: "Invalid pagination", + Error: err.Error(), + }) + } + publishedOnly := !h.canManageExamPrepPractices(c) + items, total, err := h.examPrepSvc.ListExamPrepPracticesByUnit(c.Context(), unitID, publishedOnly, limit, offset) + if err != nil { + if resp := h.listExamPrepPracticesResponse(c, nil, 0, int(limit), int(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) + return h.listExamPrepPracticesResponse(c, items, total, int(limit), int(offset), nil) +} + +// parseOptionalExamPrepPracticePagination returns limit/offset for exam-prep practice lists. +// When limit is omitted, limit=0 means "return all" (no SQL LIMIT). +// When offset is omitted, offset defaults to 0. +func parseOptionalExamPrepPracticePagination(c *fiber.Ctx) (limit, offset int32, err error) { + if ls := c.Query("limit"); ls != "" { + n, parseErr := strconv.Atoi(ls) + if parseErr != nil || n < 1 { + return 0, 0, fmt.Errorf("limit must be a positive integer") + } + limit = int32(n) + } + if os := c.Query("offset"); os != "" { + n, parseErr := strconv.Atoi(os) + if parseErr != nil || n < 0 { + return 0, 0, fmt.Errorf("offset must be a non-negative integer") + } + offset = int32(n) + } + return limit, offset, nil }