From 510f3d83f0da041e90d358c796eb0c209b265f30 Mon Sep 17 00:00:00 2001
From: Tucker Evans <tuckerevans24@gmail.com>
Date: Thu, 28 Nov 2019 07:35:18 -0500
Subject: 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.
---
 backend/main.go | 54 ++++++++++++++++++++++++------------------------------
 1 file changed, 24 insertions(+), 30 deletions(-)

(limited to 'backend')

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)
+		}
 	}
 }
 
-- 
cgit v1.1