From 40ec90687aec4f077c7f62c21530ecb468cba6a6 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Wed, 20 Nov 2024 20:29:00 +0200 Subject: [PATCH] Add currency/all endpoint and handler --- cmd/http-server/main.go | 4 ++++ handlers/currency.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 handlers/currency.go diff --git a/cmd/http-server/main.go b/cmd/http-server/main.go index 8003c4b..dd8e912 100644 --- a/cmd/http-server/main.go +++ b/cmd/http-server/main.go @@ -127,6 +127,10 @@ func main() { { paymentRoutes.POST("/add", handlers.PaymentAdd) } + currencyRoutes := api.Group("/currency", middleware.AuthMiddleware()) + { + currencyRoutes.GET("/all", handlers.CurrencyGetAll) + } } r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) diff --git a/handlers/currency.go b/handlers/currency.go new file mode 100644 index 0000000..c950306 --- /dev/null +++ b/handlers/currency.go @@ -0,0 +1,41 @@ +package handlers + +import ( + "git.qowevisa.me/Qowevisa/fin-check-api/db" + "git.qowevisa.me/Qowevisa/fin-check-api/types" + "github.com/gin-gonic/gin" +) + +var currencyTransform func(inp *db.Currency) types.DbCurrency = func(inp *db.Currency) types.DbCurrency { + return types.DbCurrency{ + ID: inp.ID, + Name: inp.Name, + ISOName: inp.ISOName, + Symbol: inp.Symbol, + } +} + +// @Summary Get all currencies for user +// @Description Get all currencies for user +// @Tags type +// @Produce json +// @Param Authorization header string true "Bearer token" +// @Success 200 {object} []types.DbCurrency +// @Failure 401 {object} types.ErrorResponse +// @Failure 500 {object} types.ErrorResponse +// @Security ApiKeyAuth +// @Router /currency/all [get] +func CurrencyGetAll(c *gin.Context) { + dbc := db.Connect() + var entities []*db.Currency + if err := dbc.Find(&entities).Error; err != nil { + c.JSON(500, types.ErrorResponse{Message: err.Error()}) + return + } + + var ret []types.DbCurrency + for _, entity := range entities { + ret = append(ret, currencyTransform(entity)) + } + c.JSON(200, ret) +}