From a52b108bf88241f59eae14eb8e5b1d3c3e9d4686 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Tue, 19 Nov 2024 10:13:41 +0200 Subject: [PATCH] Add metric /all endpoint and handler --- cmd/http-server/main.go | 4 ++++ handlers/metric.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 handlers/metric.go diff --git a/cmd/http-server/main.go b/cmd/http-server/main.go index c06440a..2f90b4f 100644 --- a/cmd/http-server/main.go +++ b/cmd/http-server/main.go @@ -119,6 +119,10 @@ func main() { itemRoutes.POST("/filter", handlers.ItemPostFilter) itemRoutes.DELETE("/delete/:id", handlers.ItemDeleteId) } + metricRoutes := api.Group("/metric", middleware.AuthMiddleware()) + { + metricRoutes.GET("/all", handlers.MetricGetAll) + } } r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) diff --git a/handlers/metric.go b/handlers/metric.go new file mode 100644 index 0000000..1120389 --- /dev/null +++ b/handlers/metric.go @@ -0,0 +1,40 @@ +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 metricTransform func(inp *db.Metric) types.DbMetric = func(inp *db.Metric) types.DbMetric { + return types.DbMetric{ + Value: inp.Value, + Name: inp.Name, + Short: inp.Short, + } +} + +// @Summary Get all metrics for user +// @Description Get all metrics for user +// @Tags type +// @Produce json +// @Param Authorization header string true "Bearer token" +// @Success 200 {object} []types.DbMetric +// @Failure 401 {object} types.ErrorResponse +// @Failure 500 {object} types.ErrorResponse +// @Security ApiKeyAuth +// @Router /metric/all [get] +func MetricGetAll(c *gin.Context) { + dbc := db.Connect() + var entities []*db.Metric + if err := dbc.Find(&entities).Error; err != nil { + c.JSON(500, types.ErrorResponse{Message: err.Error()}) + return + } + + var ret []types.DbMetric + for _, entity := range entities { + ret = append(ret, metricTransform(entity)) + } + c.JSON(200, ret) +}