graphicek/server/routes/utils.go

30 lines
531 B
Go

package routes
import (
"strconv"
"github.com/gin-gonic/gin"
)
func getIntParamOrAbort(c *gin.Context, key string) (int64, error) {
value := c.Param(key)
val, err := strconv.ParseInt(value, 10, 64)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{"error": "Invalid " + key})
return 0, err
}
return val, nil
}
func bindJSONBodyOrAbort(c *gin.Context, body interface{}) error {
if err := c.ShouldBindJSON(body); err != nil {
c.AbortWithStatusJSON(400, gin.H{"error": err.Error()})
return err
}
return nil
}