From 2cca30a487714d7ca242300195d689754259a708 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 3 Aug 2024 08:16:18 +0300 Subject: [PATCH] Create handlers for cards --- handlers/card.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 handlers/card.go diff --git a/handlers/card.go b/handlers/card.go new file mode 100644 index 0000000..4532508 --- /dev/null +++ b/handlers/card.go @@ -0,0 +1,88 @@ +package handlers + +import ( + "fmt" + "strconv" + + "git.qowevisa.me/Qowevisa/gonuts/db" + "git.qowevisa.me/Qowevisa/gonuts/types" + "github.com/gin-gonic/gin" +) + +// @Summary Get card by id +// @Description Get card by id +// @Tags card +// @Accept json +// @Produce json +// @Param card param int true "id" +// @Success 200 {object} types.DbCard +// @Failure 400 {object} types.ErrorResponse +// @Failure 500 {object} types.ErrorResponse +// @Router /card/:id [get] +func CardGetId(c *gin.Context) { + idStr := c.Param("id") + var id uint + if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil { + c.JSON(400, types.ErrorResponse{Message: "Invalid request"}) + return + } else { + id = uint(idVal) + } + + var dbCard db.Card + dbc := db.Connect() + if err := dbc.Find(&dbCard, id).Error; err != nil { + c.JSON(500, types.ErrorResponse{Message: err.Error()}) + return + } + if dbCard.ID == 0 { + c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"}) + return + } + card := types.DbCard{ + Name: dbCard.Name, + Value: dbCard.Value, + HaveCreditLine: dbCard.HaveCreditLine, + CreditLine: dbCard.CreditLine, + } + c.JSON(200, card) +} + +// @Summary Get card by id +// @Description Get card by id +// @Tags card +// @Accept json +// @Produce json +// @Param card body types.DbCard true "Card" +// @Success 200 {object} types.Message +// @Failure 400 {object} types.ErrorResponse +// @Failure 500 {object} types.ErrorResponse +// @Router /card/:id [get] +func CardAdd(c *gin.Context) { + var card types.DbCard + if err := c.ShouldBindJSON(&card); err != nil { + c.JSON(400, types.ErrorResponse{Message: "Invalid request"}) + return + } + + dbCard := &db.Card{ + Name: card.Name, + Value: card.Value, + HaveCreditLine: card.HaveCreditLine, + CreditLine: card.CreditLine, + } + dbc := db.Connect() + if err := dbc.Create(&dbCard).Error; err != nil { + c.JSON(500, types.ErrorResponse{Message: err.Error()}) + return + } + if dbCard.ID == 0 { + c.JSON(500, types.ErrorResponse{Message: "DAFUQ004"}) + return + } + msg := types.Message{ + Message: fmt.Sprintf("Card with id %d was successfully created!", dbCard.ID), + } + c.JSON(200, msg) +} +