71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package utils
|
|
|
|
import "net/http"
|
|
|
|
type Response struct {
|
|
Status bool `json:"status"`
|
|
Message string `json:"message"`
|
|
Error any `json:"error,omitempty"`
|
|
Data any `json:"data,omitempty"`
|
|
Meta any `json:"meta,omitempty"`
|
|
}
|
|
|
|
type ResponseWithPagination struct {
|
|
Code int `json:"code"`
|
|
Status string `json:"status"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data"`
|
|
Pagination PaginationResponse `json:"pagination"`
|
|
}
|
|
|
|
type EmptyObj struct{}
|
|
|
|
func BuildResponseSuccess(message string, data any) Response {
|
|
res := Response{
|
|
Status: true,
|
|
Message: message,
|
|
Data: data,
|
|
}
|
|
return res
|
|
}
|
|
|
|
func BuildResponseFailed(message string, err string, data any) Response {
|
|
res := Response{
|
|
Status: false,
|
|
Message: message,
|
|
Error: err,
|
|
Data: data,
|
|
}
|
|
return res
|
|
}
|
|
|
|
type PaginationResponse struct {
|
|
Page int `json:"page"`
|
|
PerPage int `json:"per_page"`
|
|
MaxPage int `json:"max_page"`
|
|
Total int64 `json:"total"`
|
|
}
|
|
|
|
func BuildPaginationResponse(perPage, page int, total int64) PaginationResponse {
|
|
maxPage := 1
|
|
if perPage > 0 {
|
|
maxPage = int((total + int64(perPage) - 1) / int64(perPage))
|
|
}
|
|
return PaginationResponse{
|
|
Page: page,
|
|
PerPage: perPage,
|
|
MaxPage: maxPage,
|
|
Total: total,
|
|
}
|
|
}
|
|
|
|
func BuildResponseSuccessWithPagination(code int, message string, data interface{}, pagination PaginationResponse) ResponseWithPagination {
|
|
return ResponseWithPagination{
|
|
Code: code,
|
|
Status: http.StatusText(code),
|
|
Message: message,
|
|
Data: data,
|
|
Pagination: pagination,
|
|
}
|
|
}
|