first commit
This commit is contained in:
64
backend/api/utils/response.go
Normal file
64
backend/api/utils/response.go
Normal file
@@ -0,0 +1,64 @@
|
||||
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,
|
||||
},
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user