First attempt at splitting the Go code into seperate files to clean things up.
This commit is contained in:
+40
@@ -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
@@ -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
|
||||||
|
}
|
||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//Go Best practices: https://github.com/golovers/effective-go
|
//Go Best practices: https://github.com/golovers/effective-go
|
||||||
@@ -27,17 +25,6 @@ type Item struct {
|
|||||||
Subcategory Subcategory `json:"subcategory"`
|
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() {
|
func main() {
|
||||||
http.HandleFunc("/categories", categories)
|
http.HandleFunc("/categories", categories)
|
||||||
http.HandleFunc("/category", category)
|
http.HandleFunc("/category", category)
|
||||||
@@ -84,12 +71,7 @@ func getCategory(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", "localhost", 5432, "postgres", "inv")
|
db := getSQLConnection()
|
||||||
db, err := sql.Open("postgres", psqlInfo)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
@@ -128,12 +110,7 @@ func deleteCategory(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", "localhost", 5432, "postgres", "inv")
|
db := getSQLConnection()
|
||||||
db, err := sql.Open("postgres", psqlInfo)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
@@ -150,36 +127,7 @@ func deleteCategory(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getCategories(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.
|
categories := getAllCategories()
|
||||||
//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)
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonBytes, err := json.Marshal(categories)
|
jsonBytes, err := json.Marshal(categories)
|
||||||
|
|
||||||
@@ -222,14 +170,9 @@ func insertCategory(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", "localhost", 5432, "postgres", "inv")
|
db := getSQLConnection()
|
||||||
db, err := sql.Open("postgres", psqlInfo)
|
|
||||||
|
|
||||||
if err != nil {
|
defer db.Close()
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
id := 0
|
id := 0
|
||||||
|
|
||||||
Reference in New Issue
Block a user