2020-03-31 07:38:45 -06:00
|
|
|
-- 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);
|
|
|
|
|
2020-04-05 09:33:04 -06:00
|
|
|
-- name: UpdateEntry :execrows
|
|
|
|
UPDATE entries SET
|
2020-04-15 07:40:11 -06:00
|
|
|
title = $3,
|
|
|
|
body = $4
|
|
|
|
WHERE entry_id = $1 and
|
|
|
|
user_id = $2;
|
2020-04-05 09:33:04 -06:00
|
|
|
|
2020-03-31 07:38:45 -06:00
|
|
|
-- name: GetEntry :one
|
|
|
|
SELECT * FROM entries
|
2020-04-10 07:48:33 -06:00
|
|
|
WHERE entry_id = $1 and user_id = $2
|
|
|
|
LIMIT 1;
|
2020-03-31 07:38:45 -06:00
|
|
|
|
|
|
|
-- name: GetEntries :many
|
|
|
|
SELECT * FROM entries
|
|
|
|
WHERE user_id = $1;
|
|
|
|
|
2020-04-05 09:33:04 -06:00
|
|
|
-- name: DeleteEntry :execrows
|
2020-03-31 07:38:45 -06:00
|
|
|
DELETE FROM entries
|
|
|
|
WHERE entry_id = $1;
|
|
|
|
|
|
|
|
-- name: SimilarEntries :many
|
2020-04-02 06:29:59 -06:00
|
|
|
SELECT entry_id, similarity(body, $2) as similarity,
|
2020-04-15 07:40:11 -06:00
|
|
|
ts_headline('english', body, q, 'StartSel = <b>, StopSel = </b>') as headline,
|
2020-04-02 06:29:59 -06:00
|
|
|
title from entries,
|
2020-04-05 09:33:04 -06:00
|
|
|
to_tsquery($2) q
|
2020-04-02 06:29:59 -06:00
|
|
|
WHERE user_id = $1 and
|
|
|
|
similarity(body, $2) > 0.0
|
|
|
|
order by similarity DESC
|
|
|
|
LIMIT 10;
|
2020-03-31 07:38:45 -06:00
|
|
|
|
|
|
|
-- 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;
|
|
|
|
|
|
|
|
-- name: GetUser :one
|
|
|
|
SELECT * FROM users
|
|
|
|
WHERE user_id = $1 LIMIT 1;
|
|
|
|
|
|
|
|
-- name: GetUserByToken :one
|
|
|
|
SELECT * FROM users
|
|
|
|
WHERE token = $1 LIMIT 1;
|
|
|
|
|
|
|
|
-- 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;
|
|
|
|
|
|
|
|
-- name: DeleteUser :exec
|
|
|
|
DELETE FROM users CASCADE
|
|
|
|
WHERE user_id = $1;
|
|
|
|
|
|
|
|
-- name: ValidToken :one
|
|
|
|
SELECT now() < token_created FROM users
|
|
|
|
WHERE token = $1 LIMIT 1;
|
|
|
|
|
|
|
|
-- name: EntriesByToken :many
|
|
|
|
SELECT * FROM entries
|
|
|
|
WHERE user_id = (SELECT user_id FROM users WHERE token = $1 limit 1);
|