66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
|
|
package response
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Response struct {
|
||
|
|
Code int `json:"code"`
|
||
|
|
Message string `json:"message"`
|
||
|
|
Data interface{} `json:"data,omitempty"`
|
||
|
|
Meta *Meta `json:"meta,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type Meta struct {
|
||
|
|
Page int `json:"page"`
|
||
|
|
PageSize int `json:"page_size"`
|
||
|
|
Total int `json:"total"`
|
||
|
|
HasMore bool `json:"has_more"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func OK(c *gin.Context, data interface{}) {
|
||
|
|
c.JSON(http.StatusOK, Response{
|
||
|
|
Code: ErrCodeSuccess,
|
||
|
|
Message: "success",
|
||
|
|
Data: data,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func OKWithMeta(c *gin.Context, data interface{}, meta *Meta) {
|
||
|
|
c.JSON(http.StatusOK, Response{
|
||
|
|
Code: ErrCodeSuccess,
|
||
|
|
Message: "success",
|
||
|
|
Data: data,
|
||
|
|
Meta: meta,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func Error(c *gin.Context, httpCode int, code int, message string) {
|
||
|
|
c.JSON(httpCode, Response{
|
||
|
|
Code: code,
|
||
|
|
Message: message,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func BadRequest(c *gin.Context, message string) {
|
||
|
|
Error(c, http.StatusBadRequest, ErrCodeBadRequest, message)
|
||
|
|
}
|
||
|
|
|
||
|
|
func Unauthorized(c *gin.Context, message string) {
|
||
|
|
Error(c, http.StatusUnauthorized, ErrCodeUnauthorized, message)
|
||
|
|
}
|
||
|
|
|
||
|
|
func Forbidden(c *gin.Context, message string) {
|
||
|
|
Error(c, http.StatusForbidden, ErrCodeForbidden, message)
|
||
|
|
}
|
||
|
|
|
||
|
|
func NotFound(c *gin.Context, message string) {
|
||
|
|
Error(c, http.StatusNotFound, ErrCodeNotFound, message)
|
||
|
|
}
|
||
|
|
|
||
|
|
func InternalError(c *gin.Context, message string) {
|
||
|
|
Error(c, http.StatusInternalServerError, ErrCodeInternal, message)
|
||
|
|
}
|