2024-08-03 07:16:18 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-11-04 16:55:14 +01:00
|
|
|
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
|
|
|
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
2024-08-03 07:16:18 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2024-11-10 07:25:35 +01:00
|
|
|
var cardTransform func(inp *db.Card) types.DbCard = func(inp *db.Card) types.DbCard {
|
|
|
|
return types.DbCard{
|
|
|
|
ID: inp.ID,
|
|
|
|
Name: inp.Name,
|
|
|
|
Balance: inp.Balance,
|
|
|
|
HaveCreditLine: inp.HaveCreditLine,
|
|
|
|
CreditLine: inp.CreditLine,
|
2024-11-20 20:00:17 +01:00
|
|
|
LastDigits: inp.LastDigits,
|
|
|
|
CurrencyID: inp.CurrencyID,
|
2024-11-10 07:25:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 07:16:18 +02:00
|
|
|
// @Summary Get card by id
|
|
|
|
// @Description Get card by id
|
|
|
|
// @Tags card
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2024-08-03 07:43:41 +02:00
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Param card path int true "id"
|
2024-08-03 07:16:18 +02:00
|
|
|
// @Success 200 {object} types.DbCard
|
|
|
|
// @Failure 400 {object} types.ErrorResponse
|
2024-08-03 08:15:52 +02:00
|
|
|
// @Failure 401 {object} types.ErrorResponse
|
2024-08-03 07:16:18 +02:00
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
2024-08-03 07:43:41 +02:00
|
|
|
// @Security ApiKeyAuth
|
2024-08-03 07:16:18 +02:00
|
|
|
// @Router /card/:id [get]
|
|
|
|
func CardGetId(c *gin.Context) {
|
2024-11-10 07:25:35 +01:00
|
|
|
GetHandler(cardTransform)(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Get all cards for user
|
|
|
|
// @Description Get all cards for user
|
|
|
|
// @Tags card
|
|
|
|
// @Produce json
|
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Success 200 {object} []types.DbCard
|
|
|
|
// @Failure 401 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /card/all [get]
|
|
|
|
func CardGetAll(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.Card
|
|
|
|
if err := dbc.Find(&entities, db.Card{UserID: userID}).Error; err != nil {
|
|
|
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret []types.DbCard
|
|
|
|
for _, entity := range entities {
|
|
|
|
ret = append(ret, cardTransform(entity))
|
|
|
|
}
|
|
|
|
c.JSON(200, ret)
|
2024-08-03 07:16:18 +02:00
|
|
|
}
|
|
|
|
|
2024-11-16 10:45:47 +01:00
|
|
|
// @Summary Add card
|
|
|
|
// @Description Add card
|
2024-08-03 07:16:18 +02:00
|
|
|
// @Tags card
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2024-08-03 07:43:41 +02:00
|
|
|
// @Param Authorization header string true "Bearer token"
|
2024-08-03 07:16:18 +02:00
|
|
|
// @Param card body types.DbCard true "Card"
|
|
|
|
// @Success 200 {object} types.Message
|
|
|
|
// @Failure 400 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
2024-08-03 07:43:41 +02:00
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /card/add [post]
|
2024-08-03 07:16:18 +02:00
|
|
|
func CardAdd(c *gin.Context) {
|
2024-10-31 10:07:51 +01:00
|
|
|
card := &db.Card{}
|
|
|
|
CreateHandler(card, func(src types.DbCard, dst *db.Card) {
|
|
|
|
dst.Name = src.Name
|
2024-11-01 08:26:37 +01:00
|
|
|
dst.Balance = src.Balance
|
2024-10-31 10:07:51 +01:00
|
|
|
dst.HaveCreditLine = src.HaveCreditLine
|
|
|
|
dst.CreditLine = src.CreditLine
|
2024-11-20 20:00:17 +01:00
|
|
|
dst.LastDigits = src.LastDigits
|
|
|
|
dst.CurrencyID = src.CurrencyID
|
2024-10-31 10:07:51 +01:00
|
|
|
})(c)
|
2024-08-03 07:16:18 +02:00
|
|
|
}
|
2024-08-03 08:53:38 +02:00
|
|
|
|
|
|
|
// @Summary Edit card by id
|
|
|
|
// @Description Edit card by id
|
|
|
|
// @Tags card
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Param cardID path int true "id"
|
|
|
|
// @Param card body types.DbCard true "Card"
|
|
|
|
// @Success 200 {object} types.DbCard
|
|
|
|
// @Failure 400 {object} types.ErrorResponse
|
|
|
|
// @Failure 401 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /card/edit/:id [put]
|
|
|
|
func CardPutId(c *gin.Context) {
|
2024-10-31 10:07:51 +01:00
|
|
|
UpdateHandler(
|
|
|
|
// Filter used to apply only needed changes from srt to dst before updating dst
|
|
|
|
func(src types.DbCard, dst *db.Card) {
|
|
|
|
dst.Name = src.Name
|
2024-11-01 08:26:37 +01:00
|
|
|
dst.Balance = src.Balance
|
2024-10-31 10:07:51 +01:00
|
|
|
dst.CreditLine = src.CreditLine
|
|
|
|
dst.HaveCreditLine = src.HaveCreditLine
|
2024-11-20 20:00:17 +01:00
|
|
|
dst.LastDigits = src.LastDigits
|
|
|
|
dst.CurrencyID = src.CurrencyID
|
2024-10-31 10:07:51 +01:00
|
|
|
},
|
2024-11-10 07:49:12 +01:00
|
|
|
cardTransform)(c)
|
2024-08-03 08:53:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Delete card by id
|
|
|
|
// @Description Delete card by id
|
|
|
|
// @Tags card
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Param cardID path int true "id"
|
|
|
|
// @Success 200 {object} types.DbCard
|
|
|
|
// @Failure 400 {object} types.ErrorResponse
|
|
|
|
// @Failure 401 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
|
|
|
// @Security ApiKeyAuth
|
2024-08-03 08:54:29 +02:00
|
|
|
// @Router /card/delete/:id [delete]
|
2024-08-03 08:53:38 +02:00
|
|
|
func CardDeleteId(c *gin.Context) {
|
2024-10-31 10:07:51 +01:00
|
|
|
DeleteHandler[*db.Card]()(c)
|
2024-08-03 08:53:38 +02:00
|
|
|
}
|