draft seasons get special treatment :)
This commit is contained in:
96
internal/handlers/draft_season_tabs.go
Normal file
96
internal/handlers/draft_season_tabs.go
Normal file
@@ -0,0 +1,96 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// redirectDraftSeasonLeague checks if a season is a draft type and redirects
|
||||
// GET requests from /seasons/{short}/leagues/Draft/{tab} to /seasons/{short}/{tab}.
|
||||
// Returns true if a redirect was issued (caller should return early).
|
||||
// POST requests are not redirected since they are HTMX partial content requests.
|
||||
func redirectDraftSeasonLeague(season *db.Season, tab string, w http.ResponseWriter, r *http.Request) bool {
|
||||
if r.Method == "GET" && season.Type == db.SeasonTypeDraft.String() {
|
||||
redirectURL := fmt.Sprintf("/seasons/%s/%s", season.ShortName, tab)
|
||||
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// DraftSeasonTabPage handles GET requests for draft season tab pages at
|
||||
// /seasons/{season_short_name}/{tab}. It renders the full DraftSeasonDetailPage
|
||||
// with the appropriate tab content pre-rendered.
|
||||
func DraftSeasonTabPage(
|
||||
s *hws.Server,
|
||||
conn *db.DB,
|
||||
tab string,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seasonStr := r.PathValue("season_short_name")
|
||||
|
||||
var season *db.Season
|
||||
var league *db.League
|
||||
var teams []*db.Team
|
||||
var availableTeams []*db.Team
|
||||
var fixtures []*db.Fixture
|
||||
|
||||
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
|
||||
// Verify this is a draft season
|
||||
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() {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Fetch the Draft league and teams
|
||||
season, league, teams, err = db.GetSeasonLeague(ctx, tx, seasonStr, "Draft")
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetSeasonLeague")
|
||||
}
|
||||
|
||||
// Fetch tab-specific data
|
||||
switch tab {
|
||||
case "teams":
|
||||
availableTeams, 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]")
|
||||
}
|
||||
case "fixtures":
|
||||
season, league, fixtures, err = db.GetFixtures(ctx, tx, seasonStr, "Draft")
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetFixtures")
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
renderSafely(seasonsview.DraftSeasonDetailPage(
|
||||
season, league, teams, availableTeams, fixtures, tab,
|
||||
), s, r, w)
|
||||
})
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
@@ -32,15 +33,26 @@ func SeasonPage(
|
||||
return false, errors.Wrap(err, "db.GetSeason")
|
||||
}
|
||||
|
||||
leaguesWithTeams, err = season.MapTeamsToLeagues(ctx, tx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "season.MapTeamsToLeagues")
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -50,7 +50,11 @@ func SeasonLeagueAddTeam(
|
||||
}
|
||||
|
||||
// Redirect to refresh the page
|
||||
w.Header().Set("HX-Redirect", fmt.Sprintf("/seasons/%s/leagues/%s/teams", season.ShortName, league.ShortName))
|
||||
redirectURL := fmt.Sprintf("/seasons/%s/leagues/%s/teams", season.ShortName, league.ShortName)
|
||||
if season.Type == db.SeasonTypeDraft.String() {
|
||||
redirectURL = fmt.Sprintf("/seasons/%s/teams", season.ShortName)
|
||||
}
|
||||
w.Header().Set("HX-Redirect", redirectURL)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
notify.Success(s, w, r, "Team Added", fmt.Sprintf(
|
||||
"Successfully added '%s' to the league.",
|
||||
|
||||
@@ -39,6 +39,14 @@ func SeasonLeaguePage(
|
||||
}
|
||||
|
||||
defaultTab := season.GetDefaultTab()
|
||||
|
||||
// Draft seasons redirect to /seasons/{short}/{tab} instead
|
||||
if season.Type == db.SeasonTypeDraft.String() {
|
||||
redirectURL := fmt.Sprintf("/seasons/%s/%s", seasonStr, defaultTab)
|
||||
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
redirectURL := fmt.Sprintf(
|
||||
"/seasons/%s/leagues/%s/%s",
|
||||
seasonStr, leagueStr, defaultTab,
|
||||
|
||||
@@ -39,6 +39,9 @@ func SeasonLeagueFinalsPage(
|
||||
return
|
||||
}
|
||||
|
||||
if redirectDraftSeasonLeague(season, "finals", w, r) {
|
||||
return
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
renderSafely(seasonsview.SeasonLeagueFinalsPage(season, league), s, r, w)
|
||||
} else {
|
||||
|
||||
@@ -41,6 +41,9 @@ func SeasonLeagueFixturesPage(
|
||||
return
|
||||
}
|
||||
|
||||
if redirectDraftSeasonLeague(season, "fixtures", w, r) {
|
||||
return
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
renderSafely(seasonsview.SeasonLeagueFixturesPage(season, league, fixtures), s, r, w)
|
||||
} else {
|
||||
|
||||
@@ -39,6 +39,9 @@ func SeasonLeagueStatsPage(
|
||||
return
|
||||
}
|
||||
|
||||
if redirectDraftSeasonLeague(season, "stats", w, r) {
|
||||
return
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
renderSafely(seasonsview.SeasonLeagueStatsPage(season, league), s, r, w)
|
||||
} else {
|
||||
|
||||
@@ -38,6 +38,9 @@ func SeasonLeagueTablePage(
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
if redirectDraftSeasonLeague(season, "table", w, r) {
|
||||
return
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
renderSafely(seasonsview.SeasonLeagueTablePage(season, league), s, r, w)
|
||||
} else {
|
||||
|
||||
@@ -50,6 +50,9 @@ func SeasonLeagueTeamsPage(
|
||||
return
|
||||
}
|
||||
|
||||
if redirectDraftSeasonLeague(season, "teams", w, r) {
|
||||
return
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
renderSafely(seasonsview.SeasonLeagueTeamsPage(season, league, teams, available), s, r, w)
|
||||
} else {
|
||||
|
||||
@@ -25,7 +25,21 @@ func SeasonAddLeague(
|
||||
var season *db.Season
|
||||
var allLeagues []*db.League
|
||||
if ok := conn.WithNotifyTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
err := db.NewSeasonLeague(ctx, tx, seasonShortName, leagueShortName, db.NewAuditFromRequest(r))
|
||||
// Check if season is a draft season
|
||||
seasonCheck, err := db.GetSeason(ctx, tx, seasonShortName)
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
respond.NotFound(w, err)
|
||||
return false, nil
|
||||
}
|
||||
return false, errors.Wrap(err, "db.GetSeason")
|
||||
}
|
||||
if seasonCheck.Type == db.SeasonTypeDraft.String() {
|
||||
respond.BadRequest(w, errors.New("cannot manually manage leagues for draft seasons"))
|
||||
return false, nil
|
||||
}
|
||||
|
||||
err = db.NewSeasonLeague(ctx, tx, seasonShortName, leagueShortName, db.NewAuditFromRequest(r))
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
respond.BadRequest(w, err)
|
||||
@@ -75,6 +89,10 @@ func SeasonRemoveLeague(
|
||||
}
|
||||
return false, errors.Wrap(err, "db.GetSeason")
|
||||
}
|
||||
if season.Type == db.SeasonTypeDraft.String() {
|
||||
respond.BadRequest(w, errors.New("cannot manually manage leagues for draft seasons"))
|
||||
return false, nil
|
||||
}
|
||||
err = season.RemoveLeague(ctx, tx, leagueStr, db.NewAuditFromRequest(r))
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
|
||||
@@ -35,7 +35,7 @@ func NewTeamSubmit(
|
||||
}
|
||||
name := getter.String("name").
|
||||
TrimSpace().Required().
|
||||
MaxLength(25).MinLength(3).Value
|
||||
MaxLength(50).MinLength(3).Value
|
||||
shortName := getter.String("short_name").
|
||||
TrimSpace().Required().ToUpper().
|
||||
MaxLength(3).MinLength(3).Value
|
||||
|
||||
Reference in New Issue
Block a user