package sql import ( "context" "emperror.dev/errors" "git.sleepycat.moe/sam/mercury/internal/database" "github.com/jackc/pgx/v5" "github.com/keegancsmith/sqlf" "github.com/oklog/ulid/v2" ) // PostStore is the interface to posts in the database. type PostStore struct { q Querier } // NewPostStore creates a new PostStore instance. func NewPostStore(q Querier) *PostStore { return &PostStore{q: q} } // ByID gets a post by its ID. func (s *PostStore) ByID(ctx context.Context, id ulid.ULID) (p database.Post, err error) { q := sqlf.Sprintf("SELECT * FROM posts WHERE id = %s", id) p, err = Get[database.Post](ctx, s.q, q) if err != nil { if errors.Cause(err) == pgx.ErrNoRows { return p, ErrNotFound } return p, errors.Wrap(err, "getting post") } return p, nil } func (s *PostStore) Create(ctx context.Context, blog database.Blog, content string) (p database.Post, err error) { q := sqlf.Sprintf( "INSERT INTO posts (id, blog_id, content, remote) VALUES (%s, %s, %s, %v) RETURNING *", makeULID(), blog.ID, content, blog.Remote()) p, err = Get[database.Post](ctx, s.q, q) if err != nil { return p, errors.Wrap(err, "creating post") } return p, nil } func (s *PostStore) LocalCount(ctx context.Context) (count int64, err error) { q := sqlf.Sprintf("SELECT count(*) FROM posts WHERE remote = false") err = s.q.QueryRow(ctx, q.Query(sqlf.PostgresBindVar)).Scan(&count) if err != nil { return count, errors.Wrap(err, "getting count") } return count, nil }