2024-11-16 19:31:31 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-11-23 23:28:22 +01:00
|
|
|
"fmt"
|
|
|
|
|
2024-11-16 19:31:31 +01:00
|
|
|
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
|
|
|
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
var transferTransform func(inp *db.Transfer) types.DbTransfer = func(inp *db.Transfer) types.DbTransfer {
|
2024-11-23 23:28:22 +01:00
|
|
|
var fromCard types.DbCard
|
|
|
|
var toCard types.DbCard
|
|
|
|
if inp.FromCard != nil {
|
|
|
|
fromCard = cardTransform(inp.FromCard)
|
|
|
|
}
|
|
|
|
if inp.ToCard != nil {
|
|
|
|
toCard = cardTransform(inp.ToCard)
|
|
|
|
}
|
|
|
|
haveDiffCurrs := false
|
|
|
|
if inp.FromCard != nil && inp.FromCard.Currency != nil && inp.ToCard != nil && inp.ToCard.Currency != nil {
|
|
|
|
haveDiffCurrs = inp.FromCard.CurrencyID != inp.ToCard.CurrencyID
|
|
|
|
}
|
|
|
|
var showValue string
|
2024-11-23 23:39:02 +01:00
|
|
|
sameCurrSymbol := ""
|
|
|
|
if inp.FromCard != nil && inp.FromCard.Currency != nil {
|
|
|
|
sameCurrSymbol = inp.FromCard.Currency.Symbol
|
|
|
|
}
|
|
|
|
if sameCurrSymbol == "" && inp.ToCard != nil && inp.ToCard.Currency != nil {
|
|
|
|
sameCurrSymbol = inp.ToCard.Currency.Symbol
|
|
|
|
}
|
2024-11-23 23:28:22 +01:00
|
|
|
if haveDiffCurrs {
|
|
|
|
showValue = fmt.Sprintf("%d.%02d%s -> %d.%02d%s",
|
|
|
|
inp.FromValue/100,
|
|
|
|
inp.FromValue%100,
|
|
|
|
inp.FromCard.Currency.Symbol,
|
|
|
|
inp.ToValue/100,
|
|
|
|
inp.ToValue%100,
|
|
|
|
inp.ToCard.Currency.Symbol,
|
|
|
|
)
|
|
|
|
} else {
|
2024-11-23 23:39:02 +01:00
|
|
|
showValue = fmt.Sprintf("%d.%02d%s", inp.Value/100, inp.Value%100, sameCurrSymbol)
|
2024-11-23 23:28:22 +01:00
|
|
|
}
|
2024-11-16 19:31:31 +01:00
|
|
|
return types.DbTransfer{
|
|
|
|
ID: inp.ID,
|
|
|
|
FromCardID: inp.FromCardID,
|
|
|
|
ToCardID: inp.ToCardID,
|
|
|
|
Value: inp.Value,
|
2024-11-23 23:28:22 +01:00
|
|
|
FromValue: inp.FromValue,
|
|
|
|
ToValue: inp.ToValue,
|
2024-11-16 19:31:31 +01:00
|
|
|
Date: inp.Date,
|
2024-11-23 23:28:22 +01:00
|
|
|
//
|
|
|
|
ShowValue: showValue,
|
|
|
|
HaveDifferentCurrencies: haveDiffCurrs,
|
|
|
|
FromCard: fromCard,
|
|
|
|
ToCard: toCard,
|
2024-11-16 19:31:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Get transfer by id
|
|
|
|
// @Description Get transfer by id
|
|
|
|
// @Tags transfer
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Param transfer path int true "id"
|
|
|
|
// @Success 200 {object} types.DbTransfer
|
|
|
|
// @Failure 400 {object} types.ErrorResponse
|
|
|
|
// @Failure 401 {object} types.ErrorResponse
|
|
|
|
// @Failure 403 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /transfer/:id [get]
|
|
|
|
func TransferGetId(c *gin.Context) {
|
|
|
|
GetHandler(transferTransform)(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Get all transfers for user
|
|
|
|
// @Description Get all transfers for user
|
|
|
|
// @Tags type
|
|
|
|
// @Produce json
|
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Success 200 {object} []types.DbTransfer
|
|
|
|
// @Failure 401 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /transfer/all [get]
|
|
|
|
func TransferGetAll(c *gin.Context) {
|
|
|
|
userID, err := GetUserID(c)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dbc := db.Connect()
|
|
|
|
var entities []*db.Transfer
|
2024-11-23 23:28:22 +01:00
|
|
|
if err := dbc.Preload("FromCard.Currency").Preload("ToCard.Currency").Find(&entities, db.Transfer{UserID: userID}).Error; err != nil {
|
2024-11-16 19:31:31 +01:00
|
|
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret []types.DbTransfer
|
|
|
|
for _, entity := range entities {
|
|
|
|
ret = append(ret, transferTransform(entity))
|
|
|
|
}
|
|
|
|
c.JSON(200, ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Add transfer
|
|
|
|
// @Description Add transfer
|
|
|
|
// @Tags transfer
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Param transfer body types.DbTransfer true "Transfer"
|
|
|
|
// @Success 200 {object} types.Message
|
|
|
|
// @Failure 400 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /transfer/add [post]
|
|
|
|
func TransferAdd(c *gin.Context) {
|
|
|
|
CreateHandler(&db.Transfer{}, func(src types.DbTransfer, dst *db.Transfer) {
|
|
|
|
dst.FromCardID = src.FromCardID
|
|
|
|
dst.ToCardID = src.ToCardID
|
|
|
|
dst.Value = src.Value
|
|
|
|
dst.Date = src.Date
|
2024-11-23 23:28:22 +01:00
|
|
|
dst.FromValue = src.FromValue
|
|
|
|
dst.ToValue = src.ToValue
|
2024-11-16 19:31:31 +01:00
|
|
|
})(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Edit transfer by id
|
|
|
|
// @Description Edit transfer by id
|
|
|
|
// @Tags transfer
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Param transferID path int true "id"
|
|
|
|
// @Param transfer body types.DbTransfer true "Transfer"
|
|
|
|
// @Success 200 {object} types.DbTransfer
|
|
|
|
// @Failure 400 {object} types.ErrorResponse
|
|
|
|
// @Failure 401 {object} types.ErrorResponse
|
|
|
|
// @Failure 403 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /transfer/edit/:id [put]
|
|
|
|
func TransferPutId(c *gin.Context) {
|
|
|
|
UpdateHandler(
|
|
|
|
func(src types.DbTransfer, dst *db.Transfer) {
|
|
|
|
dst.FromCardID = src.FromCardID
|
|
|
|
dst.ToCardID = src.ToCardID
|
|
|
|
dst.Value = src.Value
|
|
|
|
dst.Date = src.Date
|
2024-11-23 23:28:22 +01:00
|
|
|
dst.FromValue = src.FromValue
|
|
|
|
dst.ToValue = src.ToValue
|
2024-11-16 19:31:31 +01:00
|
|
|
},
|
|
|
|
transferTransform,
|
|
|
|
)(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Delete transfer by id
|
|
|
|
// @Description Delete transfer by id
|
|
|
|
// @Tags transfer
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Param transferID path int true "id"
|
|
|
|
// @Success 200 {object} types.DbTransfer
|
|
|
|
// @Failure 400 {object} types.ErrorResponse
|
|
|
|
// @Failure 401 {object} types.ErrorResponse
|
|
|
|
// @Failure 403 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /transfer/delete/:id [delete]
|
|
|
|
func TransferDeleteId(c *gin.Context) {
|
|
|
|
DeleteHandler[*db.Transfer]()(c)
|
|
|
|
}
|