From 20a63651591763b10aee2d05e0a314bf1f518423 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 8 Sep 2021 22:51:00 -0500 Subject: [PATCH] First attempt at splitting the Go code into seperate files to clean things up. --- category.go | 40 +++++++++++++++++++++++ database.go | 21 ++++++++++++ cmd/inv/main.go => main.go | 67 +++----------------------------------- 3 files changed, 66 insertions(+), 62 deletions(-) create mode 100644 category.go create mode 100644 database.go rename cmd/inv/main.go => main.go (70%) diff --git a/category.go b/category.go new file mode 100644 index 0000000..2f5a76e --- /dev/null +++ b/category.go @@ -0,0 +1,40 @@ +package main + +type Category struct { + Id int32 `json:"id"` + Name string `json:"name"` +} + +type Subcategory struct { + Id int32 `json:"id"` + Name string `json:"name"` + CategoryId int32 `json:"categoryId"` +} + +func getAllCategories() []Category { + db := getSQLConnection() + + defer db.Close() + + rows, err := db.Query("SELECT * FROM category;") + + if err != nil { + panic(err) + } + + defer rows.Close() + + var categories []Category + + for rows.Next() { + var category Category + + if err = rows.Scan(&category.Id, &category.Name); err != nil { + println(err.Error()) + } + + categories = append(categories, category) + } + + return categories +} diff --git a/database.go b/database.go new file mode 100644 index 0000000..671fb7d --- /dev/null +++ b/database.go @@ -0,0 +1,21 @@ +package main + +import ( + "database/sql" + "fmt" + + _ "github.com/lib/pq" +) + +func getSQLConnection() *sql.DB { + //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") + db, err := sql.Open("postgres", psqlInfo) + + if err != nil { + panic(err) + } + + return db +} diff --git a/cmd/inv/main.go b/main.go similarity index 70% rename from cmd/inv/main.go rename to main.go index dda7e23..0628953 100644 --- a/cmd/inv/main.go +++ b/main.go @@ -8,8 +8,6 @@ import ( "net/http" "strconv" "time" - - _ "github.com/lib/pq" ) //Go Best practices: https://github.com/golovers/effective-go @@ -27,17 +25,6 @@ type Item struct { Subcategory Subcategory `json:"subcategory"` } -type Category struct { - Id int32 `json:"id"` - Name string `json:"name"` -} - -type Subcategory struct { - Id int32 `json:"id"` - Name string `json:"name"` - CategoryId int32 `json:"categoryId"` -} - func main() { http.HandleFunc("/categories", categories) http.HandleFunc("/category", category) @@ -84,12 +71,7 @@ func getCategory(w http.ResponseWriter, r *http.Request) { 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 { - panic(err) - } + db := getSQLConnection() defer db.Close() @@ -128,12 +110,7 @@ func deleteCategory(w http.ResponseWriter, r *http.Request) { 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 { - panic(err) - } + db := getSQLConnection() defer db.Close() @@ -150,36 +127,7 @@ func deleteCategory(w http.ResponseWriter, r *http.Request) { } 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", "inv") - db, err := sql.Open("postgres", psqlInfo) - - if err != nil { - panic(err) - } - - defer db.Close() - - rows, err := db.Query("SELECT * FROM category;") - - if err != nil { - panic(err) - } - - defer rows.Close() - - var categories []Category - - for rows.Next() { - var category Category - - if err = rows.Scan(&category.Id, &category.Name); err != nil { - println(err.Error()) - } - - categories = append(categories, category) - } + categories := getAllCategories() jsonBytes, err := json.Marshal(categories) @@ -222,14 +170,9 @@ func insertCategory(w http.ResponseWriter, r *http.Request) { return } - psqlInfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", "localhost", 5432, "postgres", "inv") - db, err := sql.Open("postgres", psqlInfo) + db := getSQLConnection() - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - return - } + defer db.Close() id := 0