Files
xingrin/server/internal/pkg/response.go
2026-01-15 16:19:00 +08:00

140 lines
3.1 KiB
Go

package pkg
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Response represents a standard API response
type Response struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
Error *ErrorInfo `json:"error,omitempty"`
Meta *Meta `json:"meta,omitempty"`
}
// ErrorInfo represents error information
type ErrorInfo struct {
Code string `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
}
// Meta represents pagination metadata
type Meta struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
TotalCount int64 `json:"totalCount"`
TotalPages int `json:"totalPages"`
}
// OK sends a successful response with data
func OK(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, Response{
Success: true,
Data: data,
})
}
// OKWithMeta sends a successful response with data and pagination
func OKWithMeta(c *gin.Context, data interface{}, meta *Meta) {
c.JSON(http.StatusOK, Response{
Success: true,
Data: data,
Meta: meta,
})
}
// Created sends a 201 Created response
func Created(c *gin.Context, data interface{}) {
c.JSON(http.StatusCreated, Response{
Success: true,
Data: data,
})
}
// NoContent sends a 204 No Content response
func NoContent(c *gin.Context) {
c.Status(http.StatusNoContent)
}
// BadRequest sends a 400 Bad Request response
func BadRequest(c *gin.Context, message string) {
c.JSON(http.StatusBadRequest, Response{
Success: false,
Error: &ErrorInfo{
Code: "BAD_REQUEST",
Message: message,
},
})
}
// Unauthorized sends a 401 Unauthorized response
func Unauthorized(c *gin.Context, message string) {
c.JSON(http.StatusUnauthorized, Response{
Success: false,
Error: &ErrorInfo{
Code: "UNAUTHORIZED",
Message: message,
},
})
}
// Forbidden sends a 403 Forbidden response
func Forbidden(c *gin.Context, message string) {
c.JSON(http.StatusForbidden, Response{
Success: false,
Error: &ErrorInfo{
Code: "FORBIDDEN",
Message: message,
},
})
}
// NotFound sends a 404 Not Found response
func NotFound(c *gin.Context, message string) {
c.JSON(http.StatusNotFound, Response{
Success: false,
Error: &ErrorInfo{
Code: "NOT_FOUND",
Message: message,
},
})
}
// InternalError sends a 500 Internal Server Error response
func InternalError(c *gin.Context, message string) {
c.JSON(http.StatusInternalServerError, Response{
Success: false,
Error: &ErrorInfo{
Code: "INTERNAL_ERROR",
Message: message,
},
})
}
// ValidationError sends a 422 Unprocessable Entity response
func ValidationError(c *gin.Context, message string, details string) {
c.JSON(http.StatusUnprocessableEntity, Response{
Success: false,
Error: &ErrorInfo{
Code: "VALIDATION_ERROR",
Message: message,
Details: details,
},
})
}
// CalculateTotalPages calculates total pages from count and page size
func CalculateTotalPages(totalCount int64, pageSize int) int {
if pageSize <= 0 {
return 0
}
pages := int(totalCount) / pageSize
if int(totalCount)%pageSize > 0 {
pages++
}
return pages
}