2020-01-05 14:18:40 +01:00
|
|
|
package query
|
2019-12-28 20:24:20 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CategoryLabel struct {
|
2019-12-28 20:39:51 +01:00
|
|
|
Name string
|
2019-12-28 20:24:20 +01:00
|
|
|
Title string
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
func CategoryLabels(limit, offset int) (results []CategoryLabel) {
|
|
|
|
s := Db().NewScope(nil).DB()
|
2019-12-28 20:24:20 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Table("categories").
|
2019-12-28 20:24:20 +01:00
|
|
|
Select("label_name AS name").
|
|
|
|
Joins("JOIN labels l ON categories.category_id = l.id").
|
|
|
|
Group("label_name").
|
|
|
|
Limit(limit).Offset(offset)
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if err := s.Scan(&results).Error; err != nil {
|
2019-12-28 20:24:20 +01:00
|
|
|
log.Errorf("categories: %s", err.Error())
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, l := range results {
|
|
|
|
results[i].Title = strings.Title(l.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|