-- name: GetShelveByID :one
SELECT * FROM shelves
WHERE id = sqlc.arg(id);

-- name: ListShelves :many
SELECT * FROM shelves
ORDER BY created_at DESC;

-- name: CreateShelve :one
INSERT INTO shelves (cabinet_id,name,level_index, description, created_at)
VALUES (
    sqlc.arg(cabinet_id),
    sqlc.arg(name),
    sqlc.arg(level_index),
    sqlc.arg(description),
    sqlc.arg(created_at)
)
RETURNING *;

-- name: UpdateShelve :one
UPDATE shelves
SET name = CASE WHEN sqlc.arg(name) = '' THEN name ELSE sqlc.arg(name) END,
    level_index = CASE WHEN sqlc.arg(level_index) = 0 THEN level_index ELSE sqlc.arg(level_index) END,
    description = coalesce(sqlc.arg(description), description),
    updated_at = sqlc.arg(updated_at)
WHERE id = sqlc.arg(id)
RETURNING *;

-- name: DeleteShelve :execrows
DELETE FROM shelves
WHERE id = sqlc.arg(id);
