forked from takuya152005/go-gin-xorm-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
require_path_parameter.go
36 lines (31 loc) · 984 Bytes
/
require_path_parameter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package server
import (
"fmt"
"strconv"
"github.com/itomofumi/go-gin-xorm-starter/model"
"github.com/gin-gonic/gin"
)
// RequirePathParam parses PathParameter as uint64 by given param and then sets it to gin Context.
func RequirePathParam(param string) gin.HandlerFunc {
return func(c *gin.Context) {
id, err := strconv.ParseUint(c.Param(param), 10, 64)
if err != nil {
c.AbortWithStatusJSON(400, model.NewErrorResponse("400", model.ErrorParam, fmt.Sprintf("%v must be a positive number", param), err.Error()))
return
}
c.Set(param, id)
c.Next()
}
}
// RequireStringPathParam parses PathParameter as string by given param and then sets it to gin Context.
func RequireStringPathParam(param string) gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param(param)
if id == "" {
c.AbortWithStatusJSON(400, model.NewErrorResponse("400", model.ErrorParam, fmt.Sprintf("%v must not be empty", param)))
return
}
c.Set(param, id)
c.Next()
}
}