141 lines
2.9 KiB
Go
141 lines
2.9 KiB
Go
package main
|
|
|
|
type Category struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Subcategories []Subcategory `json:"subcategories"`
|
|
}
|
|
|
|
type Subcategory struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
CategoryId int64 `json:"categoryId"`
|
|
}
|
|
|
|
func getAllCategories() (categories []Category, ok bool) {
|
|
db := getSQLConnection()
|
|
|
|
defer db.Close()
|
|
|
|
rows, err := db.Query("SELECT * FROM category;")
|
|
|
|
if err != nil {
|
|
//TODO: log this error
|
|
return nil, false
|
|
//panic(err)
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var category Category
|
|
|
|
if err = rows.Scan(&category.ID, &category.Name); err != nil {
|
|
println(err.Error())
|
|
}
|
|
|
|
subcategories, ok := getSubcategories(category.ID)
|
|
|
|
if ok {
|
|
category.Subcategories = subcategories
|
|
}
|
|
|
|
categories = append(categories, category)
|
|
}
|
|
|
|
return categories, true
|
|
}
|
|
|
|
func insertNewCategory(name string) (recordID int32, ok bool) {
|
|
db := getSQLConnection()
|
|
|
|
defer db.Close()
|
|
|
|
err := db.QueryRow("INSERT INTO category (category_name) VALUES ($1) RETURNING category_id;", name).Scan(&recordID)
|
|
|
|
if err != nil {
|
|
//TODO: log this error
|
|
println(err.Error())
|
|
return 0, false
|
|
//panic(err)
|
|
}
|
|
|
|
return recordID, true
|
|
}
|
|
|
|
func deleteCategory(id int64) bool {
|
|
db := getSQLConnection()
|
|
|
|
defer db.Close()
|
|
|
|
_, err := db.Exec("DELETE FROM category WHERE category_id = $1;", id)
|
|
//TODO: log this if an error happened
|
|
if err != nil {
|
|
println(err.Error())
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func getCategoryByID(id int64) (category Category, ok bool) {
|
|
db := getSQLConnection()
|
|
|
|
defer db.Close()
|
|
|
|
if err := db.QueryRow("SELECT * FROM category WHERE category_id = $1;", id).Scan(&category.ID, &category.Name); err != nil {
|
|
return category, false
|
|
}
|
|
|
|
subcategories, ok := getSubcategories(category.ID)
|
|
|
|
if !ok {
|
|
//TODO: do something about not getting the subcategories, for now ignore.
|
|
return category, true
|
|
}
|
|
|
|
category.Subcategories = subcategories
|
|
|
|
return category, true
|
|
}
|
|
|
|
// func getSubcategoryByID(id int64) (subcategory Subcategory, ok bool) {
|
|
// db := getSQLConnection()
|
|
|
|
// defer db.Close()
|
|
|
|
// if err := db.QueryRow("SELECT * FROM subcategory WHERE subcategory_id = $1;", id).Scan(&subcategory.ID, &subcategory.Name, &subcategory.CategoryId); err != nil {
|
|
// return subcategory, false
|
|
// }
|
|
|
|
// return subcategory, true
|
|
// }
|
|
|
|
func getSubcategories(categoryID int64) (subcategories []Subcategory, ok bool) {
|
|
db := getSQLConnection()
|
|
|
|
defer db.Close()
|
|
|
|
rows, err := db.Query("SELECT * FROM subcategory WHERE category_id = $1;", categoryID)
|
|
|
|
if err != nil {
|
|
//TODO: log this error
|
|
return nil, false
|
|
//panic(err)
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var subcategory Subcategory
|
|
|
|
if err = rows.Scan(&subcategory.ID, &subcategory.Name, &subcategory.CategoryId); err != nil {
|
|
println(err.Error())
|
|
}
|
|
|
|
subcategories = append(subcategories, subcategory)
|
|
}
|
|
|
|
return subcategories, true
|
|
}
|