First attempt at splitting the Go code into seperate files to clean things up.

This commit is contained in:
2021-09-08 22:51:00 -05:00
parent bb01c11859
commit 20a6365159
3 changed files with 66 additions and 62 deletions
+40
View File
@@ -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
}
+21
View File
@@ -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
}
+5 -62
View File
@@ -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