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"
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2017-11-18 10:37:29 +00:00
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
)
|
|
|
|
|
2018-03-13 22:06:16 +00:00
|
|
|
// All renders an index of all companies.
|
2019-06-01 04:55:49 +00:00
|
|
|
func All(ctx aero.Context) error {
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2017-11-18 10:37:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|