73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"git.haelnorr.com/h/oslstats/internal/db"
|
|
"git.haelnorr.com/h/oslstats/internal/throw"
|
|
seasonsview "git.haelnorr.com/h/oslstats/internal/view/seasonsview"
|
|
"github.com/pkg/errors"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
// SeasonLeagueStatsPage renders the stats tab of a season league page
|
|
func SeasonLeagueStatsPage(
|
|
s *hws.Server,
|
|
conn *db.DB,
|
|
) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
seasonStr := r.PathValue("season_short_name")
|
|
leagueStr := r.PathValue("league_short_name")
|
|
|
|
var sl *db.SeasonLeague
|
|
var topGoals []*db.LeagueTopGoalScorer
|
|
var topAssists []*db.LeagueTopAssister
|
|
var topSaves []*db.LeagueTopSaver
|
|
var allStats []*db.LeaguePlayerStats
|
|
|
|
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
|
var err error
|
|
sl, err = db.GetSeasonLeague(ctx, tx, seasonStr, leagueStr)
|
|
if err != nil {
|
|
if db.IsBadRequest(err) {
|
|
throw.NotFound(s, w, r, r.URL.Path)
|
|
return false, nil
|
|
}
|
|
return false, errors.Wrap(err, "db.GetSeasonLeague")
|
|
}
|
|
|
|
topGoals, err = db.GetTopGoalScorers(ctx, tx, sl.SeasonID, sl.LeagueID)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "db.GetTopGoalScorers")
|
|
}
|
|
|
|
topAssists, err = db.GetTopAssisters(ctx, tx, sl.SeasonID, sl.LeagueID)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "db.GetTopAssisters")
|
|
}
|
|
|
|
topSaves, err = db.GetTopSavers(ctx, tx, sl.SeasonID, sl.LeagueID)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "db.GetTopSavers")
|
|
}
|
|
|
|
allStats, err = db.GetAllLeaguePlayerStats(ctx, tx, sl.SeasonID, sl.LeagueID)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "db.GetAllLeaguePlayerStats")
|
|
}
|
|
|
|
return true, nil
|
|
}); !ok {
|
|
return
|
|
}
|
|
|
|
if r.Method == "GET" {
|
|
renderSafely(seasonsview.SeasonLeagueStatsPage(sl.Season, sl.League, topGoals, topAssists, topSaves, allStats), s, r, w)
|
|
} else {
|
|
renderSafely(seasonsview.SeasonLeagueStats(sl.Season, sl.League, topGoals, topAssists, topSaves, allStats), s, r, w)
|
|
}
|
|
})
|
|
}
|