/
web.go
82 lines (66 loc) · 1.59 KB
/
web.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package util
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-errors/errors"
"strconv"
)
// DefaultLimit elements in page in paged requests
const DefaultLimit = 20
// MaxLimit max elements in page in paged requests
const MaxLimit = 200
type PageParams struct {
Offset int `json:"offset" form:"offset"`
Limit int `json:"limit" form:"limit"`
}
func (pp *PageParams) Fix() {
if pp.Limit > MaxLimit {
pp.Limit = MaxLimit
}
if pp.Limit == 0 {
pp.Limit = DefaultLimit
}
}
type ListDancerParams struct {
Offset int `json:"offset" form:"offset"`
Limit int `json:"limit" form:"limit"`
Query string `form:"query"`
}
func (pp *ListDancerParams) Fix() {
if pp.Limit > MaxLimit {
pp.Limit = MaxLimit
}
if pp.Limit == 0 {
pp.Limit = DefaultLimit
}
}
type PageResponse struct {
TotalCount int `json:"totalCount"`
PageSize int `json:"pageSize"`
Count int `json:"count"`
Content interface{} `json:"content"`
}
func WriteJSONStatus(c *gin.Context, model interface{}, httpStatus int) {
c.JSON(httpStatus, model)
}
func ParseParamsGet(c *gin.Context, params interface{}) {
if err := c.BindWith(params, binding.Form); err != nil {
panic(err)
}
}
// Atoi64 parses int64 from string
func Atoi64(s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}
// GetPathInt64Param returns path param with key
func GetPathInt64Param(c *gin.Context, key string) int64 {
value, exists := c.Params.Get(key)
if !exists {
panic(errors.New("path param not exists"))
}
intVal, err := Atoi64(value)
if err != nil {
panic(err)
}
return intVal
}