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 var scheduleMap map[int]*db.FixtureSchedule 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") } fixtureIDs := make([]int, len(fixtures)) for i, f := range fixtures { fixtureIDs[i] = f.ID } scheduleMap, err = db.GetAcceptedSchedulesForFixtures(ctx, tx, fixtureIDs) if err != nil { return false, errors.Wrap(err, "db.GetAcceptedSchedulesForFixtures") } 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, scheduleMap), s, r, w) }) }