summaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-11-29 17:58:41 -0500
committerTucker Evans <tuckerevans24@gmail.com>2019-11-29 17:58:41 -0500
commit9e1ab63bb039b72cdd2da98c3814366ec1851c7a (patch)
treed894d701b8df345c62e73c1b114378933552ec3c /backend
parent950e9f1bd29ffdc82740e9ac237089eb61a25661 (diff)
Fix duplicated response code
Replace response setup/send code w/ calls to sendResponse()
Diffstat (limited to 'backend')
-rw-r--r--backend/main.go175
1 files changed, 26 insertions, 149 deletions
diff --git a/backend/main.go b/backend/main.go
index 5b0e8e7..5ec4ce9 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -59,17 +59,8 @@ func RecipeList(w http.ResponseWriter, r *http.Request) {
}
}
- resp := APIResponse{
- Status: APIStatus{Code: 200, Msg: "Successful Request"},
- Data: ids,
- }
+ sendResponse(w, http.StatusOK, "Successful Request", ids)
- w.Header().Set("Content-Type",
- "application/json; charset=UTF-8")
- w.WriteHeader(http.StatusOK)
- if err := json.NewEncoder(w).Encode(resp); err != nil {
- panic(err)
- }
} else if r.Method == "POST" {
var recipe *Recipe
@@ -86,65 +77,26 @@ func RecipeList(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(body, &recipe)
if err != nil {
fmt.Println(err)
- w.WriteHeader(http.StatusUnprocessableEntity)
- w.Header().Set("Content-Type",
- "application/json; charset=UTF-8")
- resp := APIResponse{
- Status: APIStatus{
- Code: http.StatusUnprocessableEntity,
- Msg: "Invalid Recipe"},
- }
-
- err := json.NewEncoder(w).Encode(resp)
- if err != nil {
- panic(err)
- }
+ sendResponse(w, http.StatusUnprocessableEntity,
+ "Invalid Recipe", nil)
return
}
err = AddRecipeDB(recipe, db)
if err != nil {
fmt.Println(err)
- resp := APIResponse{
- Status: APIStatus{Code: http.StatusBadRequest,
- Msg: "Recipe could not be added"},
- Data: recipe,
- }
-
- w.Header().Set("Content-Type",
- "application/json; charset=UTF-8")
- w.WriteHeader(http.StatusBadRequest)
- if err := json.NewEncoder(w).Encode(resp); err != nil {
- panic(err)
- }
+ sendResponse(w, http.StatusBadRequest,
+ "Recipe could not be added", recipe)
return
}
- resp := APIResponse{
- Status: APIStatus{Code: http.StatusCreated,
- Msg: "Recipe added successfully"},
- Data: recipe,
- }
-
- w.Header().Set("Content-Type", "application/json; charset=UTF-8")
- w.WriteHeader(http.StatusCreated)
- if err := json.NewEncoder(w).Encode(resp); err != nil {
- panic(err)
- }
+ sendResponse(w, http.StatusCreated, "Recipe added successfully",
+ recipe)
} else {
- resp := APIResponse{
- Status: APIStatus{Code: http.StatusMethodNotAllowed,
- Msg: "Invalid method"},
- Data: nil,
- }
+ sendResponse(w, http.StatusMethodNotAllowed, "Invalid method",
+ 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)
- }
}
}
@@ -157,30 +109,12 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) {
return
}
if r.Method == "GET" {
- var status int
- var msg string
-
recipe := RecipeFromId(recipe_id, db)
if recipe == nil {
- status = http.StatusNotFound
- msg = "Recipe not Found"
+ sendResponse(w, http.StatusNotFound, "Recipe not Found",
+ nil)
} else {
- status = http.StatusOK
- msg = "Successful"
- }
-
- resp := APIResponse{
- Status: APIStatus{Code: status, Msg: msg},
- }
-
- if status == http.StatusOK {
- resp.Data = recipe
- }
-
- w.Header().Set("Content-Type", "application/json; charset=UTF-8")
- w.WriteHeader(http.StatusOK)
- if err := json.NewEncoder(w).Encode(resp); err != nil {
- panic(err)
+ sendResponse(w, http.StatusOK, "Successful", recipe)
}
} else if r.Method == "POST" {
@@ -195,17 +129,9 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) {
} else {
status = http.StatusConflict
}
- resp := APIResponse{
- Status: APIStatus{Code: status, Msg: "Cannot add to specific resource"},
- Data: nil,
- }
- w.Header().Set("Content-Type",
- "application/json; charset=UTF-8")
- w.WriteHeader(status)
- if err := json.NewEncoder(w).Encode(resp); err != nil {
- panic(err)
- }
+ sendResponse(w, status, "Cannot add to specific resource",
+ nil)
} else if r.Method == "PUT" {
var recipe *Recipe
@@ -222,16 +148,8 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(body, &recipe)
if err != nil {
fmt.Println(err)
- w.WriteHeader(http.StatusUnprocessableEntity)
- w.Header().Set("Content-Type", "application/json; charset=UTF-8")
- resp := APIResponse{
- Status: APIStatus{
- Code: http.StatusUnprocessableEntity,
- Msg: "Invalid Recipe"},
- }
- if err := json.NewEncoder(w).Encode(resp); err != nil {
- panic(err)
- }
+ sendResponse(w, http.StatusUnprocessableEntity,
+ "Invalid Recipe", nil)
return
}
@@ -240,32 +158,14 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) {
err = UpdateRecipeDB(recipe, db)
if err != nil {
fmt.Println(err)
- resp := APIResponse{
- Status: APIStatus{Code: http.StatusBadRequest,
- Msg: "Recipe could not be updated"},
- Data: recipe,
- }
+ sendResponse(w, http.StatusBadRequest,
+ "Recipe could not be updated", recipe)
- w.Header().Set("Content-Type",
- "application/json; charset=UTF-8")
- w.WriteHeader(http.StatusBadRequest)
- if err := json.NewEncoder(w).Encode(resp); err != nil {
- panic(err)
- }
return
}
- resp := APIResponse{
- Status: APIStatus{Code: http.StatusCreated,
- Msg: "Recipe added successfully"},
- Data: recipe,
- }
-
- w.Header().Set("Content-Type", "application/json; charset=UTF-8")
- w.WriteHeader(http.StatusCreated)
- if err := json.NewEncoder(w).Encode(resp); err != nil {
- panic(err)
- }
+ sendResponse(w, http.StatusCreated, "Recipe added successfully",
+ recipe)
} else if r.Method == "DELETE" {
@@ -275,40 +175,17 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) {
panic(err)
}
- var status int
- var msg string
if ra, _ := res.RowsAffected(); ra == 0 {
- status = http.StatusNotFound
- msg = "Recipe Not found"
+ sendResponse(w, http.StatusNotFound, "Recipe Not found",
+ nil)
} else {
- status = http.StatusOK
- msg = "Recipe Deleted Successfully"
- }
-
- resp := APIResponse{
- Status: APIStatus{Code: status, Msg: msg},
- }
-
- w.Header().Set("Content-Type",
- "application/json; charset=UTF-8")
- w.WriteHeader(http.StatusOK)
- if err := json.NewEncoder(w).Encode(resp); err != nil {
- panic(err)
+ sendResponse(w, http.StatusOK,
+ "Recipe Deleted Successfully", 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)
- }
+ sendResponse(w, http.StatusMethodNotAllowed, "Invalid method",
+ nil)
}
}