-- name: insert-certificate

INSERT INTO Certificates
  (domain, data)
VALUES
  (?, ?);

-- name: find-one-certificate

SELECT
  (id, domain, data, created_at, edited_at, active)
FROM Certificates
WHERE domain=?
LIMIT 1;

-- name: remove-one-certificate

DELETE
FROM Certificates
WHERE domain=?
LIMIT 1;

-- name: get-all-certificates

SELECT
  (id, domain, data, created_at, edited_at, active)
FROM Certificates;

-- name: insert-route

INSERT INTO
  Routes(creator, hostname)
VALUES
  (?, ?);

-- name: find-one-route-by-id

SELECT
  (id, creator, hostname, created_at, edited_at, active)
FROM Routes
WHERE id=?
LIMIT 1;

-- name: find-one-route-by-host

SELECT
  (id, creator, hostname, created_at, edited_at, active)
FROM Routes
WHERE hostname=?
LIMIT 1;

-- name: find-all-routes-for-user

SELECT
  (id, creator, hostname, created_at, edited_at, active)
FROM Routes
WHERE creator=?;

-- name: delete-one-route

DELETE
FROM Routes
WHERE
  id=? AND domain=?
LIMIT 1;

-- name: insert-token

INSERT INTO Tokens
  (body, creator, scopes, expires_at)
VALUES
  (?, ?, ?, ?);

-- name: get-one-token

SELECT
  (id, body, creator, scopes, created_at, expires_at, active)
FROM Tokens
WHERE id=?
LIMIT 1;

-- name: get-one-token-by-body

SELECT
  (id, body, creator, scopes, created_at, expires_at, active)
FROM Tokens
WHERE body=?
LIMIT 1;

-- name: get-all-tokens-for-user

SELECT
  (id, body, creator, scopes, created_at, expires_at, active)
FROM Tokens
WHERE creator=?;

-- name: remove-one-token

DELETE
FROM Tokens
WHERE id=?
LIMIT 1;

-- name: remove-expired-tokens

DELETE
FROM Tokens
WHERE expires_at - interval '7 days';
