Adopt sqlc for typed Postgres queries behind store entities.
This commit is contained in:
@@ -0,0 +1,296 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: questions.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const createQuestion = `-- name: CreateQuestion :exec
|
||||
INSERT INTO questions (id, author_id, title, body, city, hunt_date, hidden, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, 0, $7)
|
||||
`
|
||||
|
||||
type CreateQuestionParams struct {
|
||||
ID string
|
||||
AuthorID string
|
||||
Title string
|
||||
Body string
|
||||
City string
|
||||
HuntDate string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateQuestion(ctx context.Context, arg CreateQuestionParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createQuestion,
|
||||
arg.ID,
|
||||
arg.AuthorID,
|
||||
arg.Title,
|
||||
arg.Body,
|
||||
arg.City,
|
||||
arg.HuntDate,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
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,
|
||||
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
|
||||
FROM questions q
|
||||
JOIN users u ON u.id = q.author_id
|
||||
LEFT JOIN answers a ON a.question_id = q.id
|
||||
WHERE q.id = $2
|
||||
`
|
||||
|
||||
type GetQuestionParams struct {
|
||||
UserID string
|
||||
ID string
|
||||
}
|
||||
|
||||
type GetQuestionRow struct {
|
||||
ID string
|
||||
AuthorID string
|
||||
AuthorName string
|
||||
Title string
|
||||
Body string
|
||||
City string
|
||||
HuntDate string
|
||||
Hidden int32
|
||||
CreatedAt string
|
||||
Score int64
|
||||
Answered int64
|
||||
UserVote int64
|
||||
}
|
||||
|
||||
func (q *Queries) GetQuestion(ctx context.Context, arg GetQuestionParams) (GetQuestionRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getQuestion, arg.UserID, arg.ID)
|
||||
var i GetQuestionRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.AuthorID,
|
||||
&i.AuthorName,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.City,
|
||||
&i.HuntDate,
|
||||
&i.Hidden,
|
||||
&i.CreatedAt,
|
||||
&i.Score,
|
||||
&i.Answered,
|
||||
&i.UserVote,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const hideQuestion = `-- name: HideQuestion :exec
|
||||
UPDATE questions
|
||||
SET hidden = 1
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) HideQuestion(ctx context.Context, id string) error {
|
||||
_, err := q.db.ExecContext(ctx, hideQuestion, id)
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
FROM questions q
|
||||
JOIN users u ON u.id = q.author_id
|
||||
LEFT JOIN votes v ON v.question_id = q.id
|
||||
LEFT JOIN answers a ON a.question_id = q.id
|
||||
WHERE q.hunt_date = $2 AND q.hidden = 0
|
||||
GROUP BY q.id, q.author_id, u.name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at, a.question_id
|
||||
ORDER BY score DESC, q.created_at ASC
|
||||
`
|
||||
|
||||
type ListHuntParams struct {
|
||||
UserID string
|
||||
HuntDate string
|
||||
}
|
||||
|
||||
type ListHuntRow struct {
|
||||
ID string
|
||||
AuthorID string
|
||||
AuthorName string
|
||||
Title string
|
||||
Body string
|
||||
City string
|
||||
HuntDate string
|
||||
Hidden int32
|
||||
CreatedAt string
|
||||
Score int64
|
||||
Answered int64
|
||||
UserVote int64
|
||||
}
|
||||
|
||||
func (q *Queries) ListHunt(ctx context.Context, arg ListHuntParams) ([]ListHuntRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listHunt, arg.UserID, arg.HuntDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListHuntRow{}
|
||||
for rows.Next() {
|
||||
var i ListHuntRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.AuthorID,
|
||||
&i.AuthorName,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.City,
|
||||
&i.HuntDate,
|
||||
&i.Hidden,
|
||||
&i.CreatedAt,
|
||||
&i.Score,
|
||||
&i.Answered,
|
||||
&i.UserVote,
|
||||
); 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 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,
|
||||
1::bigint AS answered,
|
||||
0::bigint AS user_vote
|
||||
FROM answers ans
|
||||
JOIN questions q ON q.id = ans.question_id
|
||||
JOIN users u ON u.id = q.author_id
|
||||
WHERE ans.author_id = $1 AND q.hidden = 0
|
||||
ORDER BY ans.updated_at DESC
|
||||
`
|
||||
|
||||
type ListQuestionsAnsweredByRow struct {
|
||||
ID string
|
||||
AuthorID string
|
||||
AuthorName string
|
||||
Title string
|
||||
Body string
|
||||
City string
|
||||
HuntDate string
|
||||
Hidden int32
|
||||
CreatedAt string
|
||||
Score int64
|
||||
Answered int64
|
||||
UserVote int64
|
||||
}
|
||||
|
||||
func (q *Queries) ListQuestionsAnsweredBy(ctx context.Context, authorID string) ([]ListQuestionsAnsweredByRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listQuestionsAnsweredBy, authorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListQuestionsAnsweredByRow{}
|
||||
for rows.Next() {
|
||||
var i ListQuestionsAnsweredByRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.AuthorID,
|
||||
&i.AuthorName,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.City,
|
||||
&i.HuntDate,
|
||||
&i.Hidden,
|
||||
&i.CreatedAt,
|
||||
&i.Score,
|
||||
&i.Answered,
|
||||
&i.UserVote,
|
||||
); 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 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,
|
||||
CASE WHEN a.question_id IS NULL THEN 0 ELSE 1 END::bigint AS answered,
|
||||
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
|
||||
WHERE q.author_id = $1 AND q.hidden = 0
|
||||
ORDER BY q.created_at DESC
|
||||
`
|
||||
|
||||
type ListQuestionsByAuthorRow struct {
|
||||
ID string
|
||||
AuthorID string
|
||||
AuthorName string
|
||||
Title string
|
||||
Body string
|
||||
City string
|
||||
HuntDate string
|
||||
Hidden int32
|
||||
CreatedAt string
|
||||
Score int64
|
||||
Answered int64
|
||||
UserVote int64
|
||||
}
|
||||
|
||||
func (q *Queries) ListQuestionsByAuthor(ctx context.Context, authorID string) ([]ListQuestionsByAuthorRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listQuestionsByAuthor, authorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListQuestionsByAuthorRow{}
|
||||
for rows.Next() {
|
||||
var i ListQuestionsByAuthorRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.AuthorID,
|
||||
&i.AuthorName,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.City,
|
||||
&i.HuntDate,
|
||||
&i.Hidden,
|
||||
&i.CreatedAt,
|
||||
&i.Score,
|
||||
&i.Answered,
|
||||
&i.UserVote,
|
||||
); 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
|
||||
}
|
||||
Reference in New Issue
Block a user