49 lines
1.1 KiB
Go
Raw Normal View History

2017-11-18 10:37:29 +00:00
package companies
import (
2017-11-19 01:04:37 +00:00
"sort"
2017-11-29 14:26:11 +00:00
"strings"
2018-03-01 21:44:14 +00:00
"unicode"
2017-11-19 01:04:37 +00:00
2017-11-18 10:37:29 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
2018-03-13 22:06:16 +00:00
// All renders an index of all companies.
2017-12-02 20:53:01 +00:00
func All(ctx *aero.Context) string {
2017-11-18 10:37:29 +00:00
user := utils.GetUser(ctx)
companies := arn.FilterCompanies(func(company *arn.Company) bool {
return !company.IsDraft
})
2017-11-19 01:04:37 +00:00
sort.Slice(companies, func(i, j int) bool {
2017-11-29 14:26:11 +00:00
return strings.ToLower(companies[i].Name.English) < strings.ToLower(companies[j].Name.English)
2017-11-19 01:04:37 +00:00
})
2017-11-29 14:26:11 +00:00
groups := [][]*arn.Company{}
currentGroupIndex := -1
previousFirstLetter := ""
for _, company := range companies {
firstLetter := strings.ToLower(company.Name.English[:1])
2018-03-01 21:44:14 +00:00
if !unicode.IsLetter([]rune(firstLetter)[0]) {
continue
}
2017-11-29 14:26:11 +00:00
if firstLetter != previousFirstLetter {
groups = append(groups, []*arn.Company{})
currentGroupIndex++
previousFirstLetter = firstLetter
}
groups[currentGroupIndex] = append(groups[currentGroupIndex], company)
2017-11-18 10:37:29 +00:00
}
2018-03-13 22:06:16 +00:00
return ctx.HTML(components.CompaniesIndex(groups, user))
2017-11-18 10:37:29 +00:00
}