2017-05-22 18:25:04 +02:00
package categories
import (
2017-07-06 21:53:13 +02:00
"sort"
2017-05-22 18:25:04 +02:00
"github.com/NyaaPantsu/nyaa/config"
)
2017-07-06 21:53:13 +02:00
// Category is a struct defining a category
type Category struct {
ID string
Name string
}
2017-05-22 18:25:04 +02:00
2017-07-06 21:53:13 +02:00
// Cateogories is a struct defining an array of categories
type Categories [ ] Category
var categories Categories
var Index map [ string ] int
2017-07-06 22:38:40 +02:00
func initCategories ( ) {
var cats map [ string ] string
if config . IsSukebei ( ) {
2017-07-10 14:11:05 +02:00
cats = config . Get ( ) . Torrents . SukebeiCategories
2017-07-06 22:38:40 +02:00
} else {
2017-07-10 14:11:05 +02:00
cats = config . Get ( ) . Torrents . CleanCategories
2017-07-06 22:38:40 +02:00
}
2017-07-06 21:53:13 +02:00
2017-07-06 22:38:40 +02:00
// Sorting categories alphabetically
var index [ ] string
ids := make ( map [ string ] string )
Index = make ( map [ string ] int , len ( cats ) )
for id , name := range cats {
index = append ( index , name )
ids [ name ] = id
}
sort . Strings ( index )
2017-05-22 18:25:04 +02:00
2017-07-06 22:38:40 +02:00
// Creating index of categories
for k , name := range index {
categories = append ( categories , Category { ids [ name ] , name } )
Index [ ids [ name ] ] = k
2017-05-22 18:25:04 +02:00
}
2017-07-06 21:53:13 +02:00
}
2017-05-22 18:25:04 +02:00
2017-07-06 21:53:13 +02:00
// All : function to get all categories depending on the actual website from config/categories.go
func All ( ) Categories {
2017-07-06 22:38:40 +02:00
if len ( categories ) == 0 {
initCategories ( )
}
2017-05-22 18:25:04 +02:00
return categories
}
2017-07-06 21:53:13 +02:00
// Get : function to get a category by the key in the index array
func Get ( key int ) Category {
2017-07-06 22:38:40 +02:00
return All ( ) [ key ]
2017-07-06 21:53:13 +02:00
}
// Get : function to get a category by the id of the category from the database
func GetByID ( id string ) ( Category , bool ) {
if key , ok := Index [ id ] ; ok {
2017-07-06 22:38:40 +02:00
return All ( ) [ key ] , true
2017-07-06 21:53:13 +02:00
}
return Category { "" , "" } , false
}
// Exists : Check if a category exist in config
2017-07-06 22:38:40 +02:00
func ( cats Categories ) Exists ( id string ) bool {
2017-07-06 21:53:13 +02:00
for _ , cat := range cats {
2017-07-06 22:38:40 +02:00
if cat . ID == id {
2017-07-06 21:53:13 +02:00
return true
}
}
return false
}
// Exists : Check if a category exist in config
func Exists ( category string ) bool {
2017-07-06 22:38:40 +02:00
return All ( ) . Exists ( category )
2017-05-22 18:25:04 +02:00
}
2017-05-23 04:05:33 +02:00
2017-07-06 21:53:13 +02:00
// GetSelect : Format categories in map ordered alphabetically
2017-07-06 22:19:44 +02:00
func GetSelect ( keepParent bool , keepChild bool ) Categories {
2017-07-06 22:38:40 +02:00
var catSelect Categories
for _ , v := range All ( ) {
2017-07-06 21:53:13 +02:00
if ( keepParent && keepChild ) || ( len ( v . ID ) > 2 && ! keepParent ) || ( len ( v . ID ) <= 2 && ! keepChild ) {
2017-07-06 22:38:40 +02:00
catSelect = append ( catSelect , v )
2017-05-23 04:05:33 +02:00
}
}
return catSelect
2017-05-24 09:11:13 +02:00
}