From 496cfab1e8867d5a8ff54402b848a6a38d03b1b0 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 14 Nov 2019 13:39:21 -0500 Subject: Fix simplify APIResponse struct using interface{} Combines APIResponseItem and APIResponseList into one struct. Hopefully this allows for the combination of some response code (a function that sends a APIResponse that can be used by each http method) --- backend/main.go | 92 +++++++++++++++++++++----------------------------------- backend/todo.txt | 3 +- 2 files changed, 36 insertions(+), 59 deletions(-) diff --git a/backend/main.go b/backend/main.go index 57e6ab2..96bb824 100644 --- a/backend/main.go +++ b/backend/main.go @@ -10,27 +10,16 @@ import _ "github.com/lib/pq" import "database/sql" import "encoding/json" -type APIError struct { +type APIStatus struct { Code int Msg string } -type APIDataIds struct { - Ids interface{} +type APIResponse struct { + Status APIStatus + Data 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) { @@ -48,11 +37,10 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { } } - resp := APIResponseList{ - Status: APIError{Code: 200, Msg: "Successful Request"}, - Data: make([]APIDataIds, 0), + resp := APIResponse{ + Status: APIStatus{Code: 200, Msg: "Successful Request"}, + Data: ids, } - resp.Data = append(resp.Data, APIDataIds{Ids: ids}) w.Header().Set("Content-Type", "application/json; charset=UTF-8") @@ -80,11 +68,10 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusUnprocessableEntity) w.Header().Set("Content-Type", "application/json; charset=UTF-8") - resp := APIResponseItem{ - Status: APIError{ + resp := APIResponse{ + Status: APIStatus{ Code: http.StatusUnprocessableEntity, Msg: "Invalid Recipe"}, - Data: make([]APIDataRecipe, 0), } err := json.NewEncoder(w).Encode(resp) @@ -97,14 +84,12 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { err = AddRecipeDB(recipe, db) if err != nil { fmt.Println(err) - resp := APIResponseItem{ - Status: APIError{Code: http.StatusBadRequest, + resp := APIResponse{ + Status: APIStatus{Code: http.StatusBadRequest, Msg: "Recipe could not be added"}, - Data: make([]APIDataRecipe, 0), + Data: recipe, } - resp.Data = append(resp.Data, APIDataRecipe{recipe}) - w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusBadRequest) @@ -114,14 +99,12 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { return } - resp := APIResponseItem{ - Status: APIError{Code: http.StatusCreated, + resp := APIResponse{ + Status: APIStatus{Code: http.StatusCreated, Msg: "Recipe added successfully"}, - Data: make([]APIDataRecipe, 0), + Data: recipe, } - resp.Data = append(resp.Data, APIDataRecipe{recipe}) - w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusCreated) if err := json.NewEncoder(w).Encode(resp); err != nil { @@ -131,8 +114,8 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { return } - resp := APIResponseItem{ - Status: APIError{Code: http.StatusMethodNotAllowed, + resp := APIResponse{ + Status: APIStatus{Code: http.StatusMethodNotAllowed, Msg: "Invalid method"}, Data: nil, } @@ -164,13 +147,12 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { msg = "Successful" } - resp := APIResponseItem{ - Status: APIError{Code: status, Msg: msg}, - Data: make([]APIDataRecipe, 0), + resp := APIResponse{ + Status: APIStatus{Code: status, Msg: msg}, } if status == http.StatusOK { - resp.Data = append(resp.Data, APIDataRecipe{recipe}) + resp.Data = recipe } w.Header().Set("Content-Type", "application/json; charset=UTF-8") @@ -192,8 +174,8 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { } else { status = http.StatusConflict } - resp := APIResponseItem{ - Status: APIError{Code: status, Msg: "Cannot add to specific resource"}, + resp := APIResponse{ + Status: APIStatus{Code: status, Msg: "Cannot add to specific resource"}, Data: nil, } @@ -222,11 +204,10 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { fmt.Println(err) w.WriteHeader(http.StatusUnprocessableEntity) w.Header().Set("Content-Type", "application/json; charset=UTF-8") - resp := APIResponseItem{ - Status: APIError{ + resp := APIResponse{ + Status: APIStatus{ Code: http.StatusUnprocessableEntity, Msg: "Invalid Recipe"}, - Data: make([]APIDataRecipe, 0), } if err := json.NewEncoder(w).Encode(resp); err != nil { panic(err) @@ -239,14 +220,12 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { err = UpdateRecipeDB(recipe, db) if err != nil { fmt.Println(err) - resp := APIResponseItem{ - Status: APIError{Code: http.StatusBadRequest, + resp := APIResponse{ + Status: APIStatus{Code: http.StatusBadRequest, Msg: "Recipe could not be updated"}, - Data: make([]APIDataRecipe, 0), + Data: recipe, } - resp.Data = append(resp.Data, APIDataRecipe{recipe}) - w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusBadRequest) @@ -256,14 +235,12 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { return } - resp := APIResponseItem{ - Status: APIError{Code: http.StatusCreated, + resp := APIResponse{ + Status: APIStatus{Code: http.StatusCreated, Msg: "Recipe added successfully"}, - Data: make([]APIDataRecipe, 0), + Data: recipe, } - resp.Data = append(resp.Data, APIDataRecipe{recipe}) - w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusCreated) if err := json.NewEncoder(w).Encode(resp); err != nil { @@ -289,9 +266,8 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { msg = "Recipe Deleted Successfully" } - resp := APIResponseItem{ - Status: APIError{Code: status, Msg: msg}, - Data: make([]APIDataRecipe, 0), + resp := APIResponse{ + Status: APIStatus{Code: status, Msg: msg}, } w.Header().Set("Content-Type", @@ -304,8 +280,8 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { return } - resp := APIResponseItem{ - Status: APIError{Code: http.StatusMethodNotAllowed, + resp := APIResponse{ + Status: APIStatus{Code: http.StatusMethodNotAllowed, Msg: "Invalid method"}, Data: nil, } diff --git a/backend/todo.txt b/backend/todo.txt index cb074a4..b2d928b 100644 --- a/backend/todo.txt +++ b/backend/todo.txt @@ -1 +1,2 @@ -Add Error responses for incorrect methods +Refactor Response creation +Handle PSV parsing (rm empty string at end, or rm last pipe when creating item) -- cgit v1.1