diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-11-28 07:35:18 -0500 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2019-11-28 07:38:06 -0500 |
commit | 510f3d83f0da041e90d358c796eb0c209b265f30 (patch) | |
tree | 6e69ac8cca5432b59be146129a8becc7ea2c7a6f | |
parent | b402ebba4f9c93a0040972748d66f9c4c6b02eb7 (diff) |
Fix default case for http method check
Moves default case (Method Not Allowed) into else clause so it is not
necessary to return from the previous if's --- only for checking http
method, error checking if statements can/should be returning where
needed.
-rw-r--r-- | backend/main.go | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/backend/main.go b/backend/main.go index 0a95fc8..0d22b80 100644 --- a/backend/main.go +++ b/backend/main.go @@ -58,7 +58,6 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { if err := json.NewEncoder(w).Encode(resp); err != nil { panic(err) } - return } else if r.Method == "POST" { var recipe *Recipe @@ -121,20 +120,19 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { panic(err) } - return - } - - resp := APIResponse{ - Status: APIStatus{Code: http.StatusMethodNotAllowed, - Msg: "Invalid method"}, - Data: nil, - } + } else { + resp := APIResponse{ + Status: APIStatus{Code: http.StatusMethodNotAllowed, + Msg: "Invalid method"}, + Data: nil, + } - w.Header().Set("Content-Type", - "application/json; charset=UTF-8") - w.WriteHeader(http.StatusMethodNotAllowed) - if err := json.NewEncoder(w).Encode(resp); err != nil { - panic(err) + w.Header().Set("Content-Type", + "application/json; charset=UTF-8") + w.WriteHeader(http.StatusMethodNotAllowed) + if err := json.NewEncoder(w).Encode(resp); err != nil { + panic(err) + } } } @@ -173,7 +171,6 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { panic(err) } - return } else if r.Method == "POST" { var status int row := db.QueryRow(`SELECT id FROM recipes WHERE id = $1`, @@ -197,7 +194,6 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { if err := json.NewEncoder(w).Encode(resp); err != nil { panic(err) } - return } else if r.Method == "PUT" { var recipe *Recipe @@ -259,7 +255,6 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { panic(err) } - return } else if r.Method == "DELETE" { res, err := db.Exec(`DELETE FROM recipes where id = $1`, @@ -289,20 +284,19 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { panic(err) } - return - } - - resp := APIResponse{ - Status: APIStatus{Code: http.StatusMethodNotAllowed, - Msg: "Invalid method"}, - Data: nil, - } + } else { + resp := APIResponse{ + Status: APIStatus{Code: http.StatusMethodNotAllowed, + Msg: "Invalid method"}, + Data: nil, + } - w.Header().Set("Content-Type", - "application/json; charset=UTF-8") - w.WriteHeader(http.StatusMethodNotAllowed) - if err := json.NewEncoder(w).Encode(resp); err != nil { - panic(err) + w.Header().Set("Content-Type", + "application/json; charset=UTF-8") + w.WriteHeader(http.StatusMethodNotAllowed) + if err := json.NewEncoder(w).Encode(resp); err != nil { + panic(err) + } } } |