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:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
35
pkg/controllers/wallets-header.go
Normal file
35
pkg/controllers/wallets-header.go
Normal 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)
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Transaction struct {
|
||||
tableName struct{} `pg:"transactions,alias:transactions"`
|
||||
@@ -19,4 +22,5 @@ type NewTransactionBody struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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