fixed low performance transactions

This commit is contained in:
Fran Jurmanović
2021-06-30 22:36:03 +02:00
parent 81505baf7a
commit 1b4d5e8725
4 changed files with 46 additions and 35 deletions

View File

@@ -50,31 +50,34 @@ func (cm *Subscription) ToTrans() *Transaction {
} }
func (cm *Subscription) HasNew() bool { func (cm *Subscription) HasNew() bool {
trans := cm.TransactionType; trans := cm.TransactionType
switch trans.Type { if trans != nil {
case "monthly": switch trans.Type {
lastDate := time.Now().AddDate(0, -cm.CustomRange, 0) case "monthly":
if cm.LastTransactionDate.Before(lastDate) { lastDate := time.Now().AddDate(0, -cm.CustomRange, 0)
return true 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
} }

View File

@@ -33,9 +33,13 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub
tm.StartDate = time.Now() 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 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) { func (as *SubscriptionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) {
wm := new([]models.Subscription) 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 != "" { if walletId != "" {
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
} }
for _, sub := range *wm { for _, sub := range *wm {
if sub.HasNew() { if sub.HasNew() {
as.SubToTrans(&sub) as.SubToTrans(&sub, tx)
} }
} }
FilteredResponse(query, wm, filtered) FilteredResponse(query, wm, filtered)
tx.Commit()
} }
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) {
tx, _ := as.Db.Begin()
defer tx.Rollback()
now := time.Now() now := time.Now()
@@ -107,5 +113,4 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) {
} }
} }
} }
tx.Commit()
} }

View File

@@ -48,13 +48,16 @@ func (as *TransactionService) GetAll(am *models.Auth, walletId string, filtered
query2.Select() query2.Select()
for _, sub := range *sm { 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) query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
if walletId != "" { if walletId != "" {
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
} }
FilteredResponse(query, wm, filtered) FilteredResponse(query, wm, filtered)
tx.Commit() tx.Commit()

View File

@@ -67,7 +67,7 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
for _, sub := range *subscriptions { for _, sub := range *subscriptions {
if sub.HasNew() { if sub.HasNew() {
as.Ss.SubToTrans(&sub) as.Ss.SubToTrans(&sub, tx)
} }
} }