From 10b17512d7afe3e04f1b8740acf158b171e910df Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 10 Nov 2019 11:10:11 -0500 Subject: Add structure to JSON responses JSON responses contains a status(error) section and a data section Note: JSON is still not sent to client --- backend/main.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file 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 { -- cgit v1.1