From 65ea47dbb6f534b0e5afab8709936dfd12c8e4c0 Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Sun, 13 Jul 2025 15:57:34 +0300 Subject: [PATCH] feat: new RollbackOnError method added for transactional db helper and integrated into auth service --- backend/internal/database/helper.go | 39 ++++++++++++++++++----------- backend/internal/services/auth.go | 11 ++++---- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/backend/internal/database/helper.go b/backend/internal/database/helper.go index f008b49..6056d1f 100644 --- a/backend/internal/database/helper.go +++ b/backend/internal/database/helper.go @@ -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 . @@ -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 +} diff --git a/backend/internal/services/auth.go b/backend/internal/services/auth.go index 425ce08..d9aaa8d 100644 --- a/backend/internal/services/auth.go +++ b/backend/internal/services/auth.go @@ -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()