Post-Migrator/services/slowtwitch/get-category.go

46 lines
920 B
Go
Raw Normal View History

2024-05-14 21:59:04 +00:00
package slowtwitch
import (
"database/sql"
"fmt"
"sort"
2024-05-14 21:59:04 +00:00
)
type SlowtwitchCategory struct {
Id int
Name string
FullName string
FatherId int
CatDepth int
NumberOfLinks int
}
func GetCategories(db *sql.DB) []SlowtwitchCategory {
rows, err := db.Query("select ID, Name, Full_Name, FatherID, CatDepth, Number_of_Links from glinks_Category where Number_of_Links > 0;")
2024-05-14 21:59:04 +00:00
if err != nil {
fmt.Println(err)
}
defer rows.Close()
var categories []SlowtwitchCategory
for rows.Next() {
category := SlowtwitchCategory{}
err := rows.Scan(&category.Id, &category.Name, &category.FullName, &category.FatherId, &category.CatDepth, &category.NumberOfLinks)
if err != nil {
fmt.Println(err)
}
categories = append(categories, category)
}
sort.Slice(categories, func(i, j int) bool {
return categories[i].CatDepth < categories[j].CatDepth
})
2024-05-14 21:59:04 +00:00
return categories
}