sub to trans

This commit is contained in:
Fran Jurmanović
2021-06-20 12:03:56 +02:00
parent 3d43716669
commit 27742223ed
7 changed files with 101 additions and 77 deletions

View File

@@ -1,6 +1,7 @@
package services
import (
"math"
"time"
"wallet-api/pkg/models"
@@ -9,19 +10,20 @@ import (
type TransactionService struct {
Db *pg.DB
Ss *SubscriptionService
}
func (as *TransactionService) New(body *models.NewTransactionBody) *models.Transaction {
tm := new(models.Transaction)
amount, _ := body.Amount.Int64()
amount, _ := body.Amount.Float64()
tm.Init()
tm.WalletID = body.WalletID
tm.TransactionTypeID = body.TransactionTypeID
tm.Description = body.Description
tm.TransactionDate = body.TransactionDate
tm.Amount = int(amount)
tm.Amount = float32(math.Round(amount*100) / 100)
if body.TransactionDate.IsZero() {
tm.TransactionDate = time.Now()
@@ -34,8 +36,18 @@ func (as *TransactionService) New(body *models.NewTransactionBody) *models.Trans
func (as *TransactionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) {
wm := new([]models.Transaction)
sm := new([]models.Subscription)
query := as.Db.Model((wm)).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
query2 := as.Db.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
if walletId != "" {
query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
}
for _, sub := range *sm {
as.Ss.SubToTrans(&sub)
}
query := as.Db.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
if walletId != "" {
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
}