implemented wallet headers

This commit is contained in:
Fran Jurmanović
2021-06-10 00:13:35 +02:00
parent 636a367d3e
commit 0fc69a0af2
7 changed files with 113 additions and 7 deletions

View File

@@ -16,6 +16,7 @@ func Routes(s *gin.Engine, db *pg.DB) {
api := ver.Group("api") api := ver.Group("api")
auth := ver.Group("auth") auth := ver.Group("auth")
wallet := ver.Group("wallet", middleware.Auth) wallet := ver.Group("wallet", middleware.Auth)
walletHeader := wallet.Group("wallet-header", middleware.Auth)
transaction := ver.Group("transaction", middleware.Auth) transaction := ver.Group("transaction", middleware.Auth)
transactionType := ver.Group("transaction-type", middleware.Auth) transactionType := ver.Group("transaction-type", middleware.Auth)
@@ -28,6 +29,7 @@ func Routes(s *gin.Engine, db *pg.DB) {
controllers.NewApiController(&apiService, api) controllers.NewApiController(&apiService, api)
controllers.NewAuthController(&usersService, auth) controllers.NewAuthController(&usersService, auth)
controllers.NewWalletsController(&walletService, wallet) controllers.NewWalletsController(&walletService, wallet)
controllers.NewWalletsHeaderController(&walletService, walletHeader)
controllers.NewTransactionController(&transactionService, transaction) controllers.NewTransactionController(&transactionService, transaction)
controllers.NewTransactionTypeController(&transactionTypeService, transactionType) controllers.NewTransactionTypeController(&transactionTypeService, transactionType)
} }

View File

@@ -74,9 +74,9 @@ func (rc *AuthController) CheckToken(c *gin.Context) {
token, _ := c.GetQuery("token") token, _ := c.GetQuery("token")
re := new(models.CheckToken) re := new(models.CheckToken)
valid, err := middleware.CheckToken(token) _, err := middleware.CheckToken(token)
if err != nil && valid.Valid { if err != nil {
re.Valid = false re.Valid = false
c.AbortWithStatusJSON(400, re) c.AbortWithStatusJSON(400, re)
} }

View File

@@ -0,0 +1,35 @@
package controllers
import (
"wallet-api/pkg/models"
"wallet-api/pkg/services"
"github.com/gin-gonic/gin"
)
type WalletsHeaderController struct {
WalletService *services.WalletService
}
func NewWalletsHeaderController(as *services.WalletService, s *gin.RouterGroup) *WalletsHeaderController {
wc := new(WalletsHeaderController)
wc.WalletService = as
s.GET("", wc.Get)
return wc
}
func (wc *WalletsHeaderController) Get(c *gin.Context) {
body := new(models.Auth)
embed, _ := c.GetQuery("embed")
walletId, _ := c.GetQuery("walletId")
auth := c.MustGet("auth")
body.Id = auth.(*models.Auth).Id
wm := wc.WalletService.GetHeader(body, embed, walletId)
c.JSON(200, wm)
}

View File

@@ -1,6 +1,9 @@
package models package models
import "time" import (
"encoding/json"
"time"
)
type Transaction struct { type Transaction struct {
tableName struct{} `pg:"transactions,alias:transactions"` tableName struct{} `pg:"transactions,alias:transactions"`
@@ -15,8 +18,9 @@ type Transaction struct {
} }
type NewTransactionBody struct { type NewTransactionBody struct {
WalletID string `json:"walletId" form:"walletId"` WalletID string `json:"walletId" form:"walletId"`
TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"` TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"`
TransactionDate time.Time `json:"transactionDate" form:"transactionDate"` TransactionDate time.Time `json:"transactionDate" form:"transactionDate"`
Description string `json:"description" form:"description"` Description string `json:"description" form:"description"`
Amount json.Number `json:"amount" form:"amount"`
} }

View File

@@ -12,3 +12,11 @@ type NewWalletBody struct {
Name string `json:"name" form:"name"` Name string `json:"name" form:"name"`
UserID string `json:"userId" form:"userId"` UserID string `json:"userId" form:"userId"`
} }
type WalletHeader struct {
WalletId string `json:"walletId"`
CurrentBalance int `json:"currentBalance"`
LastMonth int `json:"lastMonth"`
NextMonth int `json:"nextMonth"`
Currency string `json:"currency"`
}

View File

@@ -1,6 +1,7 @@
package services package services
import ( import (
"time"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"github.com/go-pg/pg/v10" "github.com/go-pg/pg/v10"
@@ -13,11 +14,18 @@ type TransactionService struct {
func (as *TransactionService) New(body *models.NewTransactionBody) *models.Transaction { func (as *TransactionService) New(body *models.NewTransactionBody) *models.Transaction {
tm := new(models.Transaction) tm := new(models.Transaction)
amount, _ := body.Amount.Int64()
tm.Init() tm.Init()
tm.WalletID = body.WalletID tm.WalletID = body.WalletID
tm.TransactionTypeID = body.TransactionTypeID tm.TransactionTypeID = body.TransactionTypeID
tm.Description = body.Description tm.Description = body.Description
tm.TransactionDate = body.TransactionDate tm.TransactionDate = body.TransactionDate
tm.Amount = int(amount)
if body.TransactionDate.IsZero() {
tm.TransactionDate = time.Now()
}
as.Db.Model(tm).Insert() as.Db.Model(tm).Insert()

View File

@@ -1,6 +1,7 @@
package services package services
import ( import (
"time"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/utl/common" "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) query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
FilteredResponse(query, wm, filtered) 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
}