feat: db regular and transactional helpers to reduce boilerplate
This commit is contained in:
77
backend/internal/database/helper.go
Normal file
77
backend/internal/database/helper.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DbHelperTransaction interface {
|
||||||
|
Commit() error
|
||||||
|
Rollback() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type DbHelper struct {
|
||||||
|
CTX context.Context
|
||||||
|
Queries Queries
|
||||||
|
}
|
||||||
|
|
||||||
|
type dbHelperTransactionImpl struct {
|
||||||
|
CTX context.Context
|
||||||
|
queries Queries
|
||||||
|
TX pgx.Tx
|
||||||
|
TXQueries Queries
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDbHelper(dbContext DbContext) DbHelper {
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
queries := New(dbContext)
|
||||||
|
|
||||||
|
return DbHelper{
|
||||||
|
CTX: ctx,
|
||||||
|
Queries: *queries,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDbHelperTransaction(dbContext DbContext) (DbHelperTransaction, *dbHelperTransactionImpl, error) {
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
queries := New(dbContext)
|
||||||
|
tx, err := dbContext.BeginTx(ctx)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
txQueries := queries.WithTx(tx)
|
||||||
|
|
||||||
|
obj := &dbHelperTransactionImpl{
|
||||||
|
CTX: ctx,
|
||||||
|
queries: *queries,
|
||||||
|
TX: tx,
|
||||||
|
TXQueries: *txQueries,
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj, obj, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit implements DbHelperTransaction.
|
||||||
|
func (d *dbHelperTransactionImpl) Commit() error {
|
||||||
|
err := d.TX.Commit(d.CTX)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rollback implements DbHelperTransaction.
|
||||||
|
func (d *dbHelperTransactionImpl) Rollback() error {
|
||||||
|
err := d.TX.Rollback(d.CTX)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"easywish/internal/database"
|
"easywish/internal/database"
|
||||||
errs "easywish/internal/errors"
|
errs "easywish/internal/errors"
|
||||||
"easywish/internal/logger"
|
"easywish/internal/logger"
|
||||||
@@ -29,9 +28,9 @@ func NewAuthService(_log logger.Logger, _dbctx database.DbContext) AuthService {
|
|||||||
|
|
||||||
func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequest) (bool, error) {
|
func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequest) (bool, error) {
|
||||||
|
|
||||||
ctx := context.Background()
|
helper, db, _ := database.NewDbHelperTransaction(a.dbctx)
|
||||||
queries := database.New(a.dbctx)
|
|
||||||
user, err := queries.CreateUser(ctx, request.Username) // TODO: validation
|
user, err := db.TXQueries.CreateUser(db.CTX, request.Username) // TODO: validation
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.log.Get().Error("Failed to add user to database", zap.Error(err))
|
a.log.Get().Error("Failed to add user to database", zap.Error(err))
|
||||||
@@ -39,6 +38,8 @@ func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequ
|
|||||||
}
|
}
|
||||||
a.log.Get().Info("Registered a new user", zap.String("username", user.Username))
|
a.log.Get().Info("Registered a new user", zap.String("username", user.Username))
|
||||||
|
|
||||||
|
helper.Commit()
|
||||||
|
|
||||||
// TODO: Send verification email
|
// TODO: Send verification email
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user