From 8c116f4a0be6b0250a993f4180f6d369247fbe3b Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Tue, 28 Apr 2026 09:41:09 -0700 Subject: [PATCH] GET question sets API fix --- internal/repository/questions.go | 13 ++++++ internal/web_server/handlers/questions.go | 57 ++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/internal/repository/questions.go b/internal/repository/questions.go index baf864c..6a7c582 100644 --- a/internal/repository/questions.go +++ b/internal/repository/questions.go @@ -607,6 +607,19 @@ func (s *Store) GetQuestionSetsByType(ctx context.Context, setType string, limit UpdatedAt: timePtr(r.UpdatedAt), } } + + // COUNT(*) OVER() only appears when at least one row is returned. + // For out-of-range offsets, fetch total count explicitly so pagination metadata stays correct. + if len(rows) == 0 { + err = s.conn.QueryRow( + ctx, + `SELECT COUNT(*) FROM question_sets WHERE set_type = $1 AND status != 'ARCHIVED'`, + setType, + ).Scan(&totalCount) + if err != nil { + return nil, 0, err + } + } return result, totalCount, nil } diff --git a/internal/web_server/handlers/questions.go b/internal/web_server/handlers/questions.go index e9f5ae4..dce2e57 100644 --- a/internal/web_server/handlers/questions.go +++ b/internal/web_server/handlers/questions.go @@ -1120,11 +1120,64 @@ func (h *Handler) GetQuestionsInSet(c *fiber.Ctx) error { }) } - itemResponses := questionSetItemsToRes(items) + questionResponses := make([]questionRes, 0, len(items)) + for _, item := range items { + question, err := h.questionsSvc.GetQuestionWithDetails(c.Context(), item.QuestionID) + if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{ + Message: "Failed to get question details", + Error: err.Error(), + }) + } + + options := make([]optionRes, 0, len(question.Options)) + for _, opt := range question.Options { + options = append(options, optionRes{ + ID: opt.ID, + OptionText: opt.OptionText, + OptionOrder: opt.OptionOrder, + IsCorrect: opt.IsCorrect, + }) + } + + shortAnswers := make([]shortAnswerRes, 0, len(question.ShortAnswers)) + for _, sa := range question.ShortAnswers { + shortAnswers = append(shortAnswers, shortAnswerRes{ + ID: sa.ID, + AcceptableAnswer: sa.AcceptableAnswer, + MatchType: sa.MatchType, + }) + } + + var audioCorrectAnswerText *string + if question.AudioAnswer != nil { + audioCorrectAnswerText = &question.AudioAnswer.CorrectAnswerText + } + + questionResponses = append(questionResponses, questionRes{ + ID: question.ID, + QuestionText: question.QuestionText, + QuestionType: question.QuestionType, + DifficultyLevel: question.DifficultyLevel, + Points: question.Points, + Explanation: question.Explanation, + Tips: question.Tips, + VoicePrompt: question.VoicePrompt, + SampleAnswerVoicePrompt: question.SampleAnswerVoicePrompt, + ImageURL: question.ImageURL, + Status: question.Status, + CreatedAt: question.CreatedAt.String(), + Options: options, + ShortAnswers: shortAnswers, + AudioCorrectAnswerText: audioCorrectAnswerText, + }) + } return c.JSON(domain.Response{ Message: "Questions retrieved successfully", - Data: itemResponses, + Success: true, + StatusCode: fiber.StatusOK, + Data: questionResponses, }) }