Expose publish_status on create/update, filter learner-facing lists and gates, and add migration 000060. Co-authored-by: Cursor <cursoragent@cursor.com>
303 lines
7.2 KiB
Go
303 lines
7.2 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: lms_courses.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const CreateCourse = `-- name: CreateCourse :one
|
|
INSERT INTO courses (program_id, name, description, thumbnail, sort_order)
|
|
SELECT
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
COALESCE($5::int,
|
|
COALESCE((
|
|
SELECT
|
|
max(c.sort_order)
|
|
FROM courses c
|
|
WHERE
|
|
c.program_id = $1), 0) + 1)
|
|
RETURNING
|
|
id, program_id, name, description, thumbnail, created_at, updated_at, sort_order
|
|
`
|
|
|
|
type CreateCourseParams struct {
|
|
ProgramID int64 `json:"program_id"`
|
|
Name string `json:"name"`
|
|
Description pgtype.Text `json:"description"`
|
|
Thumbnail pgtype.Text `json:"thumbnail"`
|
|
SortOrder pgtype.Int4 `json:"sort_order"`
|
|
}
|
|
|
|
func (q *Queries) CreateCourse(ctx context.Context, arg CreateCourseParams) (Course, error) {
|
|
row := q.db.QueryRow(ctx, CreateCourse,
|
|
arg.ProgramID,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Thumbnail,
|
|
arg.SortOrder,
|
|
)
|
|
var i Course
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ProgramID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Thumbnail,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.SortOrder,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const DeleteCourse = `-- name: DeleteCourse :exec
|
|
DELETE FROM courses
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteCourse(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, DeleteCourse, id)
|
|
return err
|
|
}
|
|
|
|
const GetCourseByID = `-- name: GetCourseByID :one
|
|
SELECT
|
|
c.id, c.program_id, c.name, c.description, c.thumbnail, c.created_at, c.updated_at, c.sort_order,
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM lms_practices p
|
|
WHERE p.course_id = c.id
|
|
AND p.module_id IS NULL
|
|
AND p.lesson_id IS NULL
|
|
AND p.publish_status = 'PUBLISHED'
|
|
) AS has_practice
|
|
FROM courses
|
|
c
|
|
WHERE c.id = $1
|
|
`
|
|
|
|
type GetCourseByIDRow struct {
|
|
ID int64 `json:"id"`
|
|
ProgramID int64 `json:"program_id"`
|
|
Name string `json:"name"`
|
|
Description pgtype.Text `json:"description"`
|
|
Thumbnail pgtype.Text `json:"thumbnail"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
SortOrder int32 `json:"sort_order"`
|
|
HasPractice bool `json:"has_practice"`
|
|
}
|
|
|
|
func (q *Queries) GetCourseByID(ctx context.Context, id int64) (GetCourseByIDRow, error) {
|
|
row := q.db.QueryRow(ctx, GetCourseByID, id)
|
|
var i GetCourseByIDRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ProgramID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Thumbnail,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.SortOrder,
|
|
&i.HasPractice,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const ListCourseIDsByProgram = `-- name: ListCourseIDsByProgram :many
|
|
SELECT
|
|
c.id
|
|
FROM
|
|
courses AS c
|
|
WHERE
|
|
c.program_id = $1
|
|
ORDER BY
|
|
c.id
|
|
`
|
|
|
|
func (q *Queries) ListCourseIDsByProgram(ctx context.Context, programID int64) ([]int64, error) {
|
|
rows, err := q.db.Query(ctx, ListCourseIDsByProgram, programID)
|
|
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 ListCoursesByProgramID = `-- name: ListCoursesByProgramID :many
|
|
SELECT
|
|
COUNT(*) OVER () AS total_count,
|
|
c.id,
|
|
c.program_id,
|
|
c.name,
|
|
c.description,
|
|
c.thumbnail,
|
|
c.sort_order,
|
|
c.created_at,
|
|
c.updated_at,
|
|
(
|
|
SELECT
|
|
COUNT(*)::bigint
|
|
FROM
|
|
modules m
|
|
WHERE
|
|
m.course_id = c.id) AS module_count,
|
|
(
|
|
SELECT
|
|
COUNT(*)::bigint
|
|
FROM
|
|
lessons l
|
|
INNER JOIN modules m ON l.module_id = m.id
|
|
WHERE
|
|
m.course_id = c.id) AS lesson_count,
|
|
-- Practices whose parent is the course only (lms_practices.course_id). Excludes
|
|
-- practices linked via module_id or lesson_id, even for modules/lessons in this course.
|
|
(
|
|
SELECT
|
|
COUNT(*)::bigint
|
|
FROM
|
|
lms_practices p
|
|
WHERE
|
|
p.course_id = c.id
|
|
AND p.module_id IS NULL
|
|
AND p.lesson_id IS NULL
|
|
AND p.publish_status = 'PUBLISHED') AS practice_count,
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM lms_practices p
|
|
WHERE p.course_id = c.id
|
|
AND p.module_id IS NULL
|
|
AND p.lesson_id IS NULL
|
|
AND p.publish_status = 'PUBLISHED'
|
|
) AS has_practice
|
|
FROM
|
|
courses c
|
|
WHERE
|
|
c.program_id = $1
|
|
ORDER BY
|
|
c.sort_order ASC,
|
|
c.id ASC
|
|
LIMIT $2 OFFSET $3
|
|
`
|
|
|
|
type ListCoursesByProgramIDParams struct {
|
|
ProgramID int64 `json:"program_id"`
|
|
Limit int32 `json:"limit"`
|
|
Offset int32 `json:"offset"`
|
|
}
|
|
|
|
type ListCoursesByProgramIDRow struct {
|
|
TotalCount int64 `json:"total_count"`
|
|
ID int64 `json:"id"`
|
|
ProgramID int64 `json:"program_id"`
|
|
Name string `json:"name"`
|
|
Description pgtype.Text `json:"description"`
|
|
Thumbnail pgtype.Text `json:"thumbnail"`
|
|
SortOrder int32 `json:"sort_order"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
ModuleCount int64 `json:"module_count"`
|
|
LessonCount int64 `json:"lesson_count"`
|
|
PracticeCount int64 `json:"practice_count"`
|
|
HasPractice bool `json:"has_practice"`
|
|
}
|
|
|
|
func (q *Queries) ListCoursesByProgramID(ctx context.Context, arg ListCoursesByProgramIDParams) ([]ListCoursesByProgramIDRow, error) {
|
|
rows, err := q.db.Query(ctx, ListCoursesByProgramID, arg.ProgramID, arg.Limit, arg.Offset)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListCoursesByProgramIDRow
|
|
for rows.Next() {
|
|
var i ListCoursesByProgramIDRow
|
|
if err := rows.Scan(
|
|
&i.TotalCount,
|
|
&i.ID,
|
|
&i.ProgramID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Thumbnail,
|
|
&i.SortOrder,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ModuleCount,
|
|
&i.LessonCount,
|
|
&i.PracticeCount,
|
|
&i.HasPractice,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const UpdateCourse = `-- name: UpdateCourse :one
|
|
UPDATE courses
|
|
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, program_id, name, description, thumbnail, created_at, updated_at, sort_order
|
|
`
|
|
|
|
type UpdateCourseParams 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) UpdateCourse(ctx context.Context, arg UpdateCourseParams) (Course, error) {
|
|
row := q.db.QueryRow(ctx, UpdateCourse,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Thumbnail,
|
|
arg.SortOrder,
|
|
arg.ID,
|
|
)
|
|
var i Course
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ProgramID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Thumbnail,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.SortOrder,
|
|
)
|
|
return i, err
|
|
}
|