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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Repo) CategoryLabels(limit, offset int) (results []CategoryLabel) {
|
|
|
|
q := s.db.NewScope(nil).DB()
|
|
|
|
|
|
|
|
q = q.Table("categories").
|
|
|
|
Select("label_name AS name").
|
|
|
|
Joins("JOIN labels l ON categories.category_id = l.id").
|
|
|
|
Group("label_name").
|
|
|
|
Limit(limit).Offset(offset)
|
|
|
|
|
|
|
|
if err := q.Scan(&results).Error; err != nil {
|
|
|
|
log.Errorf("categories: %s", err.Error())
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, l := range results {
|
|
|
|
results[i].Title = strings.Title(l.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|