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