60 lines
1.6 KiB
Go
60 lines
1.6 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"
|
|
)
|
|
|
|
// SeasonLeagueTeamsPage renders the teams tab of a season league page
|
|
func SeasonLeagueTeamsPage(
|
|
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 season *db.Season
|
|
var league *db.League
|
|
var teams []*db.Team
|
|
var available []*db.Team
|
|
|
|
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
|
var err error
|
|
season, league, teams, 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")
|
|
}
|
|
|
|
available, err = db.GetList[db.Team](tx).
|
|
Join("LEFT JOIN team_participations tp ON tp.team_id = t.id").
|
|
Where("NOT tp.season_id = ? OR tp.season_id IS NULL", season.ID).
|
|
GetAll(ctx)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "db.GetList[Team]")
|
|
}
|
|
|
|
return true, nil
|
|
}); !ok {
|
|
return
|
|
}
|
|
|
|
if r.Method == "GET" {
|
|
renderSafely(seasonsview.SeasonLeagueTeamsPage(season, league, teams, available), s, r, w)
|
|
} else {
|
|
renderSafely(seasonsview.SeasonLeagueTeams(season, league, teams, available), s, r, w)
|
|
}
|
|
})
|
|
}
|