-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
52 lines (43 loc) · 1.22 KB
/
response.go
File metadata and controls
52 lines (43 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package apimaker
import (
"github.com/labstack/echo/v4"
)
type (
Response struct {
Code int `json:"code"`
SuccessMessage string `json:"success_message"`
ErrorMessage string `json:"error_message"`
Data interface{} `json:"data"`
MetaData MetaData `json:"metadata"`
}
MetaData struct {
Limit int `json:"limit"`
TotalCounts int `json:"total_counts"`
TotalPages int `json:"total_pages"`
CurrentPage int `json:"current_page"`
NextPage int `json:"next_page"`
Sort string `json:"sort"`
}
)
// SuccessResponse handles sending success responses.
func SuccessResponse(c echo.Context, code int, message string, data echo.Map, metaData MetaData) error {
resp := &Response{
Code: code,
SuccessMessage: message,
Data: data,
MetaData: metaData,
}
return c.JSON(code, resp)
}
// ErrorResponse handles sending error responses.
func (a *APIService) ErrorResponse(c echo.Context, code int, err error, message string) error {
a.Logger.Errorf("%s: %v", message, err)
resp := &Response{
Code: code,
ErrorMessage: message,
}
if err != nil {
resp.ErrorMessage = err.Error()
}
return c.JSON(code, resp)
}