@@ -5,14 +5,44 @@ import (
55 "net/http"
66)
77
8- func respondWithJSON (w http.ResponseWriter , code int , payload interface {}) {
9- response , _ := json .Marshal (payload )
8+ type BasicApiResponse struct {
9+ Data interface {} `json:"data"`
10+ Status int `json:"status"`
11+ Total int `json:"total"`
12+ Message string `json:"message"`
13+ }
14+
15+ func respondWithJSON (w http.ResponseWriter , code int , payload map [string ]interface {}) {
16+
17+ var apiResponse BasicApiResponse
18+
19+ apiResponse .Data = payload ["data" ]
20+ if ! isNil (apiResponse .Data ) {
21+ apiResponse .Status = 1
22+ }
23+
24+ if ! isNil (payload ["status" ]) {
25+ apiResponse .Status = payload ["status" ].(int )
26+ }
27+
28+ if ! isNil (payload ["total" ]) {
29+ apiResponse .Total = payload ["total" ].(int )
30+ }
31+
32+ if ! isNil (payload ["message" ].(string )) {
33+ apiResponse .Message = payload ["message" ].(string )
34+ }
35+
36+ if isNil (apiResponse .Total ) {
37+ apiResponse .Total = apiResponse .Status
38+ }
1039
40+ response , _ := json .Marshal (apiResponse )
1141 w .Header ().Set ("Content-Type" , "application/json" )
1242 w .WriteHeader (code )
1343 w .Write (response )
1444}
1545
1646func respondWithError (w http.ResponseWriter , code int , message string ) {
17- respondWithJSON (w , code , map [string ]string { "error " : message })
47+ respondWithJSON (w , code , map [string ]interface {}{ "message " : message })
1848}
0 commit comments