Store post state as text so Go owns the allowed values and future states such as locked remain representable without a database enum migration.
This commit is contained in:
@@ -25,7 +25,7 @@ type Post struct {
|
||||
Body string
|
||||
City string
|
||||
PostDate string
|
||||
Hidden int32
|
||||
PostState string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
const createPost = `-- name: CreatePost :exec
|
||||
INSERT INTO posts (
|
||||
id, parent_id, author_id, title, body, city, post_date, hidden, created_at, updated_at
|
||||
id, parent_id, author_id, title, body, city, post_date, post_state, created_at, updated_at
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
@@ -36,7 +36,7 @@ type CreatePostParams struct {
|
||||
Body string
|
||||
City string
|
||||
PostDate string
|
||||
Hidden int32
|
||||
PostState string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func (q *Queries) CreatePost(ctx context.Context, arg CreatePostParams) error {
|
||||
arg.Body,
|
||||
arg.City,
|
||||
arg.PostDate,
|
||||
arg.Hidden,
|
||||
arg.PostState,
|
||||
arg.CreatedAt,
|
||||
arg.UpdatedAt,
|
||||
)
|
||||
@@ -76,7 +76,7 @@ func (q *Queries) DeletePostVote(ctx context.Context, arg DeletePostVoteParams)
|
||||
const getPost = `-- name: GetPost :one
|
||||
SELECT
|
||||
p.id, p.parent_id, p.author_id, u.name AS author_name, u.role AS author_role,
|
||||
p.title, p.body, p.city, p.post_date, p.hidden, p.created_at, p.updated_at
|
||||
p.title, p.body, p.city, p.post_date, p.post_state, p.created_at, p.updated_at
|
||||
FROM posts p
|
||||
JOIN users u ON u.id = p.author_id
|
||||
WHERE p.id = $1
|
||||
@@ -92,7 +92,7 @@ type GetPostRow struct {
|
||||
Body string
|
||||
City string
|
||||
PostDate string
|
||||
Hidden int32
|
||||
PostState string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func (q *Queries) GetPost(ctx context.Context, id string) (GetPostRow, error) {
|
||||
&i.Body,
|
||||
&i.City,
|
||||
&i.PostDate,
|
||||
&i.Hidden,
|
||||
&i.PostState,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
@@ -119,13 +119,13 @@ func (q *Queries) GetPost(ctx context.Context, id string) (GetPostRow, error) {
|
||||
|
||||
const listPostThread = `-- name: ListPostThread :many
|
||||
WITH RECURSIVE thread AS (
|
||||
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.hidden, p.created_at, p.updated_at
|
||||
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.post_state, p.created_at, p.updated_at
|
||||
FROM posts p
|
||||
WHERE p.id = $1 AND p.parent_id IS NULL
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT child.id, child.parent_id, child.author_id, child.title, child.body, child.city, child.post_date, child.hidden, child.created_at, child.updated_at
|
||||
SELECT child.id, child.parent_id, child.author_id, child.title, child.body, child.city, child.post_date, child.post_state, child.created_at, child.updated_at
|
||||
FROM posts child
|
||||
JOIN thread parent ON child.parent_id = parent.id
|
||||
)
|
||||
@@ -133,7 +133,7 @@ SELECT
|
||||
thread.id, thread.parent_id, thread.author_id,
|
||||
u.name AS author_name, u.role AS author_role,
|
||||
thread.title, thread.body, thread.city, thread.post_date,
|
||||
thread.hidden, thread.created_at, thread.updated_at
|
||||
thread.post_state, thread.created_at, thread.updated_at
|
||||
FROM thread
|
||||
JOIN users u ON u.id = thread.author_id
|
||||
ORDER BY thread.created_at, thread.id
|
||||
@@ -149,7 +149,7 @@ type ListPostThreadRow struct {
|
||||
Body string
|
||||
City string
|
||||
PostDate string
|
||||
Hidden int32
|
||||
PostState string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
@@ -173,7 +173,7 @@ func (q *Queries) ListPostThread(ctx context.Context, rootID string) ([]ListPost
|
||||
&i.Body,
|
||||
&i.City,
|
||||
&i.PostDate,
|
||||
&i.Hidden,
|
||||
&i.PostState,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
@@ -192,11 +192,11 @@ func (q *Queries) ListPostThread(ctx context.Context, rootID string) ([]ListPost
|
||||
|
||||
const listRootPosts = `-- name: ListRootPosts :many
|
||||
WITH RECURSIVE roots AS (
|
||||
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.hidden, p.created_at, p.updated_at
|
||||
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.post_state, p.created_at, p.updated_at
|
||||
FROM posts p
|
||||
WHERE p.parent_id IS NULL
|
||||
AND p.post_date = $3
|
||||
AND p.hidden = 0
|
||||
AND p.post_state <> $4
|
||||
),
|
||||
thread AS (
|
||||
SELECT roots.id AS root_id, child.id AS post_id, child.author_id
|
||||
@@ -225,7 +225,7 @@ SELECT
|
||||
roots.id, roots.parent_id, roots.author_id,
|
||||
u.name AS author_name, u.role AS author_role,
|
||||
roots.title, roots.body, roots.city, roots.post_date,
|
||||
roots.hidden, roots.created_at, roots.updated_at,
|
||||
roots.post_state, roots.created_at, roots.updated_at,
|
||||
COALESCE(scores.score, 0)::bigint AS score,
|
||||
(answered.root_id IS NOT NULL)::bool AS answered,
|
||||
COALESCE(viewer_vote.value, 0)::bigint AS user_vote
|
||||
@@ -241,9 +241,10 @@ LIMIT $2
|
||||
`
|
||||
|
||||
type ListRootPostsParams struct {
|
||||
ViewerID string
|
||||
RowLimit int32
|
||||
PostDate string
|
||||
ViewerID string
|
||||
RowLimit int32
|
||||
PostDate string
|
||||
HiddenState string
|
||||
}
|
||||
|
||||
type ListRootPostsRow struct {
|
||||
@@ -256,7 +257,7 @@ type ListRootPostsRow struct {
|
||||
Body string
|
||||
City string
|
||||
PostDate string
|
||||
Hidden int32
|
||||
PostState string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
Score int64
|
||||
@@ -265,7 +266,12 @@ type ListRootPostsRow struct {
|
||||
}
|
||||
|
||||
func (q *Queries) ListRootPosts(ctx context.Context, arg ListRootPostsParams) ([]ListRootPostsRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listRootPosts, arg.ViewerID, arg.RowLimit, arg.PostDate)
|
||||
rows, err := q.db.QueryContext(ctx, listRootPosts,
|
||||
arg.ViewerID,
|
||||
arg.RowLimit,
|
||||
arg.PostDate,
|
||||
arg.HiddenState,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -283,7 +289,7 @@ func (q *Queries) ListRootPosts(ctx context.Context, arg ListRootPostsParams) ([
|
||||
&i.Body,
|
||||
&i.City,
|
||||
&i.PostDate,
|
||||
&i.Hidden,
|
||||
&i.PostState,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Score,
|
||||
@@ -307,12 +313,19 @@ const postIsVisibleRoot = `-- name: PostIsVisibleRoot :one
|
||||
SELECT EXISTS(
|
||||
SELECT 1
|
||||
FROM posts
|
||||
WHERE id = $1 AND parent_id IS NULL AND hidden = 0
|
||||
WHERE id = $1
|
||||
AND parent_id IS NULL
|
||||
AND post_state <> $2
|
||||
)::bool
|
||||
`
|
||||
|
||||
func (q *Queries) PostIsVisibleRoot(ctx context.Context, id string) (bool, error) {
|
||||
row := q.db.QueryRowContext(ctx, postIsVisibleRoot, id)
|
||||
type PostIsVisibleRootParams struct {
|
||||
ID string
|
||||
HiddenState string
|
||||
}
|
||||
|
||||
func (q *Queries) PostIsVisibleRoot(ctx context.Context, arg PostIsVisibleRootParams) (bool, error) {
|
||||
row := q.db.QueryRowContext(ctx, postIsVisibleRoot, arg.ID, arg.HiddenState)
|
||||
var column_1 bool
|
||||
err := row.Scan(&column_1)
|
||||
return column_1, err
|
||||
@@ -346,19 +359,25 @@ SELECT $1, $2, $3
|
||||
FROM posts p
|
||||
WHERE p.id = $2
|
||||
AND p.parent_id IS NULL
|
||||
AND p.hidden = 0
|
||||
AND p.post_state <> $4
|
||||
ON CONFLICT (user_id, post_id) DO UPDATE
|
||||
SET value = excluded.value
|
||||
`
|
||||
|
||||
type UpsertPostVoteOnVisibleRootParams struct {
|
||||
UserID string
|
||||
PostID string
|
||||
Value int32
|
||||
UserID string
|
||||
PostID string
|
||||
Value int32
|
||||
HiddenState string
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertPostVoteOnVisibleRoot(ctx context.Context, arg UpsertPostVoteOnVisibleRootParams) (int64, error) {
|
||||
result, err := q.db.ExecContext(ctx, upsertPostVoteOnVisibleRoot, arg.UserID, arg.PostID, arg.Value)
|
||||
result, err := q.db.ExecContext(ctx, upsertPostVoteOnVisibleRoot,
|
||||
arg.UserID,
|
||||
arg.PostID,
|
||||
arg.Value,
|
||||
arg.HiddenState,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user