mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
implemented wallet headers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
@@ -13,11 +14,18 @@ type TransactionService struct {
|
||||
func (as *TransactionService) New(body *models.NewTransactionBody) *models.Transaction {
|
||||
tm := new(models.Transaction)
|
||||
|
||||
amount, _ := body.Amount.Int64()
|
||||
|
||||
tm.Init()
|
||||
tm.WalletID = body.WalletID
|
||||
tm.TransactionTypeID = body.TransactionTypeID
|
||||
tm.Description = body.Description
|
||||
tm.TransactionDate = body.TransactionDate
|
||||
tm.Amount = int(amount)
|
||||
|
||||
if body.TransactionDate.IsZero() {
|
||||
tm.TransactionDate = time.Now()
|
||||
}
|
||||
|
||||
as.Db.Model(tm).Insert()
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
@@ -36,3 +37,51 @@ func (as *WalletService) GetAll(am *models.Auth, filtered *models.FilteredRespon
|
||||
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
||||
FilteredResponse(query, wm, filtered)
|
||||
}
|
||||
|
||||
func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId string) *models.WalletHeader {
|
||||
wm := new(models.WalletHeader)
|
||||
//wallets := new([]models.Wallet)
|
||||
transactions := new([]models.Transaction)
|
||||
|
||||
query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
||||
|
||||
if walletId != "" {
|
||||
query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
}
|
||||
|
||||
query.Select()
|
||||
|
||||
currentBalance := 0
|
||||
lastMonthBalance := 0
|
||||
nextMonth := 0
|
||||
|
||||
now := time.Now()
|
||||
|
||||
currentYear, currentMonth, _ := now.Date()
|
||||
currentLocation := now.Location()
|
||||
|
||||
firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
|
||||
firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation)
|
||||
firstOfMonthAfterNext := time.Date(currentYear, currentMonth+2, 1, 0, 0, 0, 0, currentLocation)
|
||||
|
||||
for _, trans := range *transactions {
|
||||
if trans.TransactionDate.Before(firstOfNextMonth) && trans.TransactionDate.After(firstOfMonth) {
|
||||
currentBalance += trans.Amount
|
||||
} else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) {
|
||||
nextMonth += trans.Amount
|
||||
} else if trans.TransactionDate.Before(firstOfMonth) {
|
||||
lastMonthBalance += trans.Amount
|
||||
currentBalance += trans.Amount
|
||||
}
|
||||
}
|
||||
|
||||
wm.CurrentBalance = currentBalance
|
||||
wm.LastMonth = lastMonthBalance
|
||||
wm.NextMonth = currentBalance + nextMonth
|
||||
wm.Currency = "USD"
|
||||
wm.WalletId = walletId
|
||||
|
||||
//common.GenerateEmbed(query, embed).Select()
|
||||
|
||||
return wm
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user