added team view to season_leagues
This commit is contained in:
63
internal/handlers/season_league_team_detail.go
Normal file
63
internal/handlers/season_league_team_detail.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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"
|
||||
seasonsview "git.haelnorr.com/h/oslstats/internal/view/seasonsview"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// SeasonLeagueTeamDetailPage renders the detail page for a team within a season league
|
||||
func SeasonLeagueTeamDetailPage(
|
||||
s *hws.Server,
|
||||
conn *db.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seasonShortName := r.PathValue("season_short_name")
|
||||
leagueShortName := r.PathValue("league_short_name")
|
||||
teamIDStr := r.PathValue("team_id")
|
||||
|
||||
teamID, err := strconv.Atoi(teamIDStr)
|
||||
if err != nil {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return
|
||||
}
|
||||
|
||||
var twr *db.TeamWithRoster
|
||||
var fixtures []*db.Fixture
|
||||
var available []*db.Player
|
||||
|
||||
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
twr, err = db.GetTeamRoster(ctx, tx, seasonShortName, leagueShortName, 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.GetTeamRoster")
|
||||
}
|
||||
fixtures, err = db.GetFixturesForTeam(ctx, tx, twr.Season.ID, twr.League.ID, twr.Team.ID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetFixturesForTeam")
|
||||
}
|
||||
|
||||
available, err = db.GetPlayersNotOnTeam(ctx, tx, twr.Season.ID, twr.League.ID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetPlayersNotOnTeam")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
renderSafely(seasonsview.SeasonLeagueTeamDetailPage(twr, fixtures, available), s, r, w)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user