Expose per-unit aggregate counts under catalog-course units listing so clients can display unit depth without extra chained requests. Co-authored-by: Cursor <cursoragent@cursor.com>
254 lines
6.5 KiB
Go
254 lines
6.5 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: exam_prep_units.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const ExamPrepCreateUnit = `-- name: ExamPrepCreateUnit :one
|
|
INSERT INTO exam_prep.units (catalog_course_id, name, description, thumbnail, sort_order)
|
|
SELECT
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
coalesce((
|
|
SELECT
|
|
max(u.sort_order)
|
|
FROM exam_prep.units u
|
|
WHERE
|
|
u.catalog_course_id = $1), 0) + 1
|
|
RETURNING
|
|
id, catalog_course_id, name, description, thumbnail, sort_order, created_at, updated_at
|
|
`
|
|
|
|
type ExamPrepCreateUnitParams struct {
|
|
CatalogCourseID int64 `json:"catalog_course_id"`
|
|
Name string `json:"name"`
|
|
Description pgtype.Text `json:"description"`
|
|
Thumbnail pgtype.Text `json:"thumbnail"`
|
|
}
|
|
|
|
func (q *Queries) ExamPrepCreateUnit(ctx context.Context, arg ExamPrepCreateUnitParams) (ExamPrepUnit, error) {
|
|
row := q.db.QueryRow(ctx, ExamPrepCreateUnit,
|
|
arg.CatalogCourseID,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Thumbnail,
|
|
)
|
|
var i ExamPrepUnit
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.CatalogCourseID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Thumbnail,
|
|
&i.SortOrder,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const ExamPrepDeleteUnit = `-- name: ExamPrepDeleteUnit :exec
|
|
DELETE FROM exam_prep.units
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) ExamPrepDeleteUnit(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, ExamPrepDeleteUnit, id)
|
|
return err
|
|
}
|
|
|
|
const ExamPrepGetUnitByID = `-- name: ExamPrepGetUnitByID :one
|
|
SELECT id, catalog_course_id, name, description, thumbnail, sort_order, created_at, updated_at
|
|
FROM exam_prep.units
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) ExamPrepGetUnitByID(ctx context.Context, id int64) (ExamPrepUnit, error) {
|
|
row := q.db.QueryRow(ctx, ExamPrepGetUnitByID, id)
|
|
var i ExamPrepUnit
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.CatalogCourseID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Thumbnail,
|
|
&i.SortOrder,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const ExamPrepListUnitIDsByCatalogCourse = `-- name: ExamPrepListUnitIDsByCatalogCourse :many
|
|
SELECT
|
|
u.id
|
|
FROM exam_prep.units u
|
|
WHERE
|
|
u.catalog_course_id = $1
|
|
ORDER BY
|
|
u.id
|
|
`
|
|
|
|
func (q *Queries) ExamPrepListUnitIDsByCatalogCourse(ctx context.Context, catalogCourseID int64) ([]int64, error) {
|
|
rows, err := q.db.Query(ctx, ExamPrepListUnitIDsByCatalogCourse, catalogCourseID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []int64
|
|
for rows.Next() {
|
|
var id int64
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ExamPrepListUnitsByCatalogCourse = `-- name: ExamPrepListUnitsByCatalogCourse :many
|
|
WITH unit_counts AS (
|
|
SELECT
|
|
u.id AS unit_id,
|
|
COUNT(DISTINCT m.id)::BIGINT AS modules_count,
|
|
COUNT(DISTINCT l.id)::BIGINT AS lessons_count,
|
|
COUNT(DISTINCT p.id)::BIGINT AS practices_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
|
|
LEFT JOIN exam_prep.lesson_practices p ON p.unit_module_lesson_id = l.id
|
|
GROUP BY u.id
|
|
)
|
|
SELECT
|
|
COUNT(*) OVER () AS total_count,
|
|
u.id,
|
|
u.catalog_course_id,
|
|
u.name,
|
|
u.description,
|
|
u.thumbnail,
|
|
u.sort_order,
|
|
COALESCE(uc.modules_count, 0)::BIGINT AS modules_count,
|
|
COALESCE(uc.lessons_count, 0)::BIGINT AS lessons_count,
|
|
COALESCE(uc.practices_count, 0)::BIGINT AS practices_count,
|
|
u.created_at,
|
|
u.updated_at
|
|
FROM exam_prep.units u
|
|
LEFT JOIN unit_counts uc ON uc.unit_id = u.id
|
|
WHERE
|
|
u.catalog_course_id = $1
|
|
ORDER BY
|
|
u.sort_order ASC,
|
|
u.id ASC
|
|
LIMIT $2
|
|
OFFSET $3
|
|
`
|
|
|
|
type ExamPrepListUnitsByCatalogCourseParams struct {
|
|
CatalogCourseID int64 `json:"catalog_course_id"`
|
|
Limit int32 `json:"limit"`
|
|
Offset int32 `json:"offset"`
|
|
}
|
|
|
|
type ExamPrepListUnitsByCatalogCourseRow struct {
|
|
TotalCount int64 `json:"total_count"`
|
|
ID int64 `json:"id"`
|
|
CatalogCourseID int64 `json:"catalog_course_id"`
|
|
Name string `json:"name"`
|
|
Description pgtype.Text `json:"description"`
|
|
Thumbnail pgtype.Text `json:"thumbnail"`
|
|
SortOrder int32 `json:"sort_order"`
|
|
ModulesCount int64 `json:"modules_count"`
|
|
LessonsCount int64 `json:"lessons_count"`
|
|
PracticesCount int64 `json:"practices_count"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) ExamPrepListUnitsByCatalogCourse(ctx context.Context, arg ExamPrepListUnitsByCatalogCourseParams) ([]ExamPrepListUnitsByCatalogCourseRow, error) {
|
|
rows, err := q.db.Query(ctx, ExamPrepListUnitsByCatalogCourse, arg.CatalogCourseID, arg.Limit, arg.Offset)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ExamPrepListUnitsByCatalogCourseRow
|
|
for rows.Next() {
|
|
var i ExamPrepListUnitsByCatalogCourseRow
|
|
if err := rows.Scan(
|
|
&i.TotalCount,
|
|
&i.ID,
|
|
&i.CatalogCourseID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Thumbnail,
|
|
&i.SortOrder,
|
|
&i.ModulesCount,
|
|
&i.LessonsCount,
|
|
&i.PracticesCount,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ExamPrepUpdateUnit = `-- name: ExamPrepUpdateUnit :one
|
|
UPDATE exam_prep.units
|
|
SET
|
|
name = coalesce($1::varchar, name),
|
|
description = coalesce($2::text, description),
|
|
thumbnail = coalesce($3::text, thumbnail),
|
|
sort_order = coalesce($4::int, sort_order),
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $5
|
|
RETURNING
|
|
id, catalog_course_id, name, description, thumbnail, sort_order, created_at, updated_at
|
|
`
|
|
|
|
type ExamPrepUpdateUnitParams struct {
|
|
Name pgtype.Text `json:"name"`
|
|
Description pgtype.Text `json:"description"`
|
|
Thumbnail pgtype.Text `json:"thumbnail"`
|
|
SortOrder pgtype.Int4 `json:"sort_order"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) ExamPrepUpdateUnit(ctx context.Context, arg ExamPrepUpdateUnitParams) (ExamPrepUnit, error) {
|
|
row := q.db.QueryRow(ctx, ExamPrepUpdateUnit,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Thumbnail,
|
|
arg.SortOrder,
|
|
arg.ID,
|
|
)
|
|
var i ExamPrepUnit
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.CatalogCourseID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Thumbnail,
|
|
&i.SortOrder,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|