Yimaru-BackEnd/db/query/lms_lessons.sql

62 lines
1.2 KiB
SQL

-- name: CreateLesson :one
INSERT INTO lessons (module_id, title, video_url, thumbnail, description, sort_order)
SELECT
$1,
$2,
$3,
$4,
$5,
coalesce((
SELECT
max(l.sort_order)
FROM lessons l
WHERE
l.module_id = $1), 0) + 1
RETURNING
*;
-- name: GetLessonByID :one
SELECT *
FROM lessons
WHERE id = $1;
-- name: ListLessonsByModuleID :many
SELECT
COUNT(*) OVER () AS total_count,
l.id,
l.module_id,
l.title,
l.video_url,
l.thumbnail,
l.description,
l.sort_order,
l.created_at,
l.updated_at
FROM
lessons l
WHERE
l.module_id = $1
ORDER BY
l.sort_order ASC,
l.id ASC
LIMIT $2
OFFSET $3;
-- name: UpdateLesson :one
UPDATE lessons
SET
title = COALESCE(sqlc.narg('title')::varchar, title),
video_url = COALESCE(sqlc.narg('video_url')::text, video_url),
thumbnail = COALESCE(sqlc.narg('thumbnail')::text, thumbnail),
description = COALESCE(sqlc.narg('description')::text, description),
sort_order = coalesce(sqlc.narg('sort_order')::int, sort_order),
updated_at = CURRENT_TIMESTAMP
WHERE
id = sqlc.arg('id')
RETURNING
*;
-- name: DeleteLesson :exec
DELETE FROM lessons
WHERE id = $1;