mirror of
https://github.com/PrivateCaptcha/PrivateCaptcha.git
synced 2026-02-09 07:19:08 -06:00
284 lines
7.7 KiB
Go
284 lines
7.7 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: apikeys.sql
|
|
|
|
package generated
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"time"
|
|
)
|
|
|
|
const createAPIKey = `-- name: CreateAPIKey :one
|
|
INSERT INTO backend.apikeys (name, user_id, expires_at, requests_per_second, requests_burst, period, scope, readonly, org_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING id, name, external_id, user_id, enabled, requests_per_second, requests_burst, created_at, expires_at, notes, org_id, updated_at, period, scope, readonly, last_used_at
|
|
`
|
|
|
|
type CreateAPIKeyParams struct {
|
|
Name string `db:"name" json:"name"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
ExpiresAt pgtype.Timestamptz `db:"expires_at" json:"expires_at"`
|
|
RequestsPerSecond float64 `db:"requests_per_second" json:"requests_per_second"`
|
|
RequestsBurst int32 `db:"requests_burst" json:"requests_burst"`
|
|
Period time.Duration `db:"period" json:"period"`
|
|
Scope ApiKeyScope `db:"scope" json:"scope"`
|
|
Readonly bool `db:"readonly" json:"readonly"`
|
|
OrgID pgtype.Int4 `db:"org_id" json:"org_id"`
|
|
}
|
|
|
|
func (q *Queries) CreateAPIKey(ctx context.Context, arg *CreateAPIKeyParams) (*APIKey, error) {
|
|
row := q.db.QueryRow(ctx, createAPIKey,
|
|
arg.Name,
|
|
arg.UserID,
|
|
arg.ExpiresAt,
|
|
arg.RequestsPerSecond,
|
|
arg.RequestsBurst,
|
|
arg.Period,
|
|
arg.Scope,
|
|
arg.Readonly,
|
|
arg.OrgID,
|
|
)
|
|
var i APIKey
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.UserID,
|
|
&i.Enabled,
|
|
&i.RequestsPerSecond,
|
|
&i.RequestsBurst,
|
|
&i.CreatedAt,
|
|
&i.ExpiresAt,
|
|
&i.Notes,
|
|
&i.OrgID,
|
|
&i.UpdatedAt,
|
|
&i.Period,
|
|
&i.Scope,
|
|
&i.Readonly,
|
|
&i.LastUsedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const deleteAPIKey = `-- name: DeleteAPIKey :one
|
|
DELETE FROM backend.apikeys WHERE id=$1 AND user_id = $2 RETURNING id, name, external_id, user_id, enabled, requests_per_second, requests_burst, created_at, expires_at, notes, org_id, updated_at, period, scope, readonly, last_used_at
|
|
`
|
|
|
|
type DeleteAPIKeyParams struct {
|
|
ID int32 `db:"id" json:"id"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) DeleteAPIKey(ctx context.Context, arg *DeleteAPIKeyParams) (*APIKey, error) {
|
|
row := q.db.QueryRow(ctx, deleteAPIKey, arg.ID, arg.UserID)
|
|
var i APIKey
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.UserID,
|
|
&i.Enabled,
|
|
&i.RequestsPerSecond,
|
|
&i.RequestsBurst,
|
|
&i.CreatedAt,
|
|
&i.ExpiresAt,
|
|
&i.Notes,
|
|
&i.OrgID,
|
|
&i.UpdatedAt,
|
|
&i.Period,
|
|
&i.Scope,
|
|
&i.Readonly,
|
|
&i.LastUsedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const deleteUserAPIKeys = `-- name: DeleteUserAPIKeys :exec
|
|
DELETE FROM backend.apikeys WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteUserAPIKeys(ctx context.Context, userID pgtype.Int4) error {
|
|
_, err := q.db.Exec(ctx, deleteUserAPIKeys, userID)
|
|
return err
|
|
}
|
|
|
|
const getAPIKeyByExternalID = `-- name: GetAPIKeyByExternalID :one
|
|
SELECT id, name, external_id, user_id, enabled, requests_per_second, requests_burst, created_at, expires_at, notes, org_id, updated_at, period, scope, readonly, last_used_at FROM backend.apikeys WHERE external_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetAPIKeyByExternalID(ctx context.Context, externalID pgtype.UUID) (*APIKey, error) {
|
|
row := q.db.QueryRow(ctx, getAPIKeyByExternalID, externalID)
|
|
var i APIKey
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.UserID,
|
|
&i.Enabled,
|
|
&i.RequestsPerSecond,
|
|
&i.RequestsBurst,
|
|
&i.CreatedAt,
|
|
&i.ExpiresAt,
|
|
&i.Notes,
|
|
&i.OrgID,
|
|
&i.UpdatedAt,
|
|
&i.Period,
|
|
&i.Scope,
|
|
&i.Readonly,
|
|
&i.LastUsedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getUserAPIKeyByName = `-- name: GetUserAPIKeyByName :one
|
|
SELECT id, name, external_id, user_id, enabled, requests_per_second, requests_burst, created_at, expires_at, notes, org_id, updated_at, period, scope, readonly, last_used_at FROM backend.apikeys WHERE user_id = $1 AND name = $2 AND expires_at > NOW()
|
|
`
|
|
|
|
type GetUserAPIKeyByNameParams struct {
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
Name string `db:"name" json:"name"`
|
|
}
|
|
|
|
func (q *Queries) GetUserAPIKeyByName(ctx context.Context, arg *GetUserAPIKeyByNameParams) (*APIKey, error) {
|
|
row := q.db.QueryRow(ctx, getUserAPIKeyByName, arg.UserID, arg.Name)
|
|
var i APIKey
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.UserID,
|
|
&i.Enabled,
|
|
&i.RequestsPerSecond,
|
|
&i.RequestsBurst,
|
|
&i.CreatedAt,
|
|
&i.ExpiresAt,
|
|
&i.Notes,
|
|
&i.OrgID,
|
|
&i.UpdatedAt,
|
|
&i.Period,
|
|
&i.Scope,
|
|
&i.Readonly,
|
|
&i.LastUsedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getUserAPIKeys = `-- name: GetUserAPIKeys :many
|
|
SELECT id, name, external_id, user_id, enabled, requests_per_second, requests_burst, created_at, expires_at, notes, org_id, updated_at, period, scope, readonly, last_used_at FROM backend.apikeys WHERE user_id = $1 AND expires_at > NOW()
|
|
`
|
|
|
|
func (q *Queries) GetUserAPIKeys(ctx context.Context, userID pgtype.Int4) ([]*APIKey, error) {
|
|
rows, err := q.db.Query(ctx, getUserAPIKeys, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*APIKey
|
|
for rows.Next() {
|
|
var i APIKey
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.UserID,
|
|
&i.Enabled,
|
|
&i.RequestsPerSecond,
|
|
&i.RequestsBurst,
|
|
&i.CreatedAt,
|
|
&i.ExpiresAt,
|
|
&i.Notes,
|
|
&i.OrgID,
|
|
&i.UpdatedAt,
|
|
&i.Period,
|
|
&i.Scope,
|
|
&i.Readonly,
|
|
&i.LastUsedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const rotateAPIKey = `-- name: RotateAPIKey :one
|
|
UPDATE backend.apikeys SET external_id = gen_random_uuid(), expires_at = NOW() + period, updated_at = NOW() WHERE id = $1 AND user_id = $2 RETURNING id, name, external_id, user_id, enabled, requests_per_second, requests_burst, created_at, expires_at, notes, org_id, updated_at, period, scope, readonly, last_used_at
|
|
`
|
|
|
|
type RotateAPIKeyParams struct {
|
|
ID int32 `db:"id" json:"id"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) RotateAPIKey(ctx context.Context, arg *RotateAPIKeyParams) (*APIKey, error) {
|
|
row := q.db.QueryRow(ctx, rotateAPIKey, arg.ID, arg.UserID)
|
|
var i APIKey
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.UserID,
|
|
&i.Enabled,
|
|
&i.RequestsPerSecond,
|
|
&i.RequestsBurst,
|
|
&i.CreatedAt,
|
|
&i.ExpiresAt,
|
|
&i.Notes,
|
|
&i.OrgID,
|
|
&i.UpdatedAt,
|
|
&i.Period,
|
|
&i.Scope,
|
|
&i.Readonly,
|
|
&i.LastUsedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const updateAPIKey = `-- name: UpdateAPIKey :one
|
|
UPDATE backend.apikeys SET expires_at = $1, enabled = $2, updated_at = NOW() WHERE external_id = $3 RETURNING id, name, external_id, user_id, enabled, requests_per_second, requests_burst, created_at, expires_at, notes, org_id, updated_at, period, scope, readonly, last_used_at
|
|
`
|
|
|
|
type UpdateAPIKeyParams struct {
|
|
ExpiresAt pgtype.Timestamptz `db:"expires_at" json:"expires_at"`
|
|
Enabled pgtype.Bool `db:"enabled" json:"enabled"`
|
|
ExternalID pgtype.UUID `db:"external_id" json:"external_id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateAPIKey(ctx context.Context, arg *UpdateAPIKeyParams) (*APIKey, error) {
|
|
row := q.db.QueryRow(ctx, updateAPIKey, arg.ExpiresAt, arg.Enabled, arg.ExternalID)
|
|
var i APIKey
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.ExternalID,
|
|
&i.UserID,
|
|
&i.Enabled,
|
|
&i.RequestsPerSecond,
|
|
&i.RequestsBurst,
|
|
&i.CreatedAt,
|
|
&i.ExpiresAt,
|
|
&i.Notes,
|
|
&i.OrgID,
|
|
&i.UpdatedAt,
|
|
&i.Period,
|
|
&i.Scope,
|
|
&i.Readonly,
|
|
&i.LastUsedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const updateAPIKeysLastUsedAt = `-- name: UpdateAPIKeysLastUsedAt :exec
|
|
UPDATE backend.apikeys SET last_used_at = NOW() WHERE id = ANY($1::int[])
|
|
`
|
|
|
|
func (q *Queries) UpdateAPIKeysLastUsedAt(ctx context.Context, dollar_1 []int32) error {
|
|
_, err := q.db.Exec(ctx, updateAPIKeysLastUsedAt, dollar_1)
|
|
return err
|
|
}
|