init: Initial commit
This commit is contained in:
77
app/utils/response.go
Normal file
77
app/utils/response.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type Response interface {
|
||||
JSON() map[string]interface{}
|
||||
Send(c *fiber.Ctx) error
|
||||
}
|
||||
|
||||
type HTTPResponse struct {
|
||||
Code int `json:"code"`
|
||||
Body *fiber.Map `json:"body"`
|
||||
ODataContext string `json:"@odata.context"`
|
||||
ODataCount *int `json:"@odata.count"`
|
||||
ODataNextLink *string `json:"@odata.nextLink"`
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Code int `json:"code"`
|
||||
Body *fiber.Map `json:"body"`
|
||||
}
|
||||
|
||||
func NewHTTPResponse(code int, body *fiber.Map, oDataContext string, oDataCount *int, oDataNextLink *string) *HTTPResponse {
|
||||
return &HTTPResponse{
|
||||
Code: code,
|
||||
Body: body,
|
||||
ODataContext: oDataContext,
|
||||
ODataCount: oDataCount,
|
||||
ODataNextLink: oDataNextLink,
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorResponse(code int, body *fiber.Map) *ErrorResponse {
|
||||
return &ErrorResponse{
|
||||
Code: code,
|
||||
Body: body,
|
||||
}
|
||||
}
|
||||
|
||||
func (res *HTTPResponse) JSON() map[string]interface{} {
|
||||
result := map[string]interface{}{
|
||||
"code": res.Code,
|
||||
}
|
||||
if res.Body != nil {
|
||||
result["body"] = res.Body
|
||||
}
|
||||
if res.ODataContext != "" {
|
||||
result["@odata.context"] = res.ODataContext
|
||||
}
|
||||
if res.ODataCount != nil {
|
||||
result["@odata.count"] = *res.ODataCount
|
||||
}
|
||||
if res.ODataNextLink != nil {
|
||||
result["@odata.nextLink"] = res.ODataNextLink
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (res *ErrorResponse) JSON() map[string]interface{} {
|
||||
result := map[string]interface{}{
|
||||
"code": res.Code,
|
||||
}
|
||||
if res.Body != nil {
|
||||
result["body"] = res.Body
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (res *ErrorResponse) Send(c *fiber.Ctx) error {
|
||||
return c.Status(res.Code).JSON(res.JSON())
|
||||
}
|
||||
|
||||
func (res *HTTPResponse) Send(c *fiber.Ctx) error {
|
||||
return c.Status(res.Code).JSON(res.JSON())
|
||||
}
|
||||
Reference in New Issue
Block a user