From 1d0b0c8b4c5cfc1a56a41817b6f2e31b69b16c61 Mon Sep 17 00:00:00 2001 From: Pasha Date: Tue, 28 Oct 2025 17:36:37 +0300 Subject: [PATCH] added method GetQuestionListByQuizID --- dal/db_query/queries.sql | 3 +++ dal/sqlcgen/queries.sql.go | 43 +++++++++++++++++++++++++++++++++ repository/question/question.go | 35 ++++++++++++++++++++++++--- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 4045a05..47bf5ac 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1676,3 +1676,6 @@ WHERE a.result = true AND s.id IS NULL AND a.deleted = false AND r.deleted = false AND q.deleted = false AND u.deleted = false AND t.active = true AND t.expiration = false; + +-- name: GetQuestionListByQuizID :many +SELECT * from question where quiz_id=$1 and deleted = false; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 9ac8c4a..9eb1d8a 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3367,6 +3367,49 @@ func (q *Queries) GetQuestionListByIDs(ctx context.Context, dollar_1 []int32) ([ return items, nil } +const getQuestionListByQuizID = `-- name: GetQuestionListByQuizID :many +SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at, session, auditory from question where quiz_id=$1 and deleted = false +` + +func (q *Queries) GetQuestionListByQuizID(ctx context.Context, quizID int64) ([]Question, error) { + rows, err := q.db.QueryContext(ctx, getQuestionListByQuizID, quizID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Question + for rows.Next() { + var i Question + if err := rows.Scan( + &i.ID, + &i.QuizID, + &i.Title, + &i.Description, + &i.Questiontype, + &i.Required, + &i.Deleted, + &i.Page, + &i.Content, + &i.Version, + pq.Array(&i.ParentIds), + &i.CreatedAt, + &i.UpdatedAt, + &i.Session, + &i.Auditory, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getQuestionTitle = `-- name: GetQuestionTitle :one SELECT title, questiontype,page FROM question WHERE id = $1 ` diff --git a/repository/question/question.go b/repository/question/question.go index 1e7c66e..d22f935 100644 --- a/repository/question/question.go +++ b/repository/question/question.go @@ -5,13 +5,14 @@ import ( "database/sql" "errors" "fmt" - "gitea.pena/SQuiz/common/dal/sqlcgen" - "gitea.pena/SQuiz/common/model" - "github.com/lib/pq" "sort" "strings" "sync" "time" + + "gitea.pena/SQuiz/common/dal/sqlcgen" + "gitea.pena/SQuiz/common/model" + "github.com/lib/pq" ) type Deps struct { @@ -559,3 +560,31 @@ func (r *QuestionRepository) CheckQuestionOwner(ctx context.Context, accountID s } return accountID == id, nil } + +func (r *QuestionRepository) GetQuestionListByQuizID(ctx context.Context, quizID int64) ([]model.Question, error) { + rows, err := r.queries.GetQuestionListByQuizID(ctx, quizID) + if err != nil { + return nil, err + } + var questions []model.Question + for _, row := range rows { + questions = append(questions, model.Question{ + Id: uint64(row.ID), + QuizId: uint64(row.QuizID), + Title: row.Title, + Description: row.Description.String, + Type: string(row.Questiontype.([]byte)), + Required: row.Required.Bool, + Deleted: row.Deleted.Bool, + Page: int(row.Page.Int16), + Content: row.Content.String, + Version: int(row.Version.Int16), + ParentIds: row.ParentIds, + CreatedAt: row.CreatedAt.Time, + UpdatedAt: row.UpdatedAt.Time, + Auditory: row.Auditory, + Session: row.Session, + }) + } + return questions, nil +}