Skip to content

Commit 0db3059

Browse files
committed
updated request response, created better response structure
1 parent 732d82f commit 0db3059

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

app.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (a *App) Run(addr string) {
3636

3737
func (a *App) initializeRoutes() {
3838
api := a.Router.PathPrefix("/api/v1").Subrouter()
39-
api.HandleFunc("", a.getRequest).Methods(http.MethodGet)
40-
api.HandleFunc("", a.apiMakeDbRequest).Methods(http.MethodPost)
41-
api.HandleFunc("", a.notFound)
39+
api.HandleFunc("/", a.getRequest).Methods(http.MethodGet)
40+
api.HandleFunc("/make-db-request", a.apiMakeDbRequest).Methods(http.MethodPost)
41+
api.HandleFunc("/", a.notFound)
4242
api.HandleFunc("/user/{userID}/comment/{commentID}", a.withParams).Methods(http.MethodGet)
4343
}

common.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
)
6+
7+
func isNil(i interface{}) bool {
8+
if i == nil {
9+
return true
10+
}
11+
switch reflect.TypeOf(i).Kind() {
12+
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
13+
return reflect.ValueOf(i).IsNil()
14+
}
15+
return false
16+
}

responseHandlers.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1646
func 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
}

routeFunctions.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,34 @@ func (a *App) apiMakeDbRequest(w http.ResponseWriter, r *http.Request) {
3434

3535
var queryrequest QueryRequest
3636
json.Unmarshal(reqBody, &queryrequest)
37-
fmt.Println(queryrequest)
37+
// fmt.Println(queryrequest)
38+
39+
var response []query_response
3840

39-
var response interface{}
4041
switch executeOnly_state {
4142
case 0:
4243
response, err = do_db_select_query(a.DB, queryrequest.QueryString)
43-
4444
default:
4545
err = do_db_query_exec(a.DB, queryrequest.QueryString)
46-
response = map[string]interface{}{"status": 1}
47-
4846
}
4947

5048
if err != nil {
5149
respondWithError(w, http.StatusInternalServerError, err.Error())
5250
return
5351
}
54-
respondWithJSON(w, http.StatusOK, response)
5552

53+
res := make(map[string]interface{})
54+
res["message"] = "db query result"
55+
res["data"] = response
56+
57+
if executeOnly_state == 0 {
58+
res["total"] = len(response)
59+
} else {
60+
res["total"] = 1
61+
res["status"] = 1
62+
}
63+
64+
respondWithJSON(w, http.StatusOK, res)
5665
}
5766

5867
func (a *App) getRequest(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)