summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-11-10 11:10:11 -0500
committerTucker Evans <tuckerevans24@gmail.com>2019-11-10 11:10:11 -0500
commit10b17512d7afe3e04f1b8740acf158b171e910df (patch)
treead5baa9c64d2e4bda059c1a72bb2d35a54edf9b8
parent7995a8bd93ab6a98c6e4a78951e60e37e475388a (diff)
Add structure to JSON responses
JSON responses contains a status(error) section and a data section Note: JSON is still not sent to client
-rw-r--r--backend/main.go39
1 files changed, 37 insertions, 2 deletions
diff --git a/backend/main.go b/backend/main.go
index 4679e73..3dd9d38 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -8,6 +8,29 @@ import _ "github.com/lib/pq"
import "database/sql"
import "encoding/json"
+type APIError struct {
+ Code int
+ Msg string
+}
+
+type APIDataIds struct {
+ Ids interface{}
+}
+
+type APIDataRecipe struct {
+ Recipe interface{}
+}
+
+type APIResponseList struct {
+ Status APIError
+ Data []APIDataIds
+}
+
+type APIResponseItem struct {
+ Status APIError
+ Data []APIDataRecipe
+}
+
func RecipeList(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
var ids []int
@@ -22,7 +45,13 @@ func RecipeList(w http.ResponseWriter, r *http.Request) {
}
}
- output, err := json.MarshalIndent(ids, "", " ")
+ resp := APIResponseList{
+ Status: APIError{Code: 200, Msg: "Successful Request"},
+ Data: make([]APIDataIds, 0),
+ }
+ resp.Data = append(resp.Data, APIDataIds{Ids: ids})
+
+ output, err := json.MarshalIndent(resp, "", " ")
if err != nil {
fmt.Println("Error converting to JSON")
} else {
@@ -43,7 +72,13 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) {
recipe = MakeRecipe()
}
- output, err := json.MarshalIndent(recipe, "", " ")
+ resp := APIResponseItem{
+ Status: APIError{Code: 200, Msg: "Successful Request"},
+ Data: make([]APIDataRecipe, 0),
+ }
+ resp.Data = append(resp.Data, APIDataRecipe{recipe})
+
+ output, err := json.MarshalIndent(resp, "", " ")
if err != nil {
fmt.Println("Error converting to JSON")
} else {