From 44959af2161613ba91b85d2c1868dd91f1e0b636 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 8 Sep 2021 13:43:08 -0500 Subject: [PATCH] Added support for inserting a new category. --- cmd/inv/main.go | 85 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/cmd/inv/main.go b/cmd/inv/main.go index e672074..800ae56 100644 --- a/cmd/inv/main.go +++ b/cmd/inv/main.go @@ -4,6 +4,7 @@ import ( "database/sql" "encoding/json" "fmt" + "io/ioutil" "net/http" "time" @@ -37,7 +38,7 @@ type Subcategory struct { } func main() { - http.HandleFunc("/data", get) + http.HandleFunc("/categories", categories) err := http.ListenAndServe(":8080", nil) if err != nil { @@ -46,31 +47,22 @@ func main() { } -func get(w http.ResponseWriter, r *http.Request) { - c := sql_test() - - // if c.Id == 0 { - // w.WriteHeader(http.StatusBadGateway) - // w.Write([]byte("Failed")) - // return - // } - - jsonBytes, err := json.Marshal(c) - - if err != nil { +func categories(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + getCategories(w, r) + case "POST": + insertCategory(w, r) + default: w.WriteHeader(http.StatusMethodNotAllowed) - w.Write([]byte("Failed 3")) - return + w.Write([]byte("Method not allowed")) } - - w.WriteHeader(http.StatusOK) - w.Write(jsonBytes) } -func sql_test() []Category { +func getCategories(w http.ResponseWriter, r *http.Request) { //Note: The postgres driver seems to get confused when no password is supplied, so omit it in the connection sting. //https://rajyavardhan.medium.com/when-you-get-relation-does-not-exist-in-postgres-7ffb0c3c674b - psqlInfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", "localhost", 5432, "postgres", "inventory") + psqlInfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", "localhost", 5432, "postgres", "inv") db, err := sql.Open("postgres", psqlInfo) if err != nil { @@ -99,5 +91,56 @@ func sql_test() []Category { categories = append(categories, category) } - return categories + jsonBytes, err := json.Marshal(categories) + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Failed to get a list of categories.")) + return + } + + w.WriteHeader(http.StatusOK) + w.Write(jsonBytes) +} + +func insertCategory(w http.ResponseWriter, r *http.Request) { + bodyBytes, err := ioutil.ReadAll(r.Body) + + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Failed to parse request body")) + return + } + + var category Category + + err = json.Unmarshal(bodyBytes, &category) + + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Failed to unmarshal JSON")) + return + } + + psqlInfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", "localhost", 5432, "postgres", "inv") + db, err := sql.Open("postgres", psqlInfo) + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Failed to connect to the DB")) + return + } + + id := 0 + + err = db.QueryRow("INSERT INTO category (category_name) VALUES ($1) RETURNING category_id", category.Name).Scan(&id) + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.WriteHeader(http.StatusOK) + w.Write([]byte(fmt.Sprint("New Record: ", id))) }