Yimaru-BackEnd/db/query/courses.sql

48 lines
817 B
SQL

-- name: CreateCourse :one
INSERT INTO courses (
category_id,
title,
description,
thumbnail,
is_active
)
VALUES ($1, $2, $3, $4, COALESCE($5, true))
RETURNING *;
-- name: GetCourseByID :one
SELECT *
FROM courses
WHERE id = $1;
-- name: GetCoursesByCategory :many
SELECT
COUNT(*) OVER () AS total_count,
id,
category_id,
title,
description,
thumbnail,
is_active
FROM courses
WHERE category_id = $1
ORDER BY id DESC
LIMIT sqlc.narg('limit')::INT
OFFSET sqlc.narg('offset')::INT;
-- name: UpdateCourse :exec
UPDATE courses
SET
title = COALESCE($1, title),
description = COALESCE($2, description),
thumbnail = COALESCE($3, thumbnail),
is_active = COALESCE($4, is_active)
WHERE id = $5;
-- name: DeleteCourse :exec
DELETE FROM courses
WHERE id = $1;