feat: list practices globally by question type definition
Add GET /questions/type-definitions/:id/practices to return paginated LMS and exam-prep practices whose question sets contain matching questions, including legacy question_type fallback for mapped definitions. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
8dd0c2d784
commit
bc8d36b46f
130
db/query/practices_by_question_type.sql
Normal file
130
db/query/practices_by_question_type.sql
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
-- name: ListPracticesByQuestionTypeDefinition :many
|
||||
WITH matched AS (
|
||||
SELECT
|
||||
'LMS'::text AS practice_kind,
|
||||
p.id AS practice_id,
|
||||
p.question_set_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
p.persona_id,
|
||||
p.quick_tips,
|
||||
p.publish_status,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
NULL::bigint AS exam_prep_lesson_id,
|
||||
p.created_at,
|
||||
p.updated_at,
|
||||
(
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM question_set_items qsi
|
||||
JOIN questions q ON q.id = qsi.question_id
|
||||
WHERE qsi.set_id = p.question_set_id
|
||||
AND q.status != 'ARCHIVED'
|
||||
AND (
|
||||
q.question_type_definition_id = sqlc.arg('definition_id')
|
||||
OR (
|
||||
q.question_type_definition_id IS NULL
|
||||
AND cardinality(sqlc.arg('legacy_runtime_types')::text[]) > 0
|
||||
AND q.question_type = ANY(sqlc.arg('legacy_runtime_types')::text[])
|
||||
)
|
||||
)
|
||||
) AS matching_question_count
|
||||
FROM lms_practices p
|
||||
WHERE (
|
||||
sqlc.arg('published_only')::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::text
|
||||
)
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM question_set_items qsi
|
||||
JOIN questions q ON q.id = qsi.question_id
|
||||
WHERE qsi.set_id = p.question_set_id
|
||||
AND q.status != 'ARCHIVED'
|
||||
AND (
|
||||
q.question_type_definition_id = sqlc.arg('definition_id')
|
||||
OR (
|
||||
q.question_type_definition_id IS NULL
|
||||
AND cardinality(sqlc.arg('legacy_runtime_types')::text[]) > 0
|
||||
AND q.question_type = ANY(sqlc.arg('legacy_runtime_types')::text[])
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
'EXAM_PREP'::text AS practice_kind,
|
||||
p.id AS practice_id,
|
||||
p.question_set_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
p.persona_id,
|
||||
p.quick_tips,
|
||||
p.publish_status,
|
||||
NULL::bigint AS course_id,
|
||||
NULL::bigint AS module_id,
|
||||
NULL::bigint AS lesson_id,
|
||||
p.unit_module_lesson_id AS exam_prep_lesson_id,
|
||||
p.created_at,
|
||||
p.updated_at,
|
||||
(
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM question_set_items qsi
|
||||
JOIN questions q ON q.id = qsi.question_id
|
||||
WHERE qsi.set_id = p.question_set_id
|
||||
AND q.status != 'ARCHIVED'
|
||||
AND (
|
||||
q.question_type_definition_id = sqlc.arg('definition_id')
|
||||
OR (
|
||||
q.question_type_definition_id IS NULL
|
||||
AND cardinality(sqlc.arg('legacy_runtime_types')::text[]) > 0
|
||||
AND q.question_type = ANY(sqlc.arg('legacy_runtime_types')::text[])
|
||||
)
|
||||
)
|
||||
) AS matching_question_count
|
||||
FROM exam_prep.lesson_practices p
|
||||
WHERE (
|
||||
sqlc.arg('published_only')::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::text
|
||||
)
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM question_set_items qsi
|
||||
JOIN questions q ON q.id = qsi.question_id
|
||||
WHERE qsi.set_id = p.question_set_id
|
||||
AND q.status != 'ARCHIVED'
|
||||
AND (
|
||||
q.question_type_definition_id = sqlc.arg('definition_id')
|
||||
OR (
|
||||
q.question_type_definition_id IS NULL
|
||||
AND cardinality(sqlc.arg('legacy_runtime_types')::text[]) > 0
|
||||
AND q.question_type = ANY(sqlc.arg('legacy_runtime_types')::text[])
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
practice_kind,
|
||||
practice_id,
|
||||
question_set_id,
|
||||
title,
|
||||
story_description,
|
||||
story_image,
|
||||
persona_id,
|
||||
quick_tips,
|
||||
publish_status,
|
||||
course_id,
|
||||
module_id,
|
||||
lesson_id,
|
||||
exam_prep_lesson_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
matching_question_count
|
||||
FROM matched
|
||||
ORDER BY created_at DESC
|
||||
LIMIT sqlc.arg('limit')::int
|
||||
OFFSET sqlc.arg('offset')::int;
|
||||
216
gen/db/practices_by_question_type.sql.go
Normal file
216
gen/db/practices_by_question_type.sql.go
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// source: practices_by_question_type.sql
|
||||
|
||||
package dbgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const ListPracticesByQuestionTypeDefinition = `-- name: ListPracticesByQuestionTypeDefinition :many
|
||||
WITH matched AS (
|
||||
SELECT
|
||||
'LMS'::text AS practice_kind,
|
||||
p.id AS practice_id,
|
||||
p.question_set_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
p.persona_id,
|
||||
p.quick_tips,
|
||||
p.publish_status,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
NULL::bigint AS exam_prep_lesson_id,
|
||||
p.created_at,
|
||||
p.updated_at,
|
||||
(
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM question_set_items qsi
|
||||
JOIN questions q ON q.id = qsi.question_id
|
||||
WHERE qsi.set_id = p.question_set_id
|
||||
AND q.status != 'ARCHIVED'
|
||||
AND (
|
||||
q.question_type_definition_id = $3
|
||||
OR (
|
||||
q.question_type_definition_id IS NULL
|
||||
AND cardinality($4::text[]) > 0
|
||||
AND q.question_type = ANY($4::text[])
|
||||
)
|
||||
)
|
||||
) AS matching_question_count
|
||||
FROM lms_practices p
|
||||
WHERE (
|
||||
$5::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::text
|
||||
)
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM question_set_items qsi
|
||||
JOIN questions q ON q.id = qsi.question_id
|
||||
WHERE qsi.set_id = p.question_set_id
|
||||
AND q.status != 'ARCHIVED'
|
||||
AND (
|
||||
q.question_type_definition_id = $3
|
||||
OR (
|
||||
q.question_type_definition_id IS NULL
|
||||
AND cardinality($4::text[]) > 0
|
||||
AND q.question_type = ANY($4::text[])
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
'EXAM_PREP'::text AS practice_kind,
|
||||
p.id AS practice_id,
|
||||
p.question_set_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
p.persona_id,
|
||||
p.quick_tips,
|
||||
p.publish_status,
|
||||
NULL::bigint AS course_id,
|
||||
NULL::bigint AS module_id,
|
||||
NULL::bigint AS lesson_id,
|
||||
p.unit_module_lesson_id AS exam_prep_lesson_id,
|
||||
p.created_at,
|
||||
p.updated_at,
|
||||
(
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM question_set_items qsi
|
||||
JOIN questions q ON q.id = qsi.question_id
|
||||
WHERE qsi.set_id = p.question_set_id
|
||||
AND q.status != 'ARCHIVED'
|
||||
AND (
|
||||
q.question_type_definition_id = $3
|
||||
OR (
|
||||
q.question_type_definition_id IS NULL
|
||||
AND cardinality($4::text[]) > 0
|
||||
AND q.question_type = ANY($4::text[])
|
||||
)
|
||||
)
|
||||
) AS matching_question_count
|
||||
FROM exam_prep.lesson_practices p
|
||||
WHERE (
|
||||
$5::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::text
|
||||
)
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM question_set_items qsi
|
||||
JOIN questions q ON q.id = qsi.question_id
|
||||
WHERE qsi.set_id = p.question_set_id
|
||||
AND q.status != 'ARCHIVED'
|
||||
AND (
|
||||
q.question_type_definition_id = $3
|
||||
OR (
|
||||
q.question_type_definition_id IS NULL
|
||||
AND cardinality($4::text[]) > 0
|
||||
AND q.question_type = ANY($4::text[])
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
practice_kind,
|
||||
practice_id,
|
||||
question_set_id,
|
||||
title,
|
||||
story_description,
|
||||
story_image,
|
||||
persona_id,
|
||||
quick_tips,
|
||||
publish_status,
|
||||
course_id,
|
||||
module_id,
|
||||
lesson_id,
|
||||
exam_prep_lesson_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
matching_question_count
|
||||
FROM matched
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $2::int
|
||||
OFFSET $1::int
|
||||
`
|
||||
|
||||
type ListPracticesByQuestionTypeDefinitionParams struct {
|
||||
Offset int32 `json:"offset"`
|
||||
Limit int32 `json:"limit"`
|
||||
DefinitionID pgtype.Int8 `json:"definition_id"`
|
||||
LegacyRuntimeTypes []string `json:"legacy_runtime_types"`
|
||||
PublishedOnly bool `json:"published_only"`
|
||||
}
|
||||
|
||||
type ListPracticesByQuestionTypeDefinitionRow struct {
|
||||
TotalCount int64 `json:"total_count"`
|
||||
PracticeKind string `json:"practice_kind"`
|
||||
PracticeID int64 `json:"practice_id"`
|
||||
QuestionSetID int64 `json:"question_set_id"`
|
||||
Title string `json:"title"`
|
||||
StoryDescription pgtype.Text `json:"story_description"`
|
||||
StoryImage pgtype.Text `json:"story_image"`
|
||||
PersonaID pgtype.Int8 `json:"persona_id"`
|
||||
QuickTips pgtype.Text `json:"quick_tips"`
|
||||
PublishStatus string `json:"publish_status"`
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
ExamPrepLessonID pgtype.Int8 `json:"exam_prep_lesson_id"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
MatchingQuestionCount int64 `json:"matching_question_count"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListPracticesByQuestionTypeDefinition(ctx context.Context, arg ListPracticesByQuestionTypeDefinitionParams) ([]ListPracticesByQuestionTypeDefinitionRow, error) {
|
||||
rows, err := q.db.Query(ctx, ListPracticesByQuestionTypeDefinition,
|
||||
arg.Offset,
|
||||
arg.Limit,
|
||||
arg.DefinitionID,
|
||||
arg.LegacyRuntimeTypes,
|
||||
arg.PublishedOnly,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListPracticesByQuestionTypeDefinitionRow
|
||||
for rows.Next() {
|
||||
var i ListPracticesByQuestionTypeDefinitionRow
|
||||
if err := rows.Scan(
|
||||
&i.TotalCount,
|
||||
&i.PracticeKind,
|
||||
&i.PracticeID,
|
||||
&i.QuestionSetID,
|
||||
&i.Title,
|
||||
&i.StoryDescription,
|
||||
&i.StoryImage,
|
||||
&i.PersonaID,
|
||||
&i.QuickTips,
|
||||
&i.PublishStatus,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
&i.ExamPrepLessonID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.MatchingQuestionCount,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
32
internal/domain/practice_by_question_type.go
Normal file
32
internal/domain/practice_by_question_type.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
// PracticeByQuestionTypeDefinition is a practice shell (LMS or exam-prep) that contains at least one
|
||||
// question matching the requested question type definition.
|
||||
type PracticeByQuestionTypeDefinition struct {
|
||||
PracticeKind string `json:"practice_kind"`
|
||||
PracticeID int64 `json:"practice_id"`
|
||||
QuestionSetID int64 `json:"question_set_id"`
|
||||
Title string `json:"title"`
|
||||
StoryDescription *string `json:"story_description,omitempty"`
|
||||
StoryImage *string `json:"story_image,omitempty"`
|
||||
PersonaID *int64 `json:"persona_id,omitempty"`
|
||||
QuickTips *string `json:"quick_tips,omitempty"`
|
||||
PublishStatus PracticePublishStatus `json:"publish_status"`
|
||||
ParentKind *ParentKind `json:"parent_kind,omitempty"`
|
||||
ParentID *int64 `json:"parent_id,omitempty"`
|
||||
ExamPrepLessonID *int64 `json:"exam_prep_lesson_id,omitempty"`
|
||||
MatchingQuestionCount int64 `json:"matching_question_count"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
// PracticesByQuestionTypeDefinitionSummary is the list payload for global practice lookup.
|
||||
type PracticesByQuestionTypeDefinitionSummary struct {
|
||||
QuestionTypeDefinitionID int64 `json:"question_type_definition_id"`
|
||||
Practices []PracticeByQuestionTypeDefinition `json:"practices"`
|
||||
TotalCount int64 `json:"total_count"`
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
}
|
||||
|
|
@ -428,6 +428,16 @@ func ResolveRuntimeQuestionTypeFromDefinition(key string, responseKinds []string
|
|||
return ""
|
||||
}
|
||||
|
||||
// LegacyRuntimeTypesForDefinitionLookup returns legacy questions.question_type values that should
|
||||
// match a definition when question_type_definition_id is unset. DYNAMIC is excluded because it is ambiguous.
|
||||
func LegacyRuntimeTypesForDefinitionLookup(def QuestionTypeDefinition) []string {
|
||||
runtime := ResolveRuntimeQuestionTypeFromDefinition(def.Key, def.ResponseComponentKinds)
|
||||
if runtime == "" || runtime == "DYNAMIC" {
|
||||
return nil
|
||||
}
|
||||
return []string{runtime}
|
||||
}
|
||||
|
||||
// ValidatePersistableQuestionTypeDefinition ensures definitions that pass component-level validation
|
||||
// are also mappable to the currently supported runtime question_type set.
|
||||
func ValidatePersistableQuestionTypeDefinition(key string, responseKinds []string) error {
|
||||
|
|
|
|||
|
|
@ -338,3 +338,20 @@ func TestSummarizeQuestionSetQuestionTypes_mergesLegacyAndLinked(t *testing.T) {
|
|||
t.Fatalf("unexpected summary: %+v", summary[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyRuntimeTypesForDefinitionLookup(t *testing.T) {
|
||||
def := QuestionTypeDefinition{
|
||||
ID: 5,
|
||||
Key: "multiple_choice",
|
||||
ResponseComponentKinds: []string{string(ResponseMultipleChoice)},
|
||||
}
|
||||
got := LegacyRuntimeTypesForDefinitionLookup(def)
|
||||
if len(got) != 1 || got[0] != "MCQ" {
|
||||
t.Fatalf("expected [MCQ], got %#v", got)
|
||||
}
|
||||
|
||||
unknown := QuestionTypeDefinition{Key: "unsupported_kind", ResponseComponentKinds: []string{"UNKNOWN_KIND"}}
|
||||
if legacy := LegacyRuntimeTypesForDefinitionLookup(unknown); legacy != nil {
|
||||
t.Fatalf("expected nil legacy types when runtime mapping is empty, got %#v", legacy)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ type QuestionStore interface {
|
|||
CountQuestionsInSet(ctx context.Context, setID int64) (int64, error)
|
||||
GetQuestionTypeCountsInSet(ctx context.Context, setID int64) ([]domain.QuestionSetQuestionTypeGroup, error)
|
||||
GetQuestionSetsContainingQuestion(ctx context.Context, questionID int64) ([]domain.QuestionSet, error)
|
||||
ListPracticesByQuestionTypeDefinition(ctx context.Context, definitionID int64, legacyRuntimeTypes []string, publishedOnly bool, limit, offset int32) ([]domain.PracticeByQuestionTypeDefinition, int64, error)
|
||||
|
||||
// User Personas in Question Sets
|
||||
AddUserPersonaToQuestionSet(ctx context.Context, questionSetID, userID int64, displayOrder int32) error
|
||||
|
|
|
|||
|
|
@ -1315,3 +1315,80 @@ func (s *Store) ReorderQuestionSets(ctx context.Context, ids []int64, positions
|
|||
Positions: positions,
|
||||
})
|
||||
}
|
||||
|
||||
func practiceByQuestionTypeRowToDomain(r dbgen.ListPracticesByQuestionTypeDefinitionRow) domain.PracticeByQuestionTypeDefinition {
|
||||
out := domain.PracticeByQuestionTypeDefinition{
|
||||
PracticeKind: r.PracticeKind,
|
||||
PracticeID: r.PracticeID,
|
||||
QuestionSetID: r.QuestionSetID,
|
||||
Title: r.Title,
|
||||
StoryDescription: fromPgText(r.StoryDescription),
|
||||
StoryImage: fromPgText(r.StoryImage),
|
||||
PersonaID: fromPgInt8(r.PersonaID),
|
||||
QuickTips: fromPgText(r.QuickTips),
|
||||
PublishStatus: domain.PracticePublishStatusFromDB(r.PublishStatus),
|
||||
MatchingQuestionCount: r.MatchingQuestionCount,
|
||||
CreatedAt: r.CreatedAt.Time,
|
||||
UpdatedAt: timePtr(r.UpdatedAt),
|
||||
}
|
||||
switch r.PracticeKind {
|
||||
case "LMS":
|
||||
switch {
|
||||
case r.CourseID.Valid:
|
||||
kind := domain.ParentKindCourse
|
||||
id := r.CourseID.Int64
|
||||
out.ParentKind = &kind
|
||||
out.ParentID = &id
|
||||
case r.ModuleID.Valid:
|
||||
kind := domain.ParentKindModule
|
||||
id := r.ModuleID.Int64
|
||||
out.ParentKind = &kind
|
||||
out.ParentID = &id
|
||||
case r.LessonID.Valid:
|
||||
kind := domain.ParentKindLesson
|
||||
id := r.LessonID.Int64
|
||||
out.ParentKind = &kind
|
||||
out.ParentID = &id
|
||||
}
|
||||
case "EXAM_PREP":
|
||||
if r.ExamPrepLessonID.Valid {
|
||||
id := r.ExamPrepLessonID.Int64
|
||||
out.ExamPrepLessonID = &id
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Store) ListPracticesByQuestionTypeDefinition(
|
||||
ctx context.Context,
|
||||
definitionID int64,
|
||||
legacyRuntimeTypes []string,
|
||||
publishedOnly bool,
|
||||
limit, offset int32,
|
||||
) ([]domain.PracticeByQuestionTypeDefinition, int64, error) {
|
||||
if legacyRuntimeTypes == nil {
|
||||
legacyRuntimeTypes = []string{}
|
||||
}
|
||||
rows, err := s.queries.ListPracticesByQuestionTypeDefinition(ctx, dbgen.ListPracticesByQuestionTypeDefinitionParams{
|
||||
Offset: offset,
|
||||
Limit: limit,
|
||||
DefinitionID: pgtype.Int8{Int64: definitionID, Valid: true},
|
||||
LegacyRuntimeTypes: legacyRuntimeTypes,
|
||||
PublishedOnly: publishedOnly,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return []domain.PracticeByQuestionTypeDefinition{}, 0, nil
|
||||
}
|
||||
out := make([]domain.PracticeByQuestionTypeDefinition, len(rows))
|
||||
var total int64
|
||||
for i, row := range rows {
|
||||
if i == 0 {
|
||||
total = row.TotalCount
|
||||
}
|
||||
out[i] = practiceByQuestionTypeRowToDomain(row)
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -303,6 +303,48 @@ func (s *Service) GetQuestionSetsContainingQuestion(ctx context.Context, questio
|
|||
return s.questionStore.GetQuestionSetsContainingQuestion(ctx, questionID)
|
||||
}
|
||||
|
||||
func clampPracticeSearchPage(limit, offset int32) (int32, int32) {
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
if limit > 200 {
|
||||
limit = 200
|
||||
}
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
return limit, offset
|
||||
}
|
||||
|
||||
func (s *Service) ListPracticesByQuestionTypeDefinition(
|
||||
ctx context.Context,
|
||||
definitionID int64,
|
||||
publishedOnly bool,
|
||||
limit, offset int32,
|
||||
) (domain.PracticesByQuestionTypeDefinitionSummary, error) {
|
||||
var empty domain.PracticesByQuestionTypeDefinitionSummary
|
||||
def, err := s.questionStore.GetQuestionTypeDefinitionByID(ctx, definitionID)
|
||||
if err != nil {
|
||||
return empty, err
|
||||
}
|
||||
limit, offset = clampPracticeSearchPage(limit, offset)
|
||||
legacyTypes := domain.LegacyRuntimeTypesForDefinitionLookup(def)
|
||||
practices, total, err := s.questionStore.ListPracticesByQuestionTypeDefinition(ctx, definitionID, legacyTypes, publishedOnly, limit, offset)
|
||||
if err != nil {
|
||||
return empty, err
|
||||
}
|
||||
if practices == nil {
|
||||
practices = []domain.PracticeByQuestionTypeDefinition{}
|
||||
}
|
||||
return domain.PracticesByQuestionTypeDefinitionSummary{
|
||||
QuestionTypeDefinitionID: definitionID,
|
||||
Practices: practices,
|
||||
TotalCount: total,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// User Personas in Question Sets
|
||||
|
||||
func (s *Service) AddUserPersonaToQuestionSet(ctx context.Context, questionSetID, userID int64, displayOrder int32) error {
|
||||
|
|
|
|||
|
|
@ -4,11 +4,13 @@ import (
|
|||
"Yimaru-Backend/internal/domain"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type componentCatalogRes struct {
|
||||
|
|
@ -260,6 +262,54 @@ func (h *Handler) GetQuestionTypeDefinitionByID(c *fiber.Ctx) error {
|
|||
})
|
||||
}
|
||||
|
||||
// ListPracticesByQuestionTypeDefinition godoc
|
||||
// @Summary List practices globally by question type definition
|
||||
// @Description Returns LMS and exam-prep practices whose linked question set contains at least one non-archived question matching the definition (by question_type_definition_id or legacy question_type).
|
||||
// @Tags questions
|
||||
// @Produce json
|
||||
// @Param id path int true "Question type definition id"
|
||||
// @Param limit query int false "Limit" default(20)
|
||||
// @Param offset query int false "Offset" default(0)
|
||||
// @Success 200 {object} domain.Response
|
||||
// @Failure 400 {object} domain.ErrorResponse
|
||||
// @Failure 404 {object} domain.ErrorResponse
|
||||
// @Router /api/v1/questions/type-definitions/{id}/practices [get]
|
||||
func (h *Handler) ListPracticesByQuestionTypeDefinition(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid id",
|
||||
Error: "id must be a positive integer",
|
||||
})
|
||||
}
|
||||
|
||||
limit, _ := strconv.Atoi(c.Query("limit", "20"))
|
||||
offset, _ := strconv.Atoi(c.Query("offset", "0"))
|
||||
role, _ := c.Locals("role").(domain.Role)
|
||||
publishedOnly := role.IsCustomerLearnerRole() || !h.canManageLMSPractices(c)
|
||||
|
||||
summary, err := h.questionsSvc.ListPracticesByQuestionTypeDefinition(c.Context(), id, publishedOnly, int32(limit), int32(offset))
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
|
||||
Message: "Question type definition not found",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||
Message: "Failed to list practices",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(domain.Response{
|
||||
Message: "Practices retrieved successfully",
|
||||
Data: summary,
|
||||
Success: true,
|
||||
StatusCode: fiber.StatusOK,
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateQuestionTypeDefinition godoc
|
||||
// @Summary Update reusable question-type definition
|
||||
// @Description Updates a reusable dynamic question-type definition. Updated definitions must remain runtime-mappable.
|
||||
|
|
|
|||
|
|
@ -196,6 +196,7 @@ func (a *App) initAppRoutes() {
|
|||
groupV1.Post("/questions/type-definitions", a.authMiddleware, a.RequirePermission("questions.create"), h.CreateQuestionTypeDefinition)
|
||||
groupV1.Get("/questions/type-definitions", a.authMiddleware, a.RequirePermission("questions.list"), h.ListQuestionTypeDefinitions)
|
||||
groupV1.Get("/questions/type-definitions/:id", a.authMiddleware, a.RequirePermission("questions.get"), h.GetQuestionTypeDefinitionByID)
|
||||
groupV1.Get("/questions/type-definitions/:id/practices", a.authMiddleware, a.RequirePermission("practices.list"), h.ListPracticesByQuestionTypeDefinition)
|
||||
groupV1.Put("/questions/type-definitions/:id", a.authMiddleware, a.RequirePermission("questions.update"), h.UpdateQuestionTypeDefinition)
|
||||
groupV1.Delete("/questions/type-definitions/:id", a.authMiddleware, a.RequirePermission("questions.delete"), h.DeleteQuestionTypeDefinition)
|
||||
groupV1.Get("/questions/:id", a.authMiddleware, a.RequirePermission("questions.get"), h.GetQuestionByID)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user