69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type APIResponse struct {
|
|
Success bool `json:"success"`
|
|
Message string `json:"message,omitempty"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
}
|
|
|
|
type PaginatedData struct {
|
|
Items interface{} `json:"items"`
|
|
Total int `json:"total"`
|
|
Page int `json:"page"`
|
|
Limit int `json:"limit"`
|
|
TotalPages int `json:"total_pages"`
|
|
}
|
|
|
|
func Success(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, APIResponse{
|
|
Success: true,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func Created(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusCreated, APIResponse{
|
|
Success: true,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func Error(c *gin.Context, code int, message string) {
|
|
c.JSON(code, APIResponse{
|
|
Success: false,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
func SuccessWithMessage(c *gin.Context, message string, data interface{}) {
|
|
c.JSON(http.StatusOK, APIResponse{
|
|
Success: true,
|
|
Message: message,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func PaginatedResponse(c *gin.Context, items interface{}, total, page, limit int) {
|
|
totalPages := total / limit
|
|
if total%limit > 0 {
|
|
totalPages++
|
|
}
|
|
|
|
c.JSON(http.StatusOK, APIResponse{
|
|
Success: true,
|
|
Data: PaginatedData{
|
|
Items: items,
|
|
Total: total,
|
|
Page: page,
|
|
Limit: limit,
|
|
TotalPages: totalPages,
|
|
},
|
|
})
|
|
}
|