big ole refactor

This commit is contained in:
2026-02-14 19:48:59 +11:00
parent e34bec2f9d
commit f9283c0563
66 changed files with 989 additions and 1114 deletions

View File

@@ -22,16 +22,15 @@ var timeout = 15 * time.Second
// WithReadTx executes a read-only transaction with automatic rollback
// Returns true if successful, false if error was thrown to client
func WithReadTx(
func (db *DB) WithReadTx(
s *hws.Server,
w http.ResponseWriter,
r *http.Request,
conn *bun.DB,
fn TxFunc,
) bool {
ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
ok, err := withTx(ctx, conn, fn, false)
ok, err := db.withTx(ctx, fn, false)
if err != nil {
throw.InternalServiceError(s, w, r, "Database error", err)
}
@@ -41,31 +40,29 @@ func WithReadTx(
// WithTxFailSilently executes a transaction with automatic rollback
// Returns true if successful, false if error occured.
// Does not throw any errors to the client.
func WithTxFailSilently(
func (db *DB) WithTxFailSilently(
ctx context.Context,
conn *bun.DB,
fn TxFuncSilent,
) error {
fnc := func(ctx context.Context, tx bun.Tx) (bool, error) {
err := fn(ctx, tx)
return err == nil, err
}
_, err := withTx(ctx, conn, fnc, true)
_, err := db.withTx(ctx, fnc, true)
return err
}
// WithWriteTx executes a write transaction with automatic rollback on error
// Commits only if fn returns nil. Returns true if successful.
func WithWriteTx(
func (db *DB) WithWriteTx(
s *hws.Server,
w http.ResponseWriter,
r *http.Request,
conn *bun.DB,
fn TxFunc,
) bool {
ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
ok, err := withTx(ctx, conn, fn, true)
ok, err := db.withTx(ctx, fn, true)
if err != nil {
throw.InternalServiceError(s, w, r, "Database error", err)
}
@@ -74,16 +71,15 @@ func WithWriteTx(
// WithNotifyTx executes a transaction with notification-based error handling
// Uses notifyInternalServiceError instead of throwInternalServiceError
func WithNotifyTx(
func (db *DB) WithNotifyTx(
s *hws.Server,
w http.ResponseWriter,
r *http.Request,
conn *bun.DB,
fn TxFunc,
) bool {
ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
ok, err := withTx(ctx, conn, fn, true)
ok, err := db.withTx(ctx, fn, true)
if err != nil {
notify.InternalServiceError(s, w, r, "Database error", err)
}
@@ -91,13 +87,12 @@ func WithNotifyTx(
}
// withTx executes a transaction with automatic rollback on error
func withTx(
func (db *DB) withTx(
ctx context.Context,
conn *bun.DB,
fn TxFunc,
write bool,
) (bool, error) {
tx, err := conn.BeginTx(ctx, nil)
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return false, errors.Wrap(err, "conn.BeginTx")
}