44 lines
1016 B
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"
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"
)
2017-12-02 20:53:01 +00:00
// All renders the companies page.
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])
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
}
2017-11-29 14:26:11 +00:00
return ctx.HTML(components.Companies(groups, user))
2017-11-18 10:37:29 +00:00
}