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

View File

@@ -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()
}

View File

@@ -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()

View File

@@ -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)
}
}