48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
// Package response provides standardized JSON response helpers.
|
|
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Response defines the standard JSON response structure.
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
}
|
|
|
|
// Success sends a standardized success response.
|
|
func Success(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: 0,
|
|
Message: "Success",
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// Error sends a standardized error response.
|
|
func Error(c *gin.Context, code int, message string) {
|
|
c.JSON(code, Response{
|
|
Code: code,
|
|
Message: message,
|
|
Data: nil,
|
|
})
|
|
}
|
|
|
|
// BadRequest sends a 400 Bad Request error response.
|
|
func BadRequest(c *gin.Context, message string) {
|
|
Error(c, http.StatusBadRequest, message)
|
|
}
|
|
|
|
// NotFound sends a 404 Not Found error response.
|
|
func NotFound(c *gin.Context, message string) {
|
|
Error(c, http.StatusNotFound, message)
|
|
}
|
|
|
|
// InternalError sends a 500 Internal Server Error response.
|
|
func InternalError(c *gin.Context, message string) {
|
|
Error(c, http.StatusInternalServerError, message)
|
|
} |