feat: new RollbackOnError method added for transactional db helper and integrated into auth service

This commit is contained in:
2025-07-13 15:57:34 +03:00
parent a3bebd89be
commit 65ea47dbb6
2 changed files with 29 additions and 21 deletions

View File

@@ -1,17 +1,17 @@
// Copyright (c) 2025 Nikolai Papin // Copyright (c) 2025 Nikolai Papin
// //
// This file is part of Easywish // This file is part of Easywish
// //
// This program is free software: you can redistribute it and/or modify // 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 // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU General Public License for more details. // the GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
@@ -26,18 +26,19 @@ import (
type DbHelperTransaction interface { type DbHelperTransaction interface {
Commit() error Commit() error
Rollback() error Rollback() error
RollbackOnError(err error) error
} }
type DbHelper struct { type DbHelper struct {
CTX context.Context CTX context.Context
Queries Queries Queries Queries
} }
type dbHelperTransactionImpl struct { type dbHelperTransactionImpl struct {
CTX context.Context CTX context.Context
TXlessQueries Queries TXlessQueries Queries
TX pgx.Tx TX pgx.Tx
TXQueries Queries TXQueries Queries
} }
func NewDbHelper(dbContext DbContext) DbHelper { func NewDbHelper(dbContext DbContext) DbHelper {
@@ -64,10 +65,10 @@ func NewDbHelperTransaction(dbContext DbContext) (DbHelperTransaction, *dbHelper
txQueries := queries.WithTx(tx) txQueries := queries.WithTx(tx)
obj := &dbHelperTransactionImpl{ obj := &dbHelperTransactionImpl{
CTX: ctx, CTX: ctx,
TXlessQueries: *queries, TXlessQueries: *queries,
TX: tx, TX: tx,
TXQueries: *txQueries, TXQueries: *txQueries,
} }
return obj, obj, nil return obj, obj, nil
@@ -81,7 +82,7 @@ func (d *dbHelperTransactionImpl) Commit() error {
errRollback := d.TX.Rollback(d.CTX) errRollback := d.TX.Rollback(d.CTX)
if errRollback != nil { if errRollback != nil {
return errRollback return errRollback
} }
return errCommit return errCommit
@@ -98,3 +99,11 @@ func (d *dbHelperTransactionImpl) Rollback() error {
} }
return nil return nil
} }
// RollbackOnError implements DbHelperTransaction.
func (d *dbHelperTransactionImpl) RollbackOnError(err error) error {
if err != nil {
return d.Rollback()
}
return nil
}

View File

@@ -66,7 +66,7 @@ func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequ
var err error var err error
helper, db, _ := database.NewDbHelperTransaction(a.dbctx) helper, db, _ := database.NewDbHelperTransaction(a.dbctx)
defer helper.Rollback() defer helper.RollbackOnError(err)
// TODO: check occupation with redis // TODO: check occupation with redis
@@ -199,7 +199,7 @@ func (a *authServiceImpl) RegistrationComplete(request models.RegistrationComple
var err error var err error
helper, db, _ := database.NewDbHelperTransaction(a.dbctx) helper, db, _ := database.NewDbHelperTransaction(a.dbctx)
defer helper.Rollback() defer helper.RollbackOnError(err)
user, err = db.TXQueries.GetUserByUsername(db.CTX, request.Username) 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) { func (a *authServiceImpl) Login(request models.LoginRequest) (*models.LoginResponse, error) {
var userRow database.GetValidUserByLoginCredentialsRow var userRow database.GetValidUserByLoginCredentialsRow
var session database.Session var session database.Session
var err error
helper, db, _ := database.NewDbHelperTransaction(a.dbctx) helper, db, _ := database.NewDbHelperTransaction(a.dbctx)
defer helper.Rollback() defer helper.RollbackOnError(err)
var err error
userRow, err = db.TXQueries.GetValidUserByLoginCredentials(db.CTX, database.GetValidUserByLoginCredentialsParams{ userRow, err = db.TXQueries.GetValidUserByLoginCredentials(db.CTX, database.GetValidUserByLoginCredentialsParams{
Username: request.Username, Username: request.Username,
@@ -417,7 +416,7 @@ func (a *authServiceImpl) PasswordResetBegin(request models.PasswordResetBeginRe
var err error var err error
helper, db, err := database.NewDbHelperTransaction(a.dbctx) helper, db, err := database.NewDbHelperTransaction(a.dbctx)
defer helper.Rollback() defer helper.RollbackOnError(err)
ctx := context.TODO() ctx := context.TODO()