mirror of
https://github.com/Dvorinka/Primora.git
synced 2026-06-04 12:33:01 +00:00
202 lines
4.9 KiB
Go
202 lines
4.9 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.29.0
|
|
// source: api_keys.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const createAPIKey = `-- name: CreateAPIKey :one
|
|
INSERT INTO core.api_keys (
|
|
project_id,
|
|
name,
|
|
prefix,
|
|
secret_hash,
|
|
created_by_user_id
|
|
) VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING id, project_id, name, prefix, secret_hash, created_by_user_id, last_used_at, revoked_at, created_at
|
|
`
|
|
|
|
type CreateAPIKeyParams struct {
|
|
ProjectID uuid.UUID `json:"project_id"`
|
|
Name string `json:"name"`
|
|
Prefix string `json:"prefix"`
|
|
SecretHash []byte `json:"secret_hash"`
|
|
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
|
|
}
|
|
|
|
func (q *Queries) CreateAPIKey(ctx context.Context, arg CreateAPIKeyParams) (CoreApiKey, error) {
|
|
row := q.db.QueryRow(ctx, createAPIKey,
|
|
arg.ProjectID,
|
|
arg.Name,
|
|
arg.Prefix,
|
|
arg.SecretHash,
|
|
arg.CreatedByUserID,
|
|
)
|
|
var i CoreApiKey
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ProjectID,
|
|
&i.Name,
|
|
&i.Prefix,
|
|
&i.SecretHash,
|
|
&i.CreatedByUserID,
|
|
&i.LastUsedAt,
|
|
&i.RevokedAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getAPIKeyByIDForProject = `-- name: GetAPIKeyByIDForProject :one
|
|
SELECT id, project_id, name, prefix, secret_hash, created_by_user_id, last_used_at, revoked_at, created_at FROM core.api_keys
|
|
WHERE project_id = $1
|
|
AND id = $2
|
|
`
|
|
|
|
type GetAPIKeyByIDForProjectParams struct {
|
|
ProjectID uuid.UUID `json:"project_id"`
|
|
ID uuid.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) GetAPIKeyByIDForProject(ctx context.Context, arg GetAPIKeyByIDForProjectParams) (CoreApiKey, error) {
|
|
row := q.db.QueryRow(ctx, getAPIKeyByIDForProject, arg.ProjectID, arg.ID)
|
|
var i CoreApiKey
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ProjectID,
|
|
&i.Name,
|
|
&i.Prefix,
|
|
&i.SecretHash,
|
|
&i.CreatedByUserID,
|
|
&i.LastUsedAt,
|
|
&i.RevokedAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getAPIKeyByPrefix = `-- name: GetAPIKeyByPrefix :one
|
|
SELECT
|
|
ak.id, ak.project_id, ak.name, ak.prefix, ak.secret_hash, ak.created_by_user_id, ak.last_used_at, ak.revoked_at, ak.created_at,
|
|
p.organization_id
|
|
FROM core.api_keys ak
|
|
JOIN core.projects p ON p.id = ak.project_id
|
|
WHERE ak.prefix = $1
|
|
`
|
|
|
|
type GetAPIKeyByPrefixRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
ProjectID uuid.UUID `json:"project_id"`
|
|
Name string `json:"name"`
|
|
Prefix string `json:"prefix"`
|
|
SecretHash []byte `json:"secret_hash"`
|
|
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
|
|
LastUsedAt pgtype.Timestamptz `json:"last_used_at"`
|
|
RevokedAt pgtype.Timestamptz `json:"revoked_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
OrganizationID uuid.UUID `json:"organization_id"`
|
|
}
|
|
|
|
func (q *Queries) GetAPIKeyByPrefix(ctx context.Context, prefix string) (GetAPIKeyByPrefixRow, error) {
|
|
row := q.db.QueryRow(ctx, getAPIKeyByPrefix, prefix)
|
|
var i GetAPIKeyByPrefixRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ProjectID,
|
|
&i.Name,
|
|
&i.Prefix,
|
|
&i.SecretHash,
|
|
&i.CreatedByUserID,
|
|
&i.LastUsedAt,
|
|
&i.RevokedAt,
|
|
&i.CreatedAt,
|
|
&i.OrganizationID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listAPIKeysForProject = `-- name: ListAPIKeysForProject :many
|
|
SELECT id, project_id, name, prefix, secret_hash, created_by_user_id, last_used_at, revoked_at, created_at FROM core.api_keys
|
|
WHERE project_id = $1
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListAPIKeysForProject(ctx context.Context, projectID uuid.UUID) ([]CoreApiKey, error) {
|
|
rows, err := q.db.Query(ctx, listAPIKeysForProject, projectID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []CoreApiKey
|
|
for rows.Next() {
|
|
var i CoreApiKey
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ProjectID,
|
|
&i.Name,
|
|
&i.Prefix,
|
|
&i.SecretHash,
|
|
&i.CreatedByUserID,
|
|
&i.LastUsedAt,
|
|
&i.RevokedAt,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const revokeAPIKey = `-- name: RevokeAPIKey :one
|
|
UPDATE core.api_keys
|
|
SET revoked_at = NOW()
|
|
WHERE project_id = $1
|
|
AND id = $2
|
|
AND revoked_at IS NULL
|
|
RETURNING id, project_id, name, prefix, secret_hash, created_by_user_id, last_used_at, revoked_at, created_at
|
|
`
|
|
|
|
type RevokeAPIKeyParams struct {
|
|
ProjectID uuid.UUID `json:"project_id"`
|
|
ID uuid.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) RevokeAPIKey(ctx context.Context, arg RevokeAPIKeyParams) (CoreApiKey, error) {
|
|
row := q.db.QueryRow(ctx, revokeAPIKey, arg.ProjectID, arg.ID)
|
|
var i CoreApiKey
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ProjectID,
|
|
&i.Name,
|
|
&i.Prefix,
|
|
&i.SecretHash,
|
|
&i.CreatedByUserID,
|
|
&i.LastUsedAt,
|
|
&i.RevokedAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const touchAPIKey = `-- name: TouchAPIKey :exec
|
|
UPDATE core.api_keys
|
|
SET last_used_at = NOW()
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) TouchAPIKey(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, touchAPIKey, id)
|
|
return err
|
|
}
|