Add metric /all endpoint and handler

This commit is contained in:
qowevisa 2024-11-19 10:13:41 +02:00
parent f4342ba019
commit a52b108bf8
2 changed files with 44 additions and 0 deletions

View File

@ -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))

40
handlers/metric.go Normal file
View File

@ -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)
}