234 lines
5.1 KiB
Go
234 lines
5.1 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((
|
|
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"`
|
|
}
|
|
|
|
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,
|
|
)
|
|
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 id, program_id, name, description, thumbnail, created_at, updated_at, sort_order
|
|
FROM courses
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetCourseByID(ctx context.Context, id int64) (Course, error) {
|
|
row := q.db.QueryRow(ctx, GetCourseByID, 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
|
|
}
|
|
|
|
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
|
|
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"`
|
|
}
|
|
|
|
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,
|
|
); 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
|
|
}
|