65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type APIResponse struct {
|
|
Success bool `json:"success"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// SuccessResponse sends a successful response
|
|
func SuccessResponse(c *gin.Context, statusCode int, message string, data interface{}) {
|
|
c.JSON(statusCode, APIResponse{
|
|
Success: true,
|
|
Message: message,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// ErrorResponse sends an error response
|
|
func ErrorResponse(c *gin.Context, statusCode int, message string) {
|
|
c.JSON(statusCode, APIResponse{
|
|
Success: false,
|
|
Error: message,
|
|
})
|
|
}
|
|
|
|
// ValidationErrorResponse sends a validation error response
|
|
func ValidationErrorResponse(c *gin.Context, errors interface{}) {
|
|
c.JSON(http.StatusBadRequest, APIResponse{
|
|
Success: false,
|
|
Error: "Validation failed",
|
|
Data: errors,
|
|
})
|
|
}
|
|
|
|
// PaginatedResponse sends a paginated response
|
|
type PaginatedResponse struct {
|
|
Data interface{} `json:"data"`
|
|
Pagination Pagination `json:"pagination"`
|
|
}
|
|
|
|
type Pagination struct {
|
|
Page int `json:"page"`
|
|
PerPage int `json:"per_page"`
|
|
Total int64 `json:"total"`
|
|
TotalPages int `json:"total_pages"`
|
|
}
|
|
|
|
func PaginatedSuccessResponse(c *gin.Context, message string, data interface{}, pagination Pagination) {
|
|
c.JSON(http.StatusOK, APIResponse{
|
|
Success: true,
|
|
Message: message,
|
|
Data: PaginatedResponse{
|
|
Data: data,
|
|
Pagination: pagination,
|
|
},
|
|
})
|
|
}
|