351 lines
8.0 KiB
Go
351 lines
8.0 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// source: queries.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const authUser = `-- name: AuthUser :one
|
|
UPDATE users
|
|
SET
|
|
token = DEFAULT,
|
|
token_expires = DEFAULT
|
|
WHERE
|
|
username = $2 and
|
|
(hash = crypt($1, hash)) = true
|
|
RETURNING user_id, created_at, first_name, last_name, username, email, token, token_expires, true as authed
|
|
`
|
|
|
|
type AuthUserParams struct {
|
|
Crypt interface{} `json:"crypt"`
|
|
Username string `json:"username"`
|
|
}
|
|
|
|
type AuthUserRow struct {
|
|
UserID int64 `json:"user_id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
FirstName string `json:"first_name"`
|
|
LastName string `json:"last_name"`
|
|
Username string `json:"username"`
|
|
Email string `json:"email"`
|
|
Token uuid.UUID `json:"token"`
|
|
TokenExpires time.Time `json:"token_expires"`
|
|
Authed bool `json:"authed"`
|
|
}
|
|
|
|
func (q *Queries) AuthUser(ctx context.Context, arg AuthUserParams) (AuthUserRow, error) {
|
|
row := q.queryRow(ctx, q.authUserStmt, authUser, arg.Crypt, arg.Username)
|
|
var i AuthUserRow
|
|
err := row.Scan(
|
|
&i.UserID,
|
|
&i.CreatedAt,
|
|
&i.FirstName,
|
|
&i.LastName,
|
|
&i.Username,
|
|
&i.Email,
|
|
&i.Token,
|
|
&i.TokenExpires,
|
|
&i.Authed,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createEntry = `-- name: CreateEntry :one
|
|
INSERT INTO entries (
|
|
entry_id, user_id, title, body
|
|
) VALUES (
|
|
$1, $2, $3, $4
|
|
)
|
|
RETURNING entry_id, created_at, to_tsvector(body)
|
|
`
|
|
|
|
type CreateEntryParams struct {
|
|
EntryID uuid.UUID `json:"entry_id"`
|
|
UserID int64 `json:"user_id"`
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
}
|
|
|
|
type CreateEntryRow struct {
|
|
EntryID uuid.UUID `json:"entry_id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
ToTsvector interface{} `json:"to_tsvector"`
|
|
}
|
|
|
|
func (q *Queries) CreateEntry(ctx context.Context, arg CreateEntryParams) (CreateEntryRow, error) {
|
|
row := q.queryRow(ctx, q.createEntryStmt, createEntry,
|
|
arg.EntryID,
|
|
arg.UserID,
|
|
arg.Title,
|
|
arg.Body,
|
|
)
|
|
var i CreateEntryRow
|
|
err := row.Scan(&i.EntryID, &i.CreatedAt, &i.ToTsvector)
|
|
return i, err
|
|
}
|
|
|
|
const createUser = `-- name: CreateUser :one
|
|
INSERT INTO users (
|
|
first_name, last_name, username, email, hash
|
|
) VALUES (
|
|
$1, $2, $3, $4, hash($5)
|
|
)
|
|
RETURNING user_id, username, token, token_expires
|
|
`
|
|
|
|
type CreateUserParams struct {
|
|
FirstName string `json:"first_name"`
|
|
LastName string `json:"last_name"`
|
|
Username string `json:"username"`
|
|
Email string `json:"email"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
type CreateUserRow struct {
|
|
UserID int64 `json:"user_id"`
|
|
Username string `json:"username"`
|
|
Token uuid.UUID `json:"token"`
|
|
TokenExpires time.Time `json:"token_expires"`
|
|
}
|
|
|
|
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (CreateUserRow, error) {
|
|
row := q.queryRow(ctx, q.createUserStmt, createUser,
|
|
arg.FirstName,
|
|
arg.LastName,
|
|
arg.Username,
|
|
arg.Email,
|
|
arg.Password,
|
|
)
|
|
var i CreateUserRow
|
|
err := row.Scan(
|
|
&i.UserID,
|
|
&i.Username,
|
|
&i.Token,
|
|
&i.TokenExpires,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteEntry = `-- name: DeleteEntry :exec
|
|
DELETE FROM entries
|
|
WHERE entry_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteEntry(ctx context.Context, entryID uuid.UUID) error {
|
|
_, err := q.exec(ctx, q.deleteEntryStmt, deleteEntry, entryID)
|
|
return err
|
|
}
|
|
|
|
const deleteUser = `-- name: DeleteUser :exec
|
|
DELETE FROM users CASCADE
|
|
WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteUser(ctx context.Context, userID int64) error {
|
|
_, err := q.exec(ctx, q.deleteUserStmt, deleteUser, userID)
|
|
return err
|
|
}
|
|
|
|
const entriesByToken = `-- name: EntriesByToken :many
|
|
SELECT entry_id, user_id, created_at, updated_at, title, body FROM entries
|
|
WHERE user_id = (SELECT user_id FROM users WHERE token = $1 limit 1)
|
|
`
|
|
|
|
func (q *Queries) EntriesByToken(ctx context.Context, token uuid.UUID) ([]Entry, error) {
|
|
rows, err := q.query(ctx, q.entriesByTokenStmt, entriesByToken, token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Entry
|
|
for rows.Next() {
|
|
var i Entry
|
|
if err := rows.Scan(
|
|
&i.EntryID,
|
|
&i.UserID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Title,
|
|
&i.Body,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getEntries = `-- name: GetEntries :many
|
|
SELECT entry_id, user_id, created_at, updated_at, title, body FROM entries
|
|
WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetEntries(ctx context.Context, userID int64) ([]Entry, error) {
|
|
rows, err := q.query(ctx, q.getEntriesStmt, getEntries, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Entry
|
|
for rows.Next() {
|
|
var i Entry
|
|
if err := rows.Scan(
|
|
&i.EntryID,
|
|
&i.UserID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Title,
|
|
&i.Body,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getEntry = `-- name: GetEntry :one
|
|
SELECT entry_id, user_id, created_at, updated_at, title, body FROM entries
|
|
WHERE entry_id = $1 LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetEntry(ctx context.Context, entryID uuid.UUID) (Entry, error) {
|
|
row := q.queryRow(ctx, q.getEntryStmt, getEntry, entryID)
|
|
var i Entry
|
|
err := row.Scan(
|
|
&i.EntryID,
|
|
&i.UserID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Title,
|
|
&i.Body,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getUser = `-- name: GetUser :one
|
|
SELECT user_id, created_at, updated_at, first_name, last_name, username, hash, email, token, token_expires FROM users
|
|
WHERE user_id = $1 LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetUser(ctx context.Context, userID int64) (User, error) {
|
|
row := q.queryRow(ctx, q.getUserStmt, getUser, userID)
|
|
var i User
|
|
err := row.Scan(
|
|
&i.UserID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.FirstName,
|
|
&i.LastName,
|
|
&i.Username,
|
|
&i.Hash,
|
|
&i.Email,
|
|
&i.Token,
|
|
&i.TokenExpires,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getUserByToken = `-- name: GetUserByToken :one
|
|
SELECT user_id, created_at, updated_at, first_name, last_name, username, hash, email, token, token_expires FROM users
|
|
WHERE token = $1 LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetUserByToken(ctx context.Context, token uuid.UUID) (User, error) {
|
|
row := q.queryRow(ctx, q.getUserByTokenStmt, getUserByToken, token)
|
|
var i User
|
|
err := row.Scan(
|
|
&i.UserID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.FirstName,
|
|
&i.LastName,
|
|
&i.Username,
|
|
&i.Hash,
|
|
&i.Email,
|
|
&i.Token,
|
|
&i.TokenExpires,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const similarEntries = `-- name: SimilarEntries :many
|
|
SELECT entry_id, similarity(body, $2) as similarity,
|
|
ts_headline('english', body, q) as headline,
|
|
title from entries,
|
|
plainto_tsquery($2) q
|
|
WHERE user_id = $1 and
|
|
similarity(body, $2) > 0.0
|
|
order by similarity DESC
|
|
LIMIT 10
|
|
`
|
|
|
|
type SimilarEntriesParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
Similarity interface{} `json:"similarity"`
|
|
}
|
|
|
|
type SimilarEntriesRow struct {
|
|
EntryID uuid.UUID `json:"entry_id"`
|
|
Similarity interface{} `json:"similarity"`
|
|
Headline interface{} `json:"headline"`
|
|
Title string `json:"title"`
|
|
}
|
|
|
|
func (q *Queries) SimilarEntries(ctx context.Context, arg SimilarEntriesParams) ([]SimilarEntriesRow, error) {
|
|
rows, err := q.query(ctx, q.similarEntriesStmt, similarEntries, arg.UserID, arg.Similarity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []SimilarEntriesRow
|
|
for rows.Next() {
|
|
var i SimilarEntriesRow
|
|
if err := rows.Scan(
|
|
&i.EntryID,
|
|
&i.Similarity,
|
|
&i.Headline,
|
|
&i.Title,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const validToken = `-- name: ValidToken :one
|
|
SELECT now() < token_created FROM users
|
|
WHERE token = $1 LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) ValidToken(ctx context.Context, token uuid.UUID) (bool, error) {
|
|
row := q.queryRow(ctx, q.validTokenStmt, validToken, token)
|
|
var column_1 bool
|
|
err := row.Scan(&column_1)
|
|
return column_1, err
|
|
}
|