add a couple post endpoints + /timelines/home

This commit is contained in:
sam 2023-09-06 16:32:33 +02:00
parent dd72a1f4c1
commit 9f052dc9ef
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
24 changed files with 462 additions and 32 deletions

View file

@ -8,8 +8,11 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/keegancsmith/sqlf"
"github.com/rs/zerolog/log"
)
var LogQueries = true
type Querier interface {
Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
@ -32,6 +35,10 @@ type Tx interface {
func Select[T any](ctx context.Context, querier Querier, query *sqlf.Query) ([]T, error) {
dst := make([]T, 0)
if LogQueries {
log.Debug().Str("query", query.Query(sqlf.PostgresBindVar)).Msg("executing select query")
}
err := pgxscan.Select(ctx, querier, &dst, query.Query(sqlf.PostgresBindVar), query.Args()...)
if err != nil {
return []T{}, errors.Wrap(err, "executing query")
@ -42,6 +49,10 @@ func Select[T any](ctx context.Context, querier Querier, query *sqlf.Query) ([]T
func Get[T any](ctx context.Context, querier Querier, query *sqlf.Query) (T, error) {
var dst T
if LogQueries {
log.Debug().Str("query", query.Query(sqlf.PostgresBindVar)).Msg("executing get query")
}
err := pgxscan.Get(ctx, querier, &dst, query.Query(sqlf.PostgresBindVar), query.Args()...)
if err != nil {
return dst, errors.Wrap(err, "executing query")
@ -50,6 +61,10 @@ func Get[T any](ctx context.Context, querier Querier, query *sqlf.Query) (T, err
}
func Exec(ctx context.Context, querier Querier, query *sqlf.Query) error {
if LogQueries {
log.Debug().Str("query", query.Query(sqlf.PostgresBindVar)).Msg("executing exec query")
}
_, err := querier.Exec(ctx, query.Query(sqlf.PostgresBindVar), query.Args()...)
return err
}