2017-10-03 13:26:29 +00:00
|
|
|
package shop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-10-04 11:39:59 +00:00
|
|
|
"sort"
|
2017-10-03 13:26:29 +00:00
|
|
|
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2017-10-04 06:12:12 +00:00
|
|
|
|
2017-10-03 13:26:29 +00:00
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get shop page.
|
2019-06-01 04:55:49 +00:00
|
|
|
func Get(ctx aero.Context) error {
|
2017-10-03 13:26:29 +00:00
|
|
|
user := utils.GetUser(ctx)
|
|
|
|
|
|
|
|
if user == nil {
|
2018-07-07 03:42:00 +00:00
|
|
|
return ctx.Error(http.StatusUnauthorized, "Not logged in")
|
2017-10-03 13:26:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 23:59:14 +00:00
|
|
|
items, err := arn.AllShopItems()
|
2017-10-04 11:39:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.StatusInternalServerError, "Error fetching shop item data", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(items, func(i, j int) bool {
|
|
|
|
return items[i].Order < items[j].Order
|
|
|
|
})
|
2017-10-04 06:12:12 +00:00
|
|
|
|
2018-04-08 20:10:45 +00:00
|
|
|
// Calculate popularity of items
|
|
|
|
itemPopularity := map[string]int{}
|
|
|
|
|
|
|
|
for purchase := range arn.StreamPurchases() {
|
|
|
|
itemPopularity[purchase.ItemID]++
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.HTML(components.Shop(items, itemPopularity, user))
|
2017-10-03 13:26:29 +00:00
|
|
|
}
|