package handlers import ( "context" "fmt" "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" ) func SeasonPage( s *hws.Server, conn *db.DB, ) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { seasonStr := r.PathValue("season_short_name") var season *db.Season var leaguesWithTeams []db.LeagueWithTeams if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) { var err error season, err = db.GetSeason(ctx, tx, seasonStr) if err != nil { if db.IsBadRequest(err) { throw.NotFound(s, w, r, r.URL.Path) return false, nil } return false, errors.Wrap(err, "db.GetSeason") } if season.Type != db.SeasonTypeDraft.String() { leaguesWithTeams, err = season.MapTeamsToLeagues(ctx, tx) if err != nil { return false, errors.Wrap(err, "season.MapTeamsToLeagues") } } return true, nil }); !ok { return } if season.Type == db.SeasonTypeDraft.String() { // Redirect draft seasons to their default tab defaultTab := season.GetDefaultTab() redirectURL := fmt.Sprintf("/seasons/%s/%s", seasonStr, defaultTab) http.Redirect(w, r, redirectURL, http.StatusSeeOther) return } renderSafely(seasonsview.DetailPage(season, leaguesWithTeams), s, r, w) }) }