github.com/soulteary/pocket-bookcase@v0.0.0-20240428065142-0b5a9a0fc98a/internal/http/response/response.go (about) 1 package response 2 3 import ( 4 "github.com/gin-gonic/gin" 5 ) 6 7 type Response struct { 8 // Response payload 9 // Ok if the response was successful or not 10 Ok bool `json:"ok"` 11 12 // Message the payload of the response, depending on the endpoint/response status 13 Message any `json:"message"` 14 15 // ErrorParams parameters defined if the response is not successful to help client's debugging 16 ErrorParams map[string]string `json:"error_params,omitempty"` 17 18 // statusCode used for the http response status code 19 statusCode int 20 } 21 22 func (m *Response) IsError() bool { 23 return !m.Ok 24 } 25 26 func (m *Response) GetMessage() any { 27 return m.Message 28 } 29 30 func (m *Response) Send(c *gin.Context) { 31 c.Status(m.statusCode) 32 c.JSON(m.statusCode, m) 33 } 34 35 func NewResponse(ok bool, message any, errorParams map[string]string, statusCode int) *Response { 36 return &Response{ 37 Ok: ok, 38 Message: message, 39 ErrorParams: errorParams, 40 statusCode: statusCode, 41 } 42 }