Category data now includes their subcategories. Categories can now be requested by ID in a prettier way.
This commit is contained in:
+110
-10
@@ -1,17 +1,18 @@
|
||||
package main
|
||||
|
||||
type Category struct {
|
||||
Id int32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Subcategories []Subcategory `json:"subcategories"`
|
||||
}
|
||||
|
||||
type Subcategory struct {
|
||||
Id int32 `json:"id"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CategoryId int32 `json:"categoryId"`
|
||||
CategoryId int64 `json:"categoryId"`
|
||||
}
|
||||
|
||||
func getAllCategories() []Category {
|
||||
func getAllCategories() (categories []Category, ok bool) {
|
||||
db := getSQLConnection()
|
||||
|
||||
defer db.Close()
|
||||
@@ -19,22 +20,121 @@ func getAllCategories() []Category {
|
||||
rows, err := db.Query("SELECT * FROM category;")
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
//TODO: log this error
|
||||
return nil, false
|
||||
//panic(err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
var categories []Category
|
||||
|
||||
for rows.Next() {
|
||||
var category Category
|
||||
|
||||
if err = rows.Scan(&category.Id, &category.Name); err != nil {
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user