diff options
author | schencej <55326070+schencej@users.noreply.github.com> | 2019-12-02 16:24:12 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-02 16:24:12 -0500 |
commit | 6f14fcb0a4692c54b76ea96fc2bc0a683af30838 (patch) | |
tree | 75f4f1227296a775e0dffb0dab920b1645dd0791 | |
parent | 7f253d927fc9a0469791a5d4684904acb24c00c5 (diff) | |
parent | 30ccc1f1c4a0ba64e16a0ef306f5b4c71df5760f (diff) |
Merge pull request #9 from tuckerevans/backend-improvement
Backend improvement (Refactoring)
-rw-r--r-- | backend/main.go | 218 | ||||
-rw-r--r-- | backend/todo.txt | 1 |
2 files changed, 60 insertions, 159 deletions
diff --git a/backend/main.go b/backend/main.go index 96bb824..0bd5ebf 100644 --- a/backend/main.go +++ b/backend/main.go @@ -20,6 +20,28 @@ type APIResponse struct { Data interface{} } +func MakeAPIResponse(status int, msg string, data interface{}) *APIResponse { + return &APIResponse{ + Status: APIStatus{ + Code: status, + Msg: msg, + }, + Data: data, + } +} + +func sendResponse(w http.ResponseWriter, code int, msg string, data interface{}) { + w.Header().Set("Access-Control-Allow-Origin", "*") //Enable CORS + w.Header().Set("Content-Type", + "application/json; charset=UTF-8") + + w.WriteHeader(code) + + resp := MakeAPIResponse(code, msg, data) + + if err := json.NewEncoder(w).Encode(resp); err != nil { + panic(err) + } } func RecipeList(w http.ResponseWriter, r *http.Request) { @@ -37,18 +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) - } - return } else if r.Method == "POST" { var recipe *Recipe @@ -65,103 +77,46 @@ 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) - } - - return - } + sendResponse(w, http.StatusCreated, "Recipe added successfully", + recipe) - resp := APIResponse{ - Status: APIStatus{Code: http.StatusMethodNotAllowed, - Msg: "Invalid method"}, - Data: nil, - } + } else { + 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) } } func SingleRecipe(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") //Enable CORS + recipe_id, err := strconv.Atoi(r.URL.Path[len("/recipes/"):]) if err != nil { fmt.Println("Not a valid ID") 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 + sendResponse(w, http.StatusOK, "Successful", 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) - } - - return } else if r.Method == "POST" { var status int row := db.QueryRow(`SELECT id FROM recipes WHERE id = $1`, @@ -174,18 +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) - } - return + sendResponse(w, status, "Cannot add to specific resource", + nil) } else if r.Method == "PUT" { var recipe *Recipe @@ -202,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 } @@ -220,34 +158,15 @@ 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) - return } else if r.Method == "DELETE" { res, err := db.Exec(`DELETE FROM recipes where id = $1`, @@ -256,41 +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" + sendResponse(w, http.StatusOK, + "Recipe Deleted Successfully", nil) } - 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) - } - - return - } - - 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) + } else { + sendResponse(w, http.StatusMethodNotAllowed, "Invalid method", + nil) } } @@ -311,8 +206,13 @@ func main() { dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", DB_USER, DB_PASSWORD, DB_NAME) db, err = sql.Open("postgres", dbinfo) - if err != nil || db.Ping() != nil { - fmt.Println("Error connecting to database") + if err != nil { + panic(err) + } + + err = db.Ping() + if err != nil { + panic(err) } http.HandleFunc("/recipes", RecipeList) diff --git a/backend/todo.txt b/backend/todo.txt index b2d928b..ae57ec0 100644 --- a/backend/todo.txt +++ b/backend/todo.txt @@ -1,2 +1,3 @@ Refactor Response creation Handle PSV parsing (rm empty string at end, or rm last pipe when creating item) +Fix Update (steps != 0) |