Replace scs postgresstore with a sqlc-backed SessionStore.
Keep scs for cookies and session API while sessions DDL and queries live in the same sqlc stack as the rest of Postgres.
This commit is contained in:
@@ -4,6 +4,10 @@
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Answer struct {
|
||||
QuestionID string
|
||||
AuthorID string
|
||||
@@ -23,6 +27,12 @@ type Question struct {
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
Token string
|
||||
Data []byte
|
||||
Expiry time.Time
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID string
|
||||
Username string
|
||||
|
||||
@@ -39,9 +39,12 @@ func (q *Queries) CreateQuestion(ctx context.Context, arg CreateQuestionParams)
|
||||
|
||||
const getQuestion = `-- name: GetQuestion :one
|
||||
SELECT q.id, q.author_id, u.name AS author_name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at,
|
||||
COALESCE((SELECT SUM(value) FROM votes WHERE votes.question_id = q.id), 0)::bigint AS score,
|
||||
COALESCE((SELECT SUM(votes.value) FROM votes WHERE votes.question_id = q.id), 0)::bigint AS score,
|
||||
CASE WHEN a.question_id IS NULL THEN 0 ELSE 1 END::bigint AS answered,
|
||||
COALESCE((SELECT value FROM votes WHERE votes.user_id = $1 AND votes.question_id = q.id), 0)::bigint AS user_vote
|
||||
COALESCE((
|
||||
SELECT votes.value FROM votes
|
||||
WHERE votes.user_id = $1 AND votes.question_id = q.id
|
||||
), 0)::bigint AS user_vote
|
||||
FROM questions q
|
||||
JOIN users u ON u.id = q.author_id
|
||||
LEFT JOIN answers a ON a.question_id = q.id
|
||||
@@ -49,8 +52,8 @@ WHERE q.id = $2
|
||||
`
|
||||
|
||||
type GetQuestionParams struct {
|
||||
UserID string
|
||||
ID string
|
||||
ViewerID string
|
||||
ID string
|
||||
}
|
||||
|
||||
type GetQuestionRow struct {
|
||||
@@ -69,7 +72,7 @@ type GetQuestionRow struct {
|
||||
}
|
||||
|
||||
func (q *Queries) GetQuestion(ctx context.Context, arg GetQuestionParams) (GetQuestionRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getQuestion, arg.UserID, arg.ID)
|
||||
row := q.db.QueryRowContext(ctx, getQuestion, arg.ViewerID, arg.ID)
|
||||
var i GetQuestionRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
@@ -103,7 +106,10 @@ const listHunt = `-- name: ListHunt :many
|
||||
SELECT q.id, q.author_id, u.name AS author_name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at,
|
||||
COALESCE(SUM(v.value), 0)::bigint AS score,
|
||||
CASE WHEN a.question_id IS NULL THEN 0 ELSE 1 END::bigint AS answered,
|
||||
COALESCE((SELECT value FROM votes WHERE votes.user_id = $1 AND votes.question_id = q.id), 0)::bigint AS user_vote
|
||||
COALESCE((
|
||||
SELECT votes.value FROM votes
|
||||
WHERE votes.user_id = $1 AND votes.question_id = q.id
|
||||
), 0)::bigint AS user_vote
|
||||
FROM questions q
|
||||
JOIN users u ON u.id = q.author_id
|
||||
LEFT JOIN votes v ON v.question_id = q.id
|
||||
@@ -114,7 +120,7 @@ ORDER BY score DESC, q.created_at ASC
|
||||
`
|
||||
|
||||
type ListHuntParams struct {
|
||||
UserID string
|
||||
ViewerID string
|
||||
HuntDate string
|
||||
}
|
||||
|
||||
@@ -134,7 +140,7 @@ type ListHuntRow struct {
|
||||
}
|
||||
|
||||
func (q *Queries) ListHunt(ctx context.Context, arg ListHuntParams) ([]ListHuntRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listHunt, arg.UserID, arg.HuntDate)
|
||||
rows, err := q.db.QueryContext(ctx, listHunt, arg.ViewerID, arg.HuntDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -171,7 +177,7 @@ func (q *Queries) ListHunt(ctx context.Context, arg ListHuntParams) ([]ListHuntR
|
||||
|
||||
const listQuestionsAnsweredBy = `-- name: ListQuestionsAnsweredBy :many
|
||||
SELECT q.id, q.author_id, u.name AS author_name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at,
|
||||
COALESCE((SELECT SUM(value) FROM votes WHERE votes.question_id = q.id), 0)::bigint AS score,
|
||||
COALESCE((SELECT SUM(votes.value) FROM votes WHERE votes.question_id = q.id), 0)::bigint AS score,
|
||||
1::bigint AS answered,
|
||||
0::bigint AS user_vote
|
||||
FROM answers ans
|
||||
@@ -196,8 +202,8 @@ type ListQuestionsAnsweredByRow struct {
|
||||
UserVote int64
|
||||
}
|
||||
|
||||
func (q *Queries) ListQuestionsAnsweredBy(ctx context.Context, authorID string) ([]ListQuestionsAnsweredByRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listQuestionsAnsweredBy, authorID)
|
||||
func (q *Queries) ListQuestionsAnsweredBy(ctx context.Context, adminID string) ([]ListQuestionsAnsweredByRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listQuestionsAnsweredBy, adminID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -234,7 +240,7 @@ func (q *Queries) ListQuestionsAnsweredBy(ctx context.Context, authorID string)
|
||||
|
||||
const listQuestionsByAuthor = `-- name: ListQuestionsByAuthor :many
|
||||
SELECT q.id, q.author_id, u.name AS author_name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at,
|
||||
COALESCE((SELECT SUM(value) FROM votes WHERE votes.question_id = q.id), 0)::bigint AS score,
|
||||
COALESCE((SELECT SUM(votes.value) FROM votes WHERE votes.question_id = q.id), 0)::bigint AS score,
|
||||
CASE WHEN a.question_id IS NULL THEN 0 ELSE 1 END::bigint AS answered,
|
||||
0::bigint AS user_vote
|
||||
FROM questions q
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: sessions.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const deleteExpiredSessions = `-- name: DeleteExpiredSessions :exec
|
||||
DELETE FROM sessions
|
||||
WHERE expiry <= now()
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteExpiredSessions(ctx context.Context) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteExpiredSessions)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteSession = `-- name: DeleteSession :exec
|
||||
DELETE FROM sessions
|
||||
WHERE token = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSession(ctx context.Context, token string) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteSession, token)
|
||||
return err
|
||||
}
|
||||
|
||||
const getSession = `-- name: GetSession :one
|
||||
SELECT data
|
||||
FROM sessions
|
||||
WHERE token = $1 AND expiry > now()
|
||||
`
|
||||
|
||||
func (q *Queries) GetSession(ctx context.Context, token string) ([]byte, error) {
|
||||
row := q.db.QueryRowContext(ctx, getSession, token)
|
||||
var data []byte
|
||||
err := row.Scan(&data)
|
||||
return data, err
|
||||
}
|
||||
|
||||
const upsertSession = `-- name: UpsertSession :exec
|
||||
INSERT INTO sessions (token, data, expiry)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (token) DO UPDATE
|
||||
SET data = excluded.data, expiry = excluded.expiry
|
||||
`
|
||||
|
||||
type UpsertSessionParams struct {
|
||||
Token string
|
||||
Data []byte
|
||||
Expiry time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertSession(ctx context.Context, arg UpsertSessionParams) error {
|
||||
_, err := q.db.ExecContext(ctx, upsertSession, arg.Token, arg.Data, arg.Expiry)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user