// Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.31.1 // source: posts.sql package sqlc import ( "context" "database/sql" ) const createPost = `-- name: CreatePost :exec INSERT INTO posts ( id, parent_id, author_id, title, body, city, post_date, post_state, created_at, updated_at ) VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10 ) ` type CreatePostParams struct { ID string ParentID sql.NullString AuthorID string Title string Body string City string PostDate string PostState string CreatedAt string UpdatedAt string } func (q *Queries) CreatePost(ctx context.Context, arg CreatePostParams) error { _, err := q.db.ExecContext(ctx, createPost, arg.ID, arg.ParentID, arg.AuthorID, arg.Title, arg.Body, arg.City, arg.PostDate, arg.PostState, arg.CreatedAt, arg.UpdatedAt, ) return err } const deletePostVote = `-- name: DeletePostVote :exec DELETE FROM post_votes WHERE user_id = $1 AND post_id = $2 ` type DeletePostVoteParams struct { UserID string PostID string } func (q *Queries) DeletePostVote(ctx context.Context, arg DeletePostVoteParams) error { _, err := q.db.ExecContext(ctx, deletePostVote, arg.UserID, arg.PostID) return err } 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.post_state, p.created_at, p.updated_at FROM posts p JOIN users u ON u.id = p.author_id WHERE p.id = $1 ` type GetPostRow struct { ID string ParentID sql.NullString AuthorID string AuthorName string AuthorRole string Title string Body string City string PostDate string PostState string CreatedAt string UpdatedAt string } func (q *Queries) GetPost(ctx context.Context, id string) (GetPostRow, error) { row := q.db.QueryRowContext(ctx, getPost, id) var i GetPostRow err := row.Scan( &i.ID, &i.ParentID, &i.AuthorID, &i.AuthorName, &i.AuthorRole, &i.Title, &i.Body, &i.City, &i.PostDate, &i.PostState, &i.CreatedAt, &i.UpdatedAt, ) return i, err } const getRootPostVoteSummary = `-- name: GetRootPostVoteSummary :one SELECT COALESCE(SUM(value), 0)::bigint AS score, COALESCE( MAX(value) FILTER (WHERE user_id = $1), 0 )::bigint AS user_vote FROM post_votes WHERE post_id = $2 ` type GetRootPostVoteSummaryParams struct { ViewerID string RootID string } type GetRootPostVoteSummaryRow struct { Score int64 UserVote int64 } func (q *Queries) GetRootPostVoteSummary(ctx context.Context, arg GetRootPostVoteSummaryParams) (GetRootPostVoteSummaryRow, error) { row := q.db.QueryRowContext(ctx, getRootPostVoteSummary, arg.ViewerID, arg.RootID) var i GetRootPostVoteSummaryRow err := row.Scan(&i.Score, &i.UserVote) return i, err } 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.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.post_state, child.created_at, child.updated_at FROM posts child JOIN thread parent ON child.parent_id = parent.id ) 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.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 ` type ListPostThreadRow struct { ID string ParentID sql.NullString AuthorID string AuthorName string AuthorRole string Title string Body string City string PostDate string PostState string CreatedAt string UpdatedAt string } func (q *Queries) ListPostThread(ctx context.Context, rootID string) ([]ListPostThreadRow, error) { rows, err := q.db.QueryContext(ctx, listPostThread, rootID) if err != nil { return nil, err } defer rows.Close() items := []ListPostThreadRow{} for rows.Next() { var i ListPostThreadRow if err := rows.Scan( &i.ID, &i.ParentID, &i.AuthorID, &i.AuthorName, &i.AuthorRole, &i.Title, &i.Body, &i.City, &i.PostDate, &i.PostState, &i.CreatedAt, &i.UpdatedAt, ); 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 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.post_state, p.created_at, p.updated_at FROM posts p WHERE p.parent_id IS NULL AND p.post_date = $3 AND p.post_state <> $4 ), thread AS ( SELECT roots.id AS root_id, child.id AS post_id, child.author_id FROM roots JOIN posts child ON child.parent_id = roots.id UNION ALL SELECT thread.root_id, child.id, child.author_id FROM thread JOIN posts child ON child.parent_id = thread.post_id ), answered AS ( SELECT DISTINCT thread.root_id FROM thread JOIN users u ON u.id = thread.author_id WHERE u.role = 'admin' ), scores AS ( SELECT votes.post_id, SUM(votes.value)::bigint AS score FROM roots JOIN post_votes votes ON votes.post_id = roots.id GROUP BY votes.post_id ) 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.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 FROM roots JOIN users u ON u.id = roots.author_id LEFT JOIN scores ON scores.post_id = roots.id LEFT JOIN answered ON answered.root_id = roots.id LEFT JOIN post_votes viewer_vote ON viewer_vote.user_id = $1 AND viewer_vote.post_id = roots.id ORDER BY score DESC, roots.created_at, roots.id LIMIT $2 ` type ListRootPostsParams struct { ViewerID string RowLimit int32 PostDate string HiddenState string } type ListRootPostsRow struct { ID string ParentID sql.NullString AuthorID string AuthorName string AuthorRole string Title string Body string City string PostDate string PostState string CreatedAt string UpdatedAt string Score int64 Answered bool UserVote int64 } func (q *Queries) ListRootPosts(ctx context.Context, arg ListRootPostsParams) ([]ListRootPostsRow, error) { rows, err := q.db.QueryContext(ctx, listRootPosts, arg.ViewerID, arg.RowLimit, arg.PostDate, arg.HiddenState, ) if err != nil { return nil, err } defer rows.Close() items := []ListRootPostsRow{} for rows.Next() { var i ListRootPostsRow if err := rows.Scan( &i.ID, &i.ParentID, &i.AuthorID, &i.AuthorName, &i.AuthorRole, &i.Title, &i.Body, &i.City, &i.PostDate, &i.PostState, &i.CreatedAt, &i.UpdatedAt, &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 listRootPostsAnsweredBy = `-- name: ListRootPostsAnsweredBy :many WITH RECURSIVE ancestors AS ( SELECT p.id, p.parent_id FROM posts p WHERE p.author_id = $3 AND p.parent_id IS NOT NULL UNION SELECT parent.id, parent.parent_id FROM posts parent JOIN ancestors child ON child.parent_id = parent.id ) SELECT DISTINCT root.id, root.parent_id, root.author_id, u.name AS author_name, u.role AS author_role, root.title, root.body, root.city, root.post_date, root.post_state, root.created_at, root.updated_at FROM posts root JOIN ancestors ON ancestors.id = root.id JOIN users u ON u.id = root.author_id WHERE root.parent_id IS NULL AND root.post_state <> $1 ORDER BY root.created_at DESC, root.id DESC LIMIT $2 ` type ListRootPostsAnsweredByParams struct { HiddenState string RowLimit int32 AdminID string } type ListRootPostsAnsweredByRow struct { ID string ParentID sql.NullString AuthorID string AuthorName string AuthorRole string Title string Body string City string PostDate string PostState string CreatedAt string UpdatedAt string } func (q *Queries) ListRootPostsAnsweredBy(ctx context.Context, arg ListRootPostsAnsweredByParams) ([]ListRootPostsAnsweredByRow, error) { rows, err := q.db.QueryContext(ctx, listRootPostsAnsweredBy, arg.HiddenState, arg.RowLimit, arg.AdminID) if err != nil { return nil, err } defer rows.Close() items := []ListRootPostsAnsweredByRow{} for rows.Next() { var i ListRootPostsAnsweredByRow if err := rows.Scan( &i.ID, &i.ParentID, &i.AuthorID, &i.AuthorName, &i.AuthorRole, &i.Title, &i.Body, &i.City, &i.PostDate, &i.PostState, &i.CreatedAt, &i.UpdatedAt, ); 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 listRootPostsByAuthor = `-- name: ListRootPostsByAuthor :many 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.post_state, p.created_at, p.updated_at FROM posts p JOIN users u ON u.id = p.author_id WHERE p.parent_id IS NULL AND p.author_id = $1 AND p.post_state <> $2 ORDER BY p.created_at DESC, p.id DESC LIMIT $3 ` type ListRootPostsByAuthorParams struct { AuthorID string HiddenState string RowLimit int32 } type ListRootPostsByAuthorRow struct { ID string ParentID sql.NullString AuthorID string AuthorName string AuthorRole string Title string Body string City string PostDate string PostState string CreatedAt string UpdatedAt string } func (q *Queries) ListRootPostsByAuthor(ctx context.Context, arg ListRootPostsByAuthorParams) ([]ListRootPostsByAuthorRow, error) { rows, err := q.db.QueryContext(ctx, listRootPostsByAuthor, arg.AuthorID, arg.HiddenState, arg.RowLimit) if err != nil { return nil, err } defer rows.Close() items := []ListRootPostsByAuthorRow{} for rows.Next() { var i ListRootPostsByAuthorRow if err := rows.Scan( &i.ID, &i.ParentID, &i.AuthorID, &i.AuthorName, &i.AuthorRole, &i.Title, &i.Body, &i.City, &i.PostDate, &i.PostState, &i.CreatedAt, &i.UpdatedAt, ); 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 postIsVisibleRoot = `-- name: PostIsVisibleRoot :one SELECT EXISTS( SELECT 1 FROM posts WHERE id = $1 AND parent_id IS NULL AND post_state <> $2 )::bool ` 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 } const updatePost = `-- name: UpdatePost :execrows UPDATE posts SET body = $1, updated_at = $2 WHERE id = $3 ` type UpdatePostParams struct { Body string UpdatedAt string ID string } func (q *Queries) UpdatePost(ctx context.Context, arg UpdatePostParams) (int64, error) { result, err := q.db.ExecContext(ctx, updatePost, arg.Body, arg.UpdatedAt, arg.ID) if err != nil { return 0, err } return result.RowsAffected() } const updateRootPostState = `-- name: UpdateRootPostState :execrows UPDATE posts SET post_state = $1, updated_at = $2 WHERE id = $3 AND parent_id IS NULL ` type UpdateRootPostStateParams struct { PostState string UpdatedAt string ID string } func (q *Queries) UpdateRootPostState(ctx context.Context, arg UpdateRootPostStateParams) (int64, error) { result, err := q.db.ExecContext(ctx, updateRootPostState, arg.PostState, arg.UpdatedAt, arg.ID) if err != nil { return 0, err } return result.RowsAffected() } const upsertPostVoteOnVisibleRoot = `-- name: UpsertPostVoteOnVisibleRoot :execrows INSERT INTO post_votes (user_id, post_id, value) SELECT $1, $2, $3 FROM posts p WHERE p.id = $2 AND p.parent_id IS NULL 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 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, arg.HiddenState, ) if err != nil { return 0, err } return result.RowsAffected() }