1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: top languages by user

This commit is contained in:
Ferdinand Mütsch 2022-10-05 21:52:10 +02:00
parent a27fe04919
commit 7a07c9d4fc
5 changed files with 35 additions and 20 deletions

View File

@ -3,14 +3,15 @@ package view
import "github.com/muety/wakapi/models" import "github.com/muety/wakapi/models"
type LeaderboardViewModel struct { type LeaderboardViewModel struct {
User *models.User User *models.User
By string By string
Key string Key string
Items []*models.LeaderboardItem Items []*models.LeaderboardItem
TopKeys []string TopKeys []string
ApiKey string UserLanguages map[string][]string
Success string ApiKey string
Error string Success string
Error string
} }
func (s *LeaderboardViewModel) WithSuccess(m string) *LeaderboardViewModel { func (s *LeaderboardViewModel) WithSuccess(m string) *LeaderboardViewModel {

View File

@ -2,6 +2,7 @@ package routes
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/slice"
"github.com/emvi/logbuch" "github.com/emvi/logbuch"
"github.com/gorilla/mux" "github.com/gorilla/mux"
conf "github.com/muety/wakapi/config" conf "github.com/muety/wakapi/config"
@ -58,6 +59,7 @@ func (h *LeaderboardHandler) buildViewModel(r *http.Request) *view.LeaderboardVi
var err error var err error
var leaderboard models.Leaderboard var leaderboard models.Leaderboard
var userLanguages map[string][]string
var topKeys []string var topKeys []string
if byParam == "" { if byParam == "" {
@ -74,6 +76,14 @@ func (h *LeaderboardHandler) buildViewModel(r *http.Request) *view.LeaderboardVi
return &view.LeaderboardViewModel{Error: criticalError} return &view.LeaderboardViewModel{Error: criticalError}
} }
userLeaderboards := slice.GroupWith[*models.LeaderboardItem, string](leaderboard, func(item *models.LeaderboardItem) string {
return item.UserID
})
userLanguages = map[string][]string{}
for u, items := range userLeaderboards {
userLanguages[u] = models.Leaderboard(items).TopKeysByUser(models.SummaryLanguage, u)
}
topKeys = leaderboard.TopKeys(by) topKeys = leaderboard.TopKeys(by)
if len(topKeys) > 0 { if len(topKeys) > 0 {
if keyParam == "" { if keyParam == "" {
@ -92,13 +102,14 @@ func (h *LeaderboardHandler) buildViewModel(r *http.Request) *view.LeaderboardVi
} }
return &view.LeaderboardViewModel{ return &view.LeaderboardViewModel{
User: user, User: user,
By: byParam, By: byParam,
Key: keyParam, Key: keyParam,
Items: leaderboard, Items: leaderboard,
TopKeys: topKeys, UserLanguages: userLanguages,
ApiKey: apiKey, TopKeys: topKeys,
Success: r.URL.Query().Get("success"), ApiKey: apiKey,
Error: r.URL.Query().Get("error"), Success: r.URL.Query().Get("success"),
Error: r.URL.Query().Get("error"),
} }
} }

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -47,8 +47,8 @@
<ol> <ol>
{{ range $i, $item := .Items }} {{ range $i, $item := .Items }}
<li class="px-4 py-2 my-2 rounded-md border-2 leaderboard-{{ ($.ColorModifier $item $.User) }} flex justify-between"> <li class="px-4 py-2 my-2 rounded-md border-2 leaderboard-{{ ($.ColorModifier $item $.User) }} flex justify-between">
<div class="w-2/12"><strong># {{ $item.Rank }}</strong></div> <div class="w-1/12"><strong># {{ $item.Rank }}</strong></div>
<div class="flex w-1/2 items-center space-x-4"> <div class="flex w-4/12 justify-start items-center space-x-4 text-ellipsis overflow-hidden whitespace-nowrap align-middle">
{{ if avatarUrlTemplate }} {{ if avatarUrlTemplate }}
<img src="{{ $item.User.AvatarURL avatarUrlTemplate }}" width="24px" class="rounded-full border-green-700" alt="User Profile Avatar"/> <img src="{{ $item.User.AvatarURL avatarUrlTemplate }}" width="24px" class="rounded-full border-green-700" alt="User Profile Avatar"/>
{{ else }} {{ else }}
@ -56,7 +56,10 @@
{{ end }} {{ end }}
<strong>@{{ $item.UserID }}</strong> <strong>@{{ $item.UserID }}</strong>
</div> </div>
<div class="w-4/12 text-right"><span>{{ $item.Total | duration }}</span></div> <span class="w-4/12 text-sm text-ellipsis overflow-hidden whitespace-nowrap leading-6 align-middle">
{{ join (index $.UserLanguages $item.UserID) ", " }}
</span>
<div class="w-3/12 text-right"><span>{{ $item.Total | duration }}</span></div>
</li> </li>
{{ end }} {{ end }}
</ol> </ol>