Added support for inserting a new category.
This commit is contained in:
+64
-21
@@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -37,7 +38,7 @@ type Subcategory struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/data", get)
|
http.HandleFunc("/categories", categories)
|
||||||
err := http.ListenAndServe(":8080", nil)
|
err := http.ListenAndServe(":8080", nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -46,31 +47,22 @@ func main() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func get(w http.ResponseWriter, r *http.Request) {
|
func categories(w http.ResponseWriter, r *http.Request) {
|
||||||
c := sql_test()
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
// if c.Id == 0 {
|
getCategories(w, r)
|
||||||
// w.WriteHeader(http.StatusBadGateway)
|
case "POST":
|
||||||
// w.Write([]byte("Failed"))
|
insertCategory(w, r)
|
||||||
// return
|
default:
|
||||||
// }
|
|
||||||
|
|
||||||
jsonBytes, err := json.Marshal(c)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
w.Write([]byte("Failed 3"))
|
w.Write([]byte("Method not allowed"))
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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.
|
//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
|
//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)
|
db, err := sql.Open("postgres", psqlInfo)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -99,5 +91,56 @@ func sql_test() []Category {
|
|||||||
categories = append(categories, 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)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user