From 43f79d34ea23cd6932c1e9f2ac94a44f7c18f47d Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Tue, 7 Apr 2026 03:37:19 -0700 Subject: [PATCH] fetch practice pagination fix --- db/query/question_set_items.sql | 1 + gen/db/question_set_items.sql.go | 19 +++++++++++++------ internal/ports/questions.go | 2 +- internal/repository/questions.go | 13 +++++++++---- internal/services/questions/service.go | 4 ++-- internal/web_server/handlers/questions.go | 8 +++++++- 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/db/query/question_set_items.sql b/db/query/question_set_items.sql index 448dd2a..69ef103 100644 --- a/db/query/question_set_items.sql +++ b/db/query/question_set_items.sql @@ -51,6 +51,7 @@ FROM question_set_items qsi JOIN questions q ON q.id = qsi.question_id LEFT JOIN question_audio_answers qaa ON qaa.question_id = q.id WHERE qsi.set_id = $1 + AND ($2::VARCHAR IS NULL OR $2 = '' OR q.question_type = $2) AND q.status != 'ARCHIVED' ORDER BY qsi.display_order LIMIT sqlc.narg('limit')::INT diff --git a/gen/db/question_set_items.sql.go b/gen/db/question_set_items.sql.go index 71d08da..eafc332 100644 --- a/gen/db/question_set_items.sql.go +++ b/gen/db/question_set_items.sql.go @@ -219,16 +219,18 @@ FROM question_set_items qsi JOIN questions q ON q.id = qsi.question_id LEFT JOIN question_audio_answers qaa ON qaa.question_id = q.id WHERE qsi.set_id = $1 + AND ($2::VARCHAR IS NULL OR $2 = '' OR q.question_type = $2) AND q.status != 'ARCHIVED' ORDER BY qsi.display_order -LIMIT $3::INT -OFFSET $2::INT +LIMIT $4::INT +OFFSET $3::INT ` type GetQuestionSetItemsPaginatedParams struct { - SetID int64 `json:"set_id"` - Offset pgtype.Int4 `json:"offset"` - Limit pgtype.Int4 `json:"limit"` + SetID int64 `json:"set_id"` + Column2 string `json:"column_2"` + Offset pgtype.Int4 `json:"offset"` + Limit pgtype.Int4 `json:"limit"` } type GetQuestionSetItemsPaginatedRow struct { @@ -251,7 +253,12 @@ type GetQuestionSetItemsPaginatedRow struct { } func (q *Queries) GetQuestionSetItemsPaginated(ctx context.Context, arg GetQuestionSetItemsPaginatedParams) ([]GetQuestionSetItemsPaginatedRow, error) { - rows, err := q.db.Query(ctx, GetQuestionSetItemsPaginated, arg.SetID, arg.Offset, arg.Limit) + rows, err := q.db.Query(ctx, GetQuestionSetItemsPaginated, + arg.SetID, + arg.Column2, + arg.Offset, + arg.Limit, + ) if err != nil { return nil, err } diff --git a/internal/ports/questions.go b/internal/ports/questions.go index 6c7f1ee..b1ba944 100644 --- a/internal/ports/questions.go +++ b/internal/ports/questions.go @@ -47,7 +47,7 @@ type QuestionStore interface { // Question Set Items AddQuestionToSet(ctx context.Context, setID, questionID int64, displayOrder *int32) (domain.QuestionSetItem, error) GetQuestionSetItems(ctx context.Context, setID int64) ([]domain.QuestionSetItemWithQuestion, error) - GetQuestionSetItemsPaginated(ctx context.Context, setID int64, limit, offset int32) ([]domain.QuestionSetItemWithQuestion, int64, error) + GetQuestionSetItemsPaginated(ctx context.Context, setID int64, questionType *string, limit, offset int32) ([]domain.QuestionSetItemWithQuestion, int64, error) GetPublishedQuestionsInSet(ctx context.Context, setID int64) ([]domain.QuestionSetItemWithQuestion, error) RemoveQuestionFromSet(ctx context.Context, setID, questionID int64) error UpdateQuestionOrder(ctx context.Context, setID, questionID int64, displayOrder int32) error diff --git a/internal/repository/questions.go b/internal/repository/questions.go index d5b118b..1bf7ccf 100644 --- a/internal/repository/questions.go +++ b/internal/repository/questions.go @@ -752,11 +752,16 @@ func (s *Store) GetQuestionSetItems(ctx context.Context, setID int64) ([]domain. return result, nil } -func (s *Store) GetQuestionSetItemsPaginated(ctx context.Context, setID int64, limit, offset int32) ([]domain.QuestionSetItemWithQuestion, int64, error) { +func (s *Store) GetQuestionSetItemsPaginated(ctx context.Context, setID int64, questionType *string, limit, offset int32) ([]domain.QuestionSetItemWithQuestion, int64, error) { + qType := "" + if questionType != nil { + qType = *questionType + } rows, err := s.queries.GetQuestionSetItemsPaginated(ctx, dbgen.GetQuestionSetItemsPaginatedParams{ - SetID: setID, - Offset: pgtype.Int4{Int32: offset, Valid: true}, - Limit: pgtype.Int4{Int32: limit, Valid: true}, + SetID: setID, + Column2: qType, + Offset: pgtype.Int4{Int32: offset, Valid: true}, + Limit: pgtype.Int4{Int32: limit, Valid: true}, }) if err != nil { return nil, 0, err diff --git a/internal/services/questions/service.go b/internal/services/questions/service.go index c813d41..0d54b80 100644 --- a/internal/services/questions/service.go +++ b/internal/services/questions/service.go @@ -154,8 +154,8 @@ func (s *Service) GetQuestionSetItems(ctx context.Context, setID int64) ([]domai return s.questionStore.GetQuestionSetItems(ctx, setID) } -func (s *Service) GetQuestionSetItemsPaginated(ctx context.Context, setID int64, limit, offset int32) ([]domain.QuestionSetItemWithQuestion, int64, error) { - return s.questionStore.GetQuestionSetItemsPaginated(ctx, setID, limit, offset) +func (s *Service) GetQuestionSetItemsPaginated(ctx context.Context, setID int64, questionType *string, limit, offset int32) ([]domain.QuestionSetItemWithQuestion, int64, error) { + return s.questionStore.GetQuestionSetItemsPaginated(ctx, setID, questionType, limit, offset) } func (s *Service) GetPublishedQuestionsInSet(ctx context.Context, setID int64) ([]domain.QuestionSetItemWithQuestion, error) { diff --git a/internal/web_server/handlers/questions.go b/internal/web_server/handlers/questions.go index f1da2ed..110af42 100644 --- a/internal/web_server/handlers/questions.go +++ b/internal/web_server/handlers/questions.go @@ -1195,6 +1195,7 @@ func (h *Handler) GetQuestionsInSet(c *fiber.Ctx) error { // @Tags question-set-items // @Produce json // @Param practiceId path int true "Practice(question-set) ID" +// @Param question_type query string false "Question type filter (e.g. AUDIO)" // @Param limit query int false "Limit" default(10) // @Param offset query int false "Offset" default(0) // @Success 200 {object} domain.Response @@ -1239,6 +1240,11 @@ func (h *Handler) GetQuestionsByPractice(c *fiber.Ctx) error { limit, _ := strconv.Atoi(c.Query("limit", "10")) offset, _ := strconv.Atoi(c.Query("offset", "0")) + questionType := strings.TrimSpace(c.Query("question_type")) + var qTypePtr *string + if questionType != "" { + qTypePtr = &questionType + } if limit <= 0 { limit = 10 } @@ -1249,7 +1255,7 @@ func (h *Handler) GetQuestionsByPractice(c *fiber.Ctx) error { offset = 0 } - items, totalCount, err := h.questionsSvc.GetQuestionSetItemsPaginated(c.Context(), practiceID, int32(limit), int32(offset)) + items, totalCount, err := h.questionsSvc.GetQuestionSetItemsPaginated(c.Context(), practiceID, qTypePtr, int32(limit), int32(offset)) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ Message: "Failed to get practice questions",