feat: new RollbackOnError method added for transactional db helper and integrated into auth service
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
// Copyright (c) 2025 Nikolai Papin
|
||||
//
|
||||
//
|
||||
// This file is part of Easywish
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||
// the GNU General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
@@ -26,18 +26,19 @@ import (
|
||||
type DbHelperTransaction interface {
|
||||
Commit() error
|
||||
Rollback() error
|
||||
RollbackOnError(err error) error
|
||||
}
|
||||
|
||||
type DbHelper struct {
|
||||
CTX context.Context
|
||||
Queries Queries
|
||||
CTX context.Context
|
||||
Queries Queries
|
||||
}
|
||||
|
||||
type dbHelperTransactionImpl struct {
|
||||
CTX context.Context
|
||||
TXlessQueries Queries
|
||||
TX pgx.Tx
|
||||
TXQueries Queries
|
||||
CTX context.Context
|
||||
TXlessQueries Queries
|
||||
TX pgx.Tx
|
||||
TXQueries Queries
|
||||
}
|
||||
|
||||
func NewDbHelper(dbContext DbContext) DbHelper {
|
||||
@@ -64,10 +65,10 @@ func NewDbHelperTransaction(dbContext DbContext) (DbHelperTransaction, *dbHelper
|
||||
txQueries := queries.WithTx(tx)
|
||||
|
||||
obj := &dbHelperTransactionImpl{
|
||||
CTX: ctx,
|
||||
TXlessQueries: *queries,
|
||||
TX: tx,
|
||||
TXQueries: *txQueries,
|
||||
CTX: ctx,
|
||||
TXlessQueries: *queries,
|
||||
TX: tx,
|
||||
TXQueries: *txQueries,
|
||||
}
|
||||
|
||||
return obj, obj, nil
|
||||
@@ -81,7 +82,7 @@ func (d *dbHelperTransactionImpl) Commit() error {
|
||||
errRollback := d.TX.Rollback(d.CTX)
|
||||
|
||||
if errRollback != nil {
|
||||
return errRollback
|
||||
return errRollback
|
||||
}
|
||||
|
||||
return errCommit
|
||||
@@ -98,3 +99,11 @@ func (d *dbHelperTransactionImpl) Rollback() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RollbackOnError implements DbHelperTransaction.
|
||||
func (d *dbHelperTransactionImpl) RollbackOnError(err error) error {
|
||||
if err != nil {
|
||||
return d.Rollback()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequ
|
||||
var err error
|
||||
|
||||
helper, db, _ := database.NewDbHelperTransaction(a.dbctx)
|
||||
defer helper.Rollback()
|
||||
defer helper.RollbackOnError(err)
|
||||
|
||||
// TODO: check occupation with redis
|
||||
|
||||
@@ -199,7 +199,7 @@ func (a *authServiceImpl) RegistrationComplete(request models.RegistrationComple
|
||||
var err error
|
||||
|
||||
helper, db, _ := database.NewDbHelperTransaction(a.dbctx)
|
||||
defer helper.Rollback()
|
||||
defer helper.RollbackOnError(err)
|
||||
|
||||
user, err = db.TXQueries.GetUserByUsername(db.CTX, request.Username)
|
||||
|
||||
@@ -334,11 +334,10 @@ func (a *authServiceImpl) RegistrationComplete(request models.RegistrationComple
|
||||
func (a *authServiceImpl) Login(request models.LoginRequest) (*models.LoginResponse, error) {
|
||||
var userRow database.GetValidUserByLoginCredentialsRow
|
||||
var session database.Session
|
||||
var err error
|
||||
|
||||
helper, db, _ := database.NewDbHelperTransaction(a.dbctx)
|
||||
defer helper.Rollback()
|
||||
|
||||
var err error
|
||||
defer helper.RollbackOnError(err)
|
||||
|
||||
userRow, err = db.TXQueries.GetValidUserByLoginCredentials(db.CTX, database.GetValidUserByLoginCredentialsParams{
|
||||
Username: request.Username,
|
||||
@@ -417,7 +416,7 @@ func (a *authServiceImpl) PasswordResetBegin(request models.PasswordResetBeginRe
|
||||
var err error
|
||||
|
||||
helper, db, err := database.NewDbHelperTransaction(a.dbctx)
|
||||
defer helper.Rollback()
|
||||
defer helper.RollbackOnError(err)
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user