- Go + Gin + GORM + PostgreSQL backend - RESTful API for material management - Docker deployment support - Database partitioning for billion-scale data - API documentation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
73 lines
1.4 KiB
Go
73 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"`
|
|
}
|
|
|
|
type PagedData struct {
|
|
Items interface{} `json:"items"`
|
|
Total int64 `json:"total"`
|
|
Page int `json:"page"`
|
|
PageSize int `json:"page_size"`
|
|
}
|
|
|
|
func Success(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: 0,
|
|
Message: "success",
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func SuccessPaged(c *gin.Context, items interface{}, total int64, page, pageSize int) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: 0,
|
|
Message: "success",
|
|
Data: PagedData{
|
|
Items: items,
|
|
Total: total,
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
},
|
|
})
|
|
}
|
|
|
|
func Created(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusCreated, Response{
|
|
Code: 0,
|
|
Message: "created",
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func Error(c *gin.Context, statusCode int, message string) {
|
|
c.JSON(statusCode, Response{
|
|
Code: statusCode,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
func BadRequest(c *gin.Context, message string) {
|
|
Error(c, http.StatusBadRequest, message)
|
|
}
|
|
|
|
func NotFound(c *gin.Context, message string) {
|
|
Error(c, http.StatusNotFound, message)
|
|
}
|
|
|
|
func Unauthorized(c *gin.Context, message string) {
|
|
Error(c, http.StatusUnauthorized, message)
|
|
}
|
|
|
|
func InternalError(c *gin.Context, message string) {
|
|
Error(c, http.StatusInternalServerError, message)
|
|
}
|