mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
fixed low performance transactions
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
@@ -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()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user