Include nested content counts in exam-prep catalog list response.
Add units, modules, and lessons aggregate counts per catalog course so clients can render hierarchy depth without extra API calls. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
10954d88b0
commit
4124f98160
|
|
@ -17,6 +17,17 @@ FROM exam_prep.catalog_courses
|
|||
WHERE id = $1;
|
||||
|
||||
-- name: ExamPrepListCatalogCourses :many
|
||||
WITH catalog_course_counts AS (
|
||||
SELECT
|
||||
u.catalog_course_id,
|
||||
COUNT(DISTINCT u.id)::BIGINT AS units_count,
|
||||
COUNT(DISTINCT m.id)::BIGINT AS modules_count,
|
||||
COUNT(DISTINCT l.id)::BIGINT AS lessons_count
|
||||
FROM exam_prep.units u
|
||||
LEFT JOIN exam_prep.unit_modules m ON m.unit_id = u.id
|
||||
LEFT JOIN exam_prep.unit_module_lessons l ON l.unit_module_id = m.id
|
||||
GROUP BY u.catalog_course_id
|
||||
)
|
||||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
c.id,
|
||||
|
|
@ -24,9 +35,13 @@ SELECT
|
|||
c.description,
|
||||
c.thumbnail,
|
||||
c.sort_order,
|
||||
COALESCE(cc.units_count, 0)::BIGINT AS units_count,
|
||||
COALESCE(cc.modules_count, 0)::BIGINT AS modules_count,
|
||||
COALESCE(cc.lessons_count, 0)::BIGINT AS lessons_count,
|
||||
c.created_at,
|
||||
c.updated_at
|
||||
FROM exam_prep.catalog_courses c
|
||||
LEFT JOIN catalog_course_counts cc ON cc.catalog_course_id = c.id
|
||||
ORDER BY c.sort_order ASC, c.id ASC
|
||||
LIMIT $1 OFFSET $2;
|
||||
|
||||
|
|
|
|||
|
|
@ -105,6 +105,17 @@ func (q *Queries) ExamPrepListAllCatalogCourseIDs(ctx context.Context) ([]int64,
|
|||
}
|
||||
|
||||
const ExamPrepListCatalogCourses = `-- name: ExamPrepListCatalogCourses :many
|
||||
WITH catalog_course_counts AS (
|
||||
SELECT
|
||||
u.catalog_course_id,
|
||||
COUNT(DISTINCT u.id)::BIGINT AS units_count,
|
||||
COUNT(DISTINCT m.id)::BIGINT AS modules_count,
|
||||
COUNT(DISTINCT l.id)::BIGINT AS lessons_count
|
||||
FROM exam_prep.units u
|
||||
LEFT JOIN exam_prep.unit_modules m ON m.unit_id = u.id
|
||||
LEFT JOIN exam_prep.unit_module_lessons l ON l.unit_module_id = m.id
|
||||
GROUP BY u.catalog_course_id
|
||||
)
|
||||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
c.id,
|
||||
|
|
@ -112,9 +123,13 @@ SELECT
|
|||
c.description,
|
||||
c.thumbnail,
|
||||
c.sort_order,
|
||||
COALESCE(cc.units_count, 0)::BIGINT AS units_count,
|
||||
COALESCE(cc.modules_count, 0)::BIGINT AS modules_count,
|
||||
COALESCE(cc.lessons_count, 0)::BIGINT AS lessons_count,
|
||||
c.created_at,
|
||||
c.updated_at
|
||||
FROM exam_prep.catalog_courses c
|
||||
LEFT JOIN catalog_course_counts cc ON cc.catalog_course_id = c.id
|
||||
ORDER BY c.sort_order ASC, c.id ASC
|
||||
LIMIT $1 OFFSET $2
|
||||
`
|
||||
|
|
@ -131,6 +146,9 @@ type ExamPrepListCatalogCoursesRow struct {
|
|||
Description pgtype.Text `json:"description"`
|
||||
Thumbnail pgtype.Text `json:"thumbnail"`
|
||||
SortOrder int32 `json:"sort_order"`
|
||||
UnitsCount int64 `json:"units_count"`
|
||||
ModulesCount int64 `json:"modules_count"`
|
||||
LessonsCount int64 `json:"lessons_count"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
|
@ -151,6 +169,9 @@ func (q *Queries) ExamPrepListCatalogCourses(ctx context.Context, arg ExamPrepLi
|
|||
&i.Description,
|
||||
&i.Thumbnail,
|
||||
&i.SortOrder,
|
||||
&i.UnitsCount,
|
||||
&i.ModulesCount,
|
||||
&i.LessonsCount,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ type ExamPrepCatalogCourse struct {
|
|||
Description *string `json:"description,omitempty"`
|
||||
Thumbnail *string `json:"thumbnail,omitempty"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
UnitsCount *int64 `json:"units_count,omitempty"`
|
||||
ModulesCount *int64 `json:"modules_count,omitempty"`
|
||||
LessonsCount *int64 `json:"lessons_count,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ func (s *Store) ListExamPrepCatalogCourses(ctx context.Context, limit, offset in
|
|||
if i == 0 {
|
||||
total = r.TotalCount
|
||||
}
|
||||
out = append(out, examPrepCatalogCourseToDomain(dbgen.ExamPrepCatalogCourse{
|
||||
item := examPrepCatalogCourseToDomain(dbgen.ExamPrepCatalogCourse{
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
Description: r.Description,
|
||||
|
|
@ -75,7 +75,11 @@ func (s *Store) ListExamPrepCatalogCourses(ctx context.Context, limit, offset in
|
|||
SortOrder: r.SortOrder,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}))
|
||||
})
|
||||
item.UnitsCount = &r.UnitsCount
|
||||
item.ModulesCount = &r.ModulesCount
|
||||
item.LessonsCount = &r.LessonsCount
|
||||
out = append(out, item)
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user