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 1/2] 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 } From 1b4d5e8725b9d877f6d89276f47ba7a934867ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Wed, 30 Jun 2021 22:36:03 +0200 Subject: [PATCH 2/2] fixed low performance transactions --- pkg/models/subscriptions.go | 53 ++++++++++++++++++----------------- pkg/services/subscriptions.go | 21 ++++++++------ pkg/services/transactions.go | 5 +++- pkg/services/wallets.go | 2 +- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/pkg/models/subscriptions.go b/pkg/models/subscriptions.go index 65a1a86..5384216 100644 --- a/pkg/models/subscriptions.go +++ b/pkg/models/subscriptions.go @@ -50,31 +50,34 @@ func (cm *Subscription) ToTrans() *Transaction { } func (cm *Subscription) HasNew() bool { - trans := cm.TransactionType; - switch trans.Type { - case "monthly": - lastDate := time.Now().AddDate(0, -cm.CustomRange, 0) - if cm.LastTransactionDate.Before(lastDate) { - return true + trans := cm.TransactionType + if trans != nil { + switch trans.Type { + case "monthly": + lastDate := time.Now().AddDate(0, -cm.CustomRange, 0) + if cm.LastTransactionDate.Before(lastDate) { + return true + } + return false + case "weekly": + lastDate := time.Now().AddDate(0, 0, -(7*cm.CustomRange)) + if cm.LastTransactionDate.Before(lastDate) { + return true + } + return false + case "daily": + lastDate := time.Now().AddDate(0, 0, -cm.CustomRange) + if cm.LastTransactionDate.Before(lastDate) { + return true + } + return false + default: + lastDate := time.Now().AddDate(-cm.CustomRange, 0, 0) + if cm.LastTransactionDate.Before(lastDate) { + return true + } + return false } - return false - case "weekly": - lastDate := time.Now().AddDate(0, 0, -(7*cm.CustomRange)) - if cm.LastTransactionDate.Before(lastDate) { - return true - } - return false - case "daily": - lastDate := time.Now().AddDate(0, 0, -cm.CustomRange) - if cm.LastTransactionDate.Before(lastDate) { - return true - } - return false - default: - lastDate := time.Now().AddDate(-cm.CustomRange, 0, 0) - if cm.LastTransactionDate.Before(lastDate) { - return true - } - return false } + return true } \ No newline at end of file diff --git a/pkg/services/subscriptions.go b/pkg/services/subscriptions.go index 3d6d6bb..1db9553 100644 --- a/pkg/services/subscriptions.go +++ b/pkg/services/subscriptions.go @@ -33,9 +33,13 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub tm.StartDate = time.Now() } - as.Db.Model(tm).Insert() + tx, _ := as.Db.Begin() + defer tx.Rollback() - as.SubToTrans(tm) + tx.Model(tm).Insert() + + as.SubToTrans(tm, tx) + tx.Commit() return tm } @@ -43,22 +47,24 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub func (as *SubscriptionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) { wm := new([]models.Subscription) - query := as.Db.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) + tx, _ := as.Db.Begin() + defer tx.Rollback() + + query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) if walletId != "" { query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) } for _, sub := range *wm { if sub.HasNew() { - as.SubToTrans(&sub) + as.SubToTrans(&sub, tx) } } FilteredResponse(query, wm, filtered) + tx.Commit() } -func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { - tx, _ := as.Db.Begin() - defer tx.Rollback() +func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) { now := time.Now() @@ -107,5 +113,4 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { } } } - tx.Commit() } diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 4580e0c..d11015a 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -48,13 +48,16 @@ func (as *TransactionService) GetAll(am *models.Auth, walletId string, filtered query2.Select() for _, sub := range *sm { - as.Ss.SubToTrans(&sub) + if sub.HasNew() { + as.Ss.SubToTrans(&sub, tx) + } } 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/wallets.go b/pkg/services/wallets.go index 48d183e..462eb92 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -67,7 +67,7 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal for _, sub := range *subscriptions { if sub.HasNew() { - as.Ss.SubToTrans(&sub) + as.Ss.SubToTrans(&sub, tx) } }