Compare commits

..

4 Commits

6 changed files with 49 additions and 14 deletions

View File

@ -54,6 +54,7 @@ func main() {
{
cardsRoutes.POST("/add", handlers.CardAdd)
cardsRoutes.GET("/:id", handlers.CardGetId)
cardsRoutes.GET("/all", handlers.CardGetAll)
cardsRoutes.PUT("/edit/:id", handlers.CardPutId)
cardsRoutes.DELETE("/delete/:id", handlers.CardDeleteId)
}

View File

@ -6,6 +6,16 @@ import (
"github.com/gin-gonic/gin"
)
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,
}
}
// @Summary Get card by id
// @Description Get card by id
// @Tags card
@ -20,15 +30,37 @@ import (
// @Security ApiKeyAuth
// @Router /card/:id [get]
func CardGetId(c *gin.Context) {
GetHandler(func(inp *db.Card) types.DbCard {
return types.DbCard{
ID: inp.ID,
Name: inp.Name,
Balance: inp.Balance,
HaveCreditLine: inp.HaveCreditLine,
CreditLine: inp.CreditLine,
}
})(c)
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)
}
// @Summary Get card by id

View File

@ -130,7 +130,7 @@ func IncomeAdd(c *gin.Context) {
return
}
msg := types.Message{
Message: fmt.Sprintf("Income with id %d was successfully created!", dbIncome.ID),
Info: fmt.Sprintf("Income with id %d was successfully created!", dbIncome.ID),
}
c.JSON(200, msg)
}

View File

@ -13,5 +13,5 @@ import (
// @Success 200 {object} types.Message
// @Router /ping [get]
func PingGet(c *gin.Context) {
c.JSON(200, types.Message{Message: "Pong!"})
c.JSON(200, types.Message{Info: "Pong!"})
}

View File

@ -2,6 +2,7 @@ package handlers
import (
"fmt"
"log"
"git.qowevisa.me/Qowevisa/fin-check-api/db"
"git.qowevisa.me/Qowevisa/fin-check-api/types"
@ -58,6 +59,7 @@ func CreateHandler[T db.UserIdentifiable, R any](entity T, applyChanges func(src
var updates R
if err := c.ShouldBindJSON(&updates); err != nil {
log.Printf("err is %v\n", err)
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
}
@ -70,7 +72,7 @@ func CreateHandler[T db.UserIdentifiable, R any](entity T, applyChanges func(src
return
}
c.JSON(200, types.Message{Message: fmt.Sprintf("Entity created with ID %d", entity.GetID())})
c.JSON(200, types.Message{Info: fmt.Sprintf("Entity created with ID %d", entity.GetID())})
}
}
@ -166,6 +168,6 @@ func DeleteHandler[T db.UserIdentifiable]() gin.HandlerFunc {
return
}
c.JSON(200, types.Message{Message: fmt.Sprintf("Entity with ID %d deleted", entity.GetID())})
c.JSON(200, types.Message{Info: fmt.Sprintf("Entity with ID %d deleted", entity.GetID())})
}
}

View File

@ -16,7 +16,7 @@ type Account struct {
}
type Message struct {
Message string `json:"message" example:"Success!"`
Info string `json:"info" example:"Success!"`
}
type ErrorResponse struct {