-- name: CreateSysApi :one
INSERT INTO sys_apis (name, group_name, method, path, sort)
VALUES ($1, $2, $3, $4, $5)
RETURNING id;

-- name: GetSysApis :many
SELECT *
FROM sys_apis
ORDER BY id
LIMIT $1 OFFSET $2;

-- name: CountSysApis :one
SELECT COUNT(*)
FROM sys_apis;

-- name: GetSysApiByID :one
SELECT *
FROM sys_apis
WHERE id = $1;

-- name: UpdateSysApi :execrows
UPDATE sys_apis
SET name       = $2,
    method     = $3,
    path       = $4,
    sort       = $5,
    group_name = $6
WHERE id = $1;

-- name: DeleteSysApi :execrows
DELETE
FROM sys_apis
WHERE id = $1;

-- name: CreateSysApiPermission :exec
INSERT INTO sys_api_permission (api_id, permission_id)
VALUES ($1, $2);

-- name: DeleteSysApiPermission :exec
DELETE
FROM sys_api_permission
WHERE api_id = $1;

-- name: DeleteSysPermissionBySysApiID :exec
DELETE
FROM sys_permissions
WHERE id IN (SELECT permission_id
             FROM sys_api_permission
             WHERE api_id = $1);

-- name: GetSysApiGroupNames :many
SELECT DISTINCT group_name
FROM sys_apis;

-- name: GetAllSysApis :many
SELECT *
FROM sys_apis
ORDER BY sort ASC;