2025-11-17 12:17:44 +07:00

67 lines
1.7 KiB
Go

package utils
import (
"github.com/gin-gonic/gin"
)
// Response represents a standard API response
type Response struct {
Success bool `json:"success"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}
// PaginatedResponse represents a paginated API response
type PaginatedResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Data interface{} `json:"data"`
Pagination Pagination `json:"pagination"`
}
// Pagination represents pagination metadata
type Pagination struct {
CurrentPage int `json:"current_page"`
PerPage int `json:"per_page"`
TotalPages int `json:"total_pages"`
TotalRecords int64 `json:"total_records"`
}
// SuccessResponse sends a success response
func SuccessResponse(ctx *gin.Context, statusCode int, message string, data interface{}) {
ctx.JSON(statusCode, Response{
Success: true,
Message: message,
Data: data,
})
}
// ErrorResponse sends an error response
func ErrorResponse(ctx *gin.Context, statusCode int, message string, error string) {
ctx.JSON(statusCode, Response{
Success: false,
Message: message,
Error: error,
})
}
// SendPaginatedResponse sends a paginated response (nama fungsi diubah untuk menghindari konflik)
func SendPaginatedResponse(ctx *gin.Context, statusCode int, message string, data interface{}, total int64, page, limit int) {
totalPages := int(total) / limit
if int(total)%limit != 0 {
totalPages++
}
ctx.JSON(statusCode, PaginatedResponse{
Success: true,
Message: message,
Data: data,
Pagination: Pagination{
CurrentPage: page,
PerPage: limit,
TotalPages: totalPages,
TotalRecords: total,
},
})
}