HN-like selfhosted forums
// Code generated by sqlc. DO NOT EDIT.// versions:// sqlc v1.31.1// source: comments.sqlpackage storeimport ( "context" "time" "github.com/google/uuid")const createComment = `-- name: CreateComment :oneINSERT INTO comments(post_id, parent_id, author_id, body)SELECT $1, $2, $3, $4 -- creates a custom row hereWHERE $2::BIGINT IS NULL -- stop here if parent commentOR EXISTS ( SELECT 1 -- discard, returns true if succeeds FROM comments WHERE id = $2::BIGINT -- check if the parent comment exists on the post AND post_id = $1)RETURNING id, post_id, parent_id, author_id, body, created_at, deleted_at`type CreateCommentParams struct { PostID int64 ParentID *int64 AuthorID uuid.UUID Body string}func (q *Queries) CreateComment(ctx context.Context, arg CreateCommentParams) (Comment, error) { row := q.db.QueryRow(ctx, createComment, arg.PostID, arg.ParentID, arg.AuthorID, arg.Body, ) var i Comment err := row.Scan( &i.ID, &i.PostID, &i.ParentID, &i.AuthorID, &i.Body, &i.CreatedAt, &i.DeletedAt, ) return i, err}const getCommentsByPost = `-- name: GetCommentsByPost :manySELECT c.id, c.post_id, c.parent_id, c.author_id, c.body, c.created_at, c.deleted_at, u.username AS author_usernameFROM comments cJOIN users u ON u.id = c.author_idWHERE c.post_id = $1ORDER BY c.created_at DESC, c.id ASC`type GetCommentsByPostRow struct { ID int64 PostID int64 ParentID *int64 AuthorID uuid.UUID Body string CreatedAt time.Time DeletedAt *time.Time AuthorUsername string}func (q *Queries) GetCommentsByPost(ctx context.Context, postID int64) ([]GetCommentsByPostRow, error) { rows, err := q.db.Query(ctx, getCommentsByPost, postID) if err != nil { return nil, err } defer rows.Close() var items []GetCommentsByPostRow for rows.Next() { var i GetCommentsByPostRow if err := rows.Scan( &i.ID, &i.PostID, &i.ParentID, &i.AuthorID, &i.Body, &i.CreatedAt, &i.DeletedAt, &i.AuthorUsername, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Err(); err != nil { return nil, err } return items, nil}