From 81505baf7a945b2c9f10ad3dc4eac6a78bb61f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Wed, 30 Jun 2021 21:55:47 +0200 Subject: [PATCH] grouped transactions --- pkg/services/subscriptions.go | 10 +++++++--- pkg/services/transactions.go | 9 +++++++-- pkg/services/users.go | 18 ++++++++++++++---- pkg/services/wallets.go | 9 +++++++-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/pkg/services/subscriptions.go b/pkg/services/subscriptions.go index 7f0963a..3d6d6bb 100644 --- a/pkg/services/subscriptions.go +++ b/pkg/services/subscriptions.go @@ -57,6 +57,9 @@ func (as *SubscriptionService) GetAll(am *models.Auth, walletId string, filtered } func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { + tx, _ := as.Db.Begin() + defer tx.Rollback() + now := time.Now() currentYear, currentMonth, _ := now.Date() @@ -75,7 +78,7 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { if subModel.SubscriptionType == nil { st := new(models.SubscriptionType) - as.Db.Model(st).Where("? = ?", pg.Ident("id"), subModel.SubscriptionTypeID).Select() + tx.Model(st).Where("? = ?", pg.Ident("id"), subModel.SubscriptionTypeID).Select() subModel.SubscriptionType = st } @@ -98,10 +101,11 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { if len(*transactions) > 0 { for _, trans := range *transactions { - _, err := as.Db.Model(&trans).Where("? = ?", pg.Ident("transaction_date"), trans.TransactionDate).Where("? = ?", pg.Ident("subscription_id"), trans.SubscriptionID).OnConflict("DO NOTHING").SelectOrInsert() + _, err := tx.Model(&trans).Where("? = ?", pg.Ident("transaction_date"), trans.TransactionDate).Where("? = ?", pg.Ident("subscription_id"), trans.SubscriptionID).OnConflict("DO NOTHING").SelectOrInsert() if err != nil { - as.Db.Model(subModel).Set("? = ?", pg.Ident("last_transaction_date"), trans.TransactionDate).WherePK().Update() + tx.Model(subModel).Set("? = ?", pg.Ident("last_transaction_date"), trans.TransactionDate).WherePK().Update() } } } + tx.Commit() } diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 43d9784..4580e0c 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -38,7 +38,10 @@ func (as *TransactionService) GetAll(am *models.Auth, walletId string, filtered wm := new([]models.Transaction) sm := new([]models.Subscription) - query2 := as.Db.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) + tx, _ := as.Db.Begin() + defer tx.Rollback() + + query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) if walletId != "" { query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId) } @@ -48,9 +51,11 @@ func (as *TransactionService) GetAll(am *models.Auth, walletId string, filtered as.Ss.SubToTrans(&sub) } - query := as.Db.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) + query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) if walletId != "" { query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) } FilteredResponse(query, wm, filtered) + + tx.Commit() } diff --git a/pkg/services/users.go b/pkg/services/users.go index 2b979b1..04edcec 100644 --- a/pkg/services/users.go +++ b/pkg/services/users.go @@ -21,7 +21,10 @@ func (us *UsersService) Create(registerBody *models.User) (*models.User, *models check := new(models.User) exceptionReturn := new(models.Exception) - us.Db.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select() + tx, _ := us.Db.Begin() + defer tx.Rollback() + + tx.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select() if check.Username != "" || check.Email != "" { exceptionReturn.Message = "User already exists" exceptionReturn.ErrorCode = "400101" @@ -33,7 +36,7 @@ func (us *UsersService) Create(registerBody *models.User) (*models.User, *models common.CheckError(err) registerBody.Password = string(hashedPassword) - _, err = us.Db.Model(registerBody).Insert() + _, err = tx.Model(registerBody).Insert() if err != nil { exceptionReturn.Message = "Error creating user" @@ -41,6 +44,8 @@ func (us *UsersService) Create(registerBody *models.User) (*models.User, *models exceptionReturn.StatusCode = 400 } + tx.Commit() + return registerBody, exceptionReturn } @@ -84,7 +89,10 @@ func (us *UsersService) Deactivate(auth *models.Auth) (*models.MessageResponse, me := new(models.Exception) um := new(models.User) - err := us.Db.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select() + tx, _ := us.Db.Begin() + defer tx.Rollback() + + err := tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select() if err != nil { me.ErrorCode = "404101" @@ -93,7 +101,7 @@ func (us *UsersService) Deactivate(auth *models.Auth) (*models.MessageResponse, return mm, me } um.IsActive = false - _, err = us.Db.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Update() + _, err = tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Update() if err != nil { me.ErrorCode = "400105" @@ -104,6 +112,8 @@ func (us *UsersService) Deactivate(auth *models.Auth) (*models.MessageResponse, mm.Message = "User successfully deactivated." + tx.Commit() + return mm, me } diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index 709da31..48d183e 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -47,7 +47,10 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal transactions := new([]models.Transaction) subscriptions := new([]models.Subscription) - query2 := as.Db.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType") + tx, _ := as.Db.Begin() + defer tx.Rollback() + + query2 := tx.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType") if walletId != "" { query2.Where("? = ?", pg.Ident("wallet_id"), walletId) } @@ -68,7 +71,7 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal } } - query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType") + query := tx.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType") if walletId != "" { query.Where("? = ?", pg.Ident("wallet_id"), walletId) } @@ -117,6 +120,8 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal wm.Currency = "USD" wm.WalletId = walletId + tx.Commit() + return wm }