/
handlers.helpers.go
59 lines (52 loc) · 1.37 KB
/
handlers.helpers.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
package helpers
import (
"github.com/gin-gonic/gin"
"net/http"
)
const (
MessageBadRequest = "Bad Request"
MessageBadRequestInsufficientData = "Bad Request insufficient data"
MessageInvalidBody = "Invalid Body"
MessageTemplateInvalidValue = "Invalid %s, please check"
MessageError = "Message"
)
func BadRequest(c *gin.Context, message string) {
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
"message": message,
})
c.Abort()
}
func InternalServerError(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": "Internal server error, Something went wrong !",
})
c.Abort()
}
func StatusOK(c *gin.Context, data interface{}, message string) {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": message,
"data": data,
})
c.Abort()
}
func StatusOKPag(c *gin.Context, data interface{}, pag Pagination, message string) {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": message,
"data": data,
"pagination": pag.Validate(),
})
c.Abort()
}
func InfoHandler(info map[string]interface{}) gin.HandlerFunc {
_info := map[string]interface{}{}
for key, value := range info {
_info[key] = value
}
return func(c *gin.Context) {
StatusOK(c, _info, "Auto Generated Routes")
}
}