fetch practice pagination fix

This commit is contained in:
Yared Yemane 2026-04-07 03:37:19 -07:00
parent cd8670d5a6
commit 43f79d34ea
6 changed files with 33 additions and 14 deletions

View File

@ -51,6 +51,7 @@ FROM question_set_items qsi
JOIN questions q ON q.id = qsi.question_id JOIN questions q ON q.id = qsi.question_id
LEFT JOIN question_audio_answers qaa ON qaa.question_id = q.id LEFT JOIN question_audio_answers qaa ON qaa.question_id = q.id
WHERE qsi.set_id = $1 WHERE qsi.set_id = $1
AND ($2::VARCHAR IS NULL OR $2 = '' OR q.question_type = $2)
AND q.status != 'ARCHIVED' AND q.status != 'ARCHIVED'
ORDER BY qsi.display_order ORDER BY qsi.display_order
LIMIT sqlc.narg('limit')::INT LIMIT sqlc.narg('limit')::INT

View File

@ -219,16 +219,18 @@ FROM question_set_items qsi
JOIN questions q ON q.id = qsi.question_id JOIN questions q ON q.id = qsi.question_id
LEFT JOIN question_audio_answers qaa ON qaa.question_id = q.id LEFT JOIN question_audio_answers qaa ON qaa.question_id = q.id
WHERE qsi.set_id = $1 WHERE qsi.set_id = $1
AND ($2::VARCHAR IS NULL OR $2 = '' OR q.question_type = $2)
AND q.status != 'ARCHIVED' AND q.status != 'ARCHIVED'
ORDER BY qsi.display_order ORDER BY qsi.display_order
LIMIT $3::INT LIMIT $4::INT
OFFSET $2::INT OFFSET $3::INT
` `
type GetQuestionSetItemsPaginatedParams struct { type GetQuestionSetItemsPaginatedParams struct {
SetID int64 `json:"set_id"` SetID int64 `json:"set_id"`
Offset pgtype.Int4 `json:"offset"` Column2 string `json:"column_2"`
Limit pgtype.Int4 `json:"limit"` Offset pgtype.Int4 `json:"offset"`
Limit pgtype.Int4 `json:"limit"`
} }
type GetQuestionSetItemsPaginatedRow struct { type GetQuestionSetItemsPaginatedRow struct {
@ -251,7 +253,12 @@ type GetQuestionSetItemsPaginatedRow struct {
} }
func (q *Queries) GetQuestionSetItemsPaginated(ctx context.Context, arg GetQuestionSetItemsPaginatedParams) ([]GetQuestionSetItemsPaginatedRow, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -47,7 +47,7 @@ type QuestionStore interface {
// Question Set Items // Question Set Items
AddQuestionToSet(ctx context.Context, setID, questionID int64, displayOrder *int32) (domain.QuestionSetItem, error) AddQuestionToSet(ctx context.Context, setID, questionID int64, displayOrder *int32) (domain.QuestionSetItem, error)
GetQuestionSetItems(ctx context.Context, setID int64) ([]domain.QuestionSetItemWithQuestion, 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) GetPublishedQuestionsInSet(ctx context.Context, setID int64) ([]domain.QuestionSetItemWithQuestion, error)
RemoveQuestionFromSet(ctx context.Context, setID, questionID int64) error RemoveQuestionFromSet(ctx context.Context, setID, questionID int64) error
UpdateQuestionOrder(ctx context.Context, setID, questionID int64, displayOrder int32) error UpdateQuestionOrder(ctx context.Context, setID, questionID int64, displayOrder int32) error

View File

@ -752,11 +752,16 @@ func (s *Store) GetQuestionSetItems(ctx context.Context, setID int64) ([]domain.
return result, nil 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{ rows, err := s.queries.GetQuestionSetItemsPaginated(ctx, dbgen.GetQuestionSetItemsPaginatedParams{
SetID: setID, SetID: setID,
Offset: pgtype.Int4{Int32: offset, Valid: true}, Column2: qType,
Limit: pgtype.Int4{Int32: limit, Valid: true}, Offset: pgtype.Int4{Int32: offset, Valid: true},
Limit: pgtype.Int4{Int32: limit, Valid: true},
}) })
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err

View File

@ -154,8 +154,8 @@ func (s *Service) GetQuestionSetItems(ctx context.Context, setID int64) ([]domai
return s.questionStore.GetQuestionSetItems(ctx, setID) return s.questionStore.GetQuestionSetItems(ctx, setID)
} }
func (s *Service) GetQuestionSetItemsPaginated(ctx context.Context, setID int64, limit, offset int32) ([]domain.QuestionSetItemWithQuestion, int64, error) { func (s *Service) GetQuestionSetItemsPaginated(ctx context.Context, setID int64, questionType *string, limit, offset int32) ([]domain.QuestionSetItemWithQuestion, int64, error) {
return s.questionStore.GetQuestionSetItemsPaginated(ctx, setID, limit, offset) return s.questionStore.GetQuestionSetItemsPaginated(ctx, setID, questionType, limit, offset)
} }
func (s *Service) GetPublishedQuestionsInSet(ctx context.Context, setID int64) ([]domain.QuestionSetItemWithQuestion, error) { func (s *Service) GetPublishedQuestionsInSet(ctx context.Context, setID int64) ([]domain.QuestionSetItemWithQuestion, error) {

View File

@ -1195,6 +1195,7 @@ func (h *Handler) GetQuestionsInSet(c *fiber.Ctx) error {
// @Tags question-set-items // @Tags question-set-items
// @Produce json // @Produce json
// @Param practiceId path int true "Practice(question-set) ID" // @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 limit query int false "Limit" default(10)
// @Param offset query int false "Offset" default(0) // @Param offset query int false "Offset" default(0)
// @Success 200 {object} domain.Response // @Success 200 {object} domain.Response
@ -1239,6 +1240,11 @@ func (h *Handler) GetQuestionsByPractice(c *fiber.Ctx) error {
limit, _ := strconv.Atoi(c.Query("limit", "10")) limit, _ := strconv.Atoi(c.Query("limit", "10"))
offset, _ := strconv.Atoi(c.Query("offset", "0")) offset, _ := strconv.Atoi(c.Query("offset", "0"))
questionType := strings.TrimSpace(c.Query("question_type"))
var qTypePtr *string
if questionType != "" {
qTypePtr = &questionType
}
if limit <= 0 { if limit <= 0 {
limit = 10 limit = 10
} }
@ -1249,7 +1255,7 @@ func (h *Handler) GetQuestionsByPractice(c *fiber.Ctx) error {
offset = 0 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 { if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to get practice questions", Message: "Failed to get practice questions",