material_manage_system/pkg/response/response.go
2026-09-15 16:08:18 +08:00

87 lines
1.7 KiB
Go

package response
import (
"github.com/gin-gonic/gin"
)
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
type PaginatedData struct {
Items interface{} `json:"items"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Total int64 `json:"total"`
HasMore bool `json:"has_more"`
}
func Success(c *gin.Context, data interface{}) {
c.JSON(200, Response{
Code: 0,
Message: "success",
Data: data,
})
}
func PaginatedSuccess(c *gin.Context, items interface{}, limit, offset int, total int64) {
hasMore := int64(offset+limit) < total
c.JSON(200, Response{
Code: 0,
Message: "success",
Data: PaginatedData{
Items: items,
Limit: limit,
Offset: offset,
Total: total,
HasMore: hasMore,
},
})
}
func SuccessWithMessage(c *gin.Context, message string, data interface{}) {
c.JSON(200, Response{
Code: 0,
Message: message,
Data: data,
})
}
func Created(c *gin.Context, message string, data interface{}) {
c.JSON(201, Response{
Code: 0,
Message: message,
Data: data,
})
}
func Error(c *gin.Context, httpStatus int, message string) {
c.JSON(httpStatus, Response{
Code: httpStatus,
Message: message,
Data: nil,
})
}
func BadRequest(c *gin.Context, message string) {
Error(c, 400, message)
}
func NotFound(c *gin.Context, message string) {
Error(c, 404, message)
}
func Unauthorized(c *gin.Context, message string) {
Error(c, 401, message)
}
func Forbidden(c *gin.Context, message string) {
Error(c, 403, message)
}
func InternalError(c *gin.Context, message string) {
Error(c, 500, message)
}