From 0fc69a0af2633d8f999e09f654469191e3ad062d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Thu, 10 Jun 2021 00:13:35 +0200 Subject: [PATCH] implemented wallet headers --- pkg/api/routes.go | 2 ++ pkg/controllers/auth.go | 4 +-- pkg/controllers/wallets-header.go | 35 ++++++++++++++++++++++ pkg/models/transactions.go | 14 +++++---- pkg/models/wallets.go | 8 +++++ pkg/services/transactions.go | 8 +++++ pkg/services/wallets.go | 49 +++++++++++++++++++++++++++++++ 7 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 pkg/controllers/wallets-header.go diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 5327a9c..ea1e861 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -16,6 +16,7 @@ func Routes(s *gin.Engine, db *pg.DB) { api := ver.Group("api") auth := ver.Group("auth") wallet := ver.Group("wallet", middleware.Auth) + walletHeader := wallet.Group("wallet-header", middleware.Auth) transaction := ver.Group("transaction", 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.NewAuthController(&usersService, auth) controllers.NewWalletsController(&walletService, wallet) + controllers.NewWalletsHeaderController(&walletService, walletHeader) controllers.NewTransactionController(&transactionService, transaction) controllers.NewTransactionTypeController(&transactionTypeService, transactionType) } diff --git a/pkg/controllers/auth.go b/pkg/controllers/auth.go index fb52f90..1ead3a5 100644 --- a/pkg/controllers/auth.go +++ b/pkg/controllers/auth.go @@ -74,9 +74,9 @@ func (rc *AuthController) CheckToken(c *gin.Context) { token, _ := c.GetQuery("token") re := new(models.CheckToken) - valid, err := middleware.CheckToken(token) + _, err := middleware.CheckToken(token) - if err != nil && valid.Valid { + if err != nil { re.Valid = false c.AbortWithStatusJSON(400, re) } diff --git a/pkg/controllers/wallets-header.go b/pkg/controllers/wallets-header.go new file mode 100644 index 0000000..0aa08e4 --- /dev/null +++ b/pkg/controllers/wallets-header.go @@ -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) +} diff --git a/pkg/models/transactions.go b/pkg/models/transactions.go index 9fb9b8a..9bed283 100644 --- a/pkg/models/transactions.go +++ b/pkg/models/transactions.go @@ -1,6 +1,9 @@ package models -import "time" +import ( + "encoding/json" + "time" +) type Transaction struct { tableName struct{} `pg:"transactions,alias:transactions"` @@ -15,8 +18,9 @@ type Transaction struct { } type NewTransactionBody struct { - WalletID string `json:"walletId" form:"walletId"` - TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"` - TransactionDate time.Time `json:"transactionDate" form:"transactionDate"` - Description string `json:"description" form:"description"` + WalletID string `json:"walletId" form:"walletId"` + TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"` + TransactionDate time.Time `json:"transactionDate" form:"transactionDate"` + Description string `json:"description" form:"description"` + Amount json.Number `json:"amount" form:"amount"` } diff --git a/pkg/models/wallets.go b/pkg/models/wallets.go index a58013a..f72112e 100644 --- a/pkg/models/wallets.go +++ b/pkg/models/wallets.go @@ -12,3 +12,11 @@ type NewWalletBody struct { Name string `json:"name" form:"name"` 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"` +} diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 30a4d93..e70a924 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -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() diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index 2213fee..eade649 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -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 +}