added team overview
This commit is contained in:
67
internal/handlers/team_detail.go
Normal file
67
internal/handlers/team_detail.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
"git.haelnorr.com/h/oslstats/internal/throw"
|
||||
teamsview "git.haelnorr.com/h/oslstats/internal/view/teamsview"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// TeamDetailPage renders the global team detail page showing cross-season stats
|
||||
func TeamDetailPage(
|
||||
s *hws.Server,
|
||||
conn *db.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
teamIDStr := r.PathValue("team_id")
|
||||
|
||||
teamID, err := strconv.Atoi(teamIDStr)
|
||||
if err != nil {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return
|
||||
}
|
||||
|
||||
var team *db.Team
|
||||
var seasonInfos []*db.TeamSeasonInfo
|
||||
var playerStats []*db.TeamAllTimePlayerStats
|
||||
|
||||
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
team, err = db.GetTeam(ctx, tx, teamID)
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return false, nil
|
||||
}
|
||||
return false, errors.Wrap(err, "db.GetTeam")
|
||||
}
|
||||
|
||||
seasonInfos, err = db.GetTeamSeasonParticipation(ctx, tx, teamID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetTeamSeasonParticipation")
|
||||
}
|
||||
|
||||
playerStats, err = db.GetTeamAllTimePlayerStats(ctx, tx, teamID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetTeamAllTimePlayerStats")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
activeTab := r.URL.Query().Get("tab")
|
||||
if activeTab != "seasons" && activeTab != "stats" {
|
||||
activeTab = "seasons"
|
||||
}
|
||||
|
||||
renderSafely(teamsview.DetailPage(team, seasonInfos, playerStats, activeTab), s, r, w)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user