95 lines
1.9 KiB
Go
Raw Normal View History

2017-10-21 17:16:11 +00:00
package compare
import (
"net/http"
"sort"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
"github.com/aerogo/aero"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2017-10-21 17:16:11 +00:00
)
// AnimeList ...
2019-06-01 04:55:49 +00:00
func AnimeList(ctx aero.Context) error {
2019-11-17 07:59:34 +00:00
user := arn.GetUserFromContext(ctx)
2017-10-21 17:16:11 +00:00
nickA := ctx.Get("nick-1")
nickB := ctx.Get("nick-2")
a, err := arn.GetUserByNick(nickA)
if err != nil || a == nil {
return ctx.Error(http.StatusNotFound, "User not found: "+nickA, err)
}
b, err := arn.GetUserByNick(nickB)
if err != nil || b == nil {
return ctx.Error(http.StatusNotFound, "User not found: "+nickB, err)
}
comparisons := []*utils.Comparison{}
2017-10-21 17:23:40 +00:00
countA := 0
countB := 0
2017-10-21 17:16:11 +00:00
2017-12-04 21:23:31 +00:00
animeListA := a.AnimeList()
animeListB := b.AnimeList()
for _, item := range animeListA.Items {
2017-10-21 17:16:11 +00:00
if item.Status == arn.AnimeListStatusPlanned {
continue
}
2017-10-21 17:23:40 +00:00
countA++
2017-10-21 17:16:11 +00:00
comparisons = append(comparisons, &utils.Comparison{
Anime: item.Anime(),
ItemA: item,
2017-12-04 21:23:31 +00:00
ItemB: animeListB.Find(item.AnimeID),
2017-10-21 17:16:11 +00:00
})
}
2017-12-04 21:23:31 +00:00
for _, item := range animeListB.Items {
2017-10-21 17:23:40 +00:00
if item.Status == arn.AnimeListStatusPlanned {
continue
}
countB++
if Contains(comparisons, item.AnimeID) {
2017-10-21 17:16:11 +00:00
continue
}
comparisons = append(comparisons, &utils.Comparison{
Anime: item.Anime(),
2017-12-04 21:23:31 +00:00
ItemA: animeListA.Find(item.AnimeID),
2017-10-21 17:16:11 +00:00
ItemB: item,
})
}
sort.Slice(comparisons, func(i, j int) bool {
aPopularity := comparisons[i].Anime.Popularity.Total()
bPopularity := comparisons[j].Anime.Popularity.Total()
if aPopularity == bPopularity {
return comparisons[i].Anime.Title.Canonical < comparisons[j].Anime.Title.Canonical
}
return aPopularity > bPopularity
2017-10-21 17:16:11 +00:00
})
2017-10-21 17:23:40 +00:00
return ctx.HTML(components.CompareAnimeList(a, b, countA, countB, comparisons, user))
2017-10-21 17:16:11 +00:00
}
// Contains ...
func Contains(comparisons []*utils.Comparison, animeID string) bool {
for _, comparison := range comparisons {
if comparison.Anime.ID == animeID {
return true
}
}
return false
}