fixtures #2

Merged
h merged 20 commits from fixtures into master 2026-02-23 20:38:26 +11:00
16 changed files with 416 additions and 78 deletions
Showing only changes of commit 9db855f45b - Show all commits

View 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)
})
}

View File

@@ -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)
})
}

View File

@@ -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.",

View File

@@ -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,

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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

View File

@@ -2,6 +2,7 @@ package rbac
import (
"context"
"fmt"
"net/http"
"strconv"
@@ -28,8 +29,11 @@ func LoadPreviewRoleMiddleware(s *hws.Server, conn *db.DB) func(http.Handler) ht
user := db.CurrentUser(r.Context())
if user == nil {
// User not logged in,
ClearPreviewRoleCookie(w)
fmt.Println(user)
// User not logged in
// Auth middleware skips on certain routes like CSS files so even
// if user IS logged in, this will trigger on those routes,
// so we just pass the request on and do nothing.
next.ServeHTTP(w, r)
return
}

View File

@@ -97,6 +97,32 @@ func addRoutes(
Method: hws.MethodPOST,
Handler: perms.RequirePermission(s, permissions.SeasonsUpdate)(handlers.SeasonEditSubmit(s, conn)),
},
// Draft season tab pages (must be before league routes to avoid conflicts)
{
Path: "/seasons/{season_short_name}/table",
Method: hws.MethodGET,
Handler: handlers.DraftSeasonTabPage(s, conn, "table"),
},
{
Path: "/seasons/{season_short_name}/fixtures",
Method: hws.MethodGET,
Handler: handlers.DraftSeasonTabPage(s, conn, "fixtures"),
},
{
Path: "/seasons/{season_short_name}/teams",
Method: hws.MethodGET,
Handler: handlers.DraftSeasonTabPage(s, conn, "teams"),
},
{
Path: "/seasons/{season_short_name}/stats",
Method: hws.MethodGET,
Handler: handlers.DraftSeasonTabPage(s, conn, "stats"),
},
{
Path: "/seasons/{season_short_name}/finals",
Method: hws.MethodGET,
Handler: handlers.DraftSeasonTabPage(s, conn, "finals"),
},
{
Path: "/seasons/{season_short_name}/leagues/{league_short_name}",
Method: hws.MethodGET,

View File

@@ -15,6 +15,136 @@ templ DetailPage(season *db.Season, leaguesWithTeams []db.LeagueWithTeams) {
}
}
templ DraftSeasonDetailPage(season *db.Season, league *db.League, teams []*db.Team, available []*db.Team, fixtures []*db.Fixture, defaultTab string) {
@baseview.Layout(season.Name) {
<div class="max-w-screen-2xl mx-auto px-4 py-8">
@DraftSeasonDetail(season, league, teams, available, fixtures, defaultTab)
</div>
<script src="/static/js/tabs.js" defer></script>
}
}
templ DraftSeasonDetail(season *db.Season, league *db.League, teams []*db.Team, available []*db.Team, fixtures []*db.Fixture, defaultTab string) {
{{
permCache := contexts.Permissions(ctx)
canEditSeason := permCache.HasPermission(permissions.SeasonsUpdate)
}}
<div class="bg-mantle border border-surface1 rounded-lg overflow-hidden">
<!-- Header Section -->
<div class="bg-surface0 border-b border-surface1 px-6 py-8">
<div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 mb-4">
<div>
<div class="flex items-center gap-3 mb-2">
<h1 class="text-4xl font-bold text-text">{ season.Name }</h1>
<span class="text-lg font-mono text-subtext0 bg-surface1 px-2 py-0.5 rounded">{ season.ShortName }</span>
</div>
<div class="flex items-center gap-2 flex-wrap">
@SeasonTypeBadge(season.Type)
@SlapVersionBadge(season.SlapVersion)
@StatusBadge(season, false, false)
</div>
</div>
<div class="flex gap-2">
if canEditSeason {
<a
href={ templ.SafeURL("/seasons/" + season.ShortName + "/edit") }
class="rounded-lg px-4 py-2 hover:cursor-pointer text-center
bg-blue hover:bg-blue/75 text-mantle transition"
>
Edit
</a>
}
<a
href="/seasons"
class="rounded-lg px-4 py-2 hover:cursor-pointer text-center
bg-surface1 hover:bg-surface2 text-text transition"
>
Back to Seasons
</a>
</div>
</div>
<!-- Season Dates -->
<div class="mt-4 pt-4 border-t border-surface1">
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<!-- Regular Season -->
<div class="bg-mantle border border-surface1 rounded-lg p-4">
<h3 class="text-sm font-semibold text-text mb-3 flex items-center justify-center gap-2">
<span class="text-blue">&#9679;</span>
Regular Season
</h3>
<div class="grid grid-cols-2 gap-4">
<div class="text-center">
<div class="text-xs text-subtext0 uppercase mb-1">Start</div>
<div class="text-sm text-text font-medium">{ formatDateLong(season.StartDate) }</div>
</div>
<div class="text-center">
<div class="text-xs text-subtext0 uppercase mb-1">Finish</div>
if !season.EndDate.IsZero() {
<div class="text-sm text-text font-medium">{ formatDateLong(season.EndDate.Time) }</div>
} else {
<div class="text-sm text-subtext1 italic">Not set</div>
}
</div>
</div>
</div>
<!-- Finals -->
<div class="bg-mantle border border-surface1 rounded-lg p-4">
<h3 class="text-sm font-semibold text-text mb-3 flex items-center justify-center gap-2">
<span class="text-yellow">&#9733;</span>
Finals
</h3>
<div class="grid grid-cols-2 gap-4">
<div class="text-center">
<div class="text-xs text-subtext0 uppercase mb-1">Start</div>
if !season.FinalsStartDate.IsZero() {
<div class="text-sm text-text font-medium">{ formatDateLong(season.FinalsStartDate.Time) }</div>
} else {
<div class="text-sm text-subtext1 italic">Not set</div>
}
</div>
<div class="text-center">
<div class="text-xs text-subtext0 uppercase mb-1">Finish</div>
if !season.FinalsEndDate.IsZero() {
<div class="text-sm text-text font-medium">{ formatDateLong(season.FinalsEndDate.Time) }</div>
} else {
<div class="text-sm text-subtext1 italic">Not set</div>
}
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Tab Navigation -->
<nav class="bg-surface0 border-b border-surface1" data-tab-nav="season-league-content">
<ul class="flex flex-wrap">
@draftNavItem("table", "Table", defaultTab, season, league)
@draftNavItem("fixtures", "Fixtures", defaultTab, season, league)
@draftNavItem("teams", "Teams", defaultTab, season, league)
@draftNavItem("stats", "Stats", defaultTab, season, league)
@draftNavItem("finals", "Finals", defaultTab, season, league)
</ul>
</nav>
<!-- Content Area -->
<main class="bg-crust p-6" id="season-league-content">
switch defaultTab {
case "table":
@SeasonLeagueTable()
case "fixtures":
@SeasonLeagueFixtures(season, league, fixtures)
case "teams":
@SeasonLeagueTeams(season, league, teams, available)
case "stats":
@SeasonLeagueStats()
case "finals":
@SeasonLeagueFinals()
}
</main>
</div>
}
templ SeasonDetails(season *db.Season, leaguesWithTeams []db.LeagueWithTeams) {
{{
permCache := contexts.Permissions(ctx)

View File

@@ -5,84 +5,86 @@ import "git.haelnorr.com/h/oslstats/internal/contexts"
import "git.haelnorr.com/h/oslstats/internal/permissions"
templ LeaguesSection(season *db.Season, allLeagues []*db.League) {
{{
permCache := contexts.Permissions(ctx)
canAddLeague := permCache.HasPermission(permissions.SeasonsAddLeague)
canRemoveLeague := permCache.HasPermission(permissions.SeasonsRemoveLeague)
if season.Type != db.SeasonTypeDraft.String() {
{{
permCache := contexts.Permissions(ctx)
canAddLeague := permCache.HasPermission(permissions.SeasonsAddLeague)
canRemoveLeague := permCache.HasPermission(permissions.SeasonsRemoveLeague)
// Create a map of assigned league IDs for quick lookup
assignedLeagueIDs := make(map[int]bool)
for _, league := range season.Leagues {
assignedLeagueIDs[league.ID] = true
}
}}
if canAddLeague || canRemoveLeague {
<div
id="leagues-section"
class="px-6 pb-6"
>
<div class="bg-surface0 border border-surface1 rounded-lg p-6">
<h2 class="text-2xl font-bold text-text mb-4">Leagues</h2>
<!-- Currently Assigned Leagues -->
if len(season.Leagues) > 0 {
<div class="mb-4">
<h3 class="text-sm font-medium text-subtext0 mb-2">Currently Assigned</h3>
<div class="flex flex-wrap gap-2">
for _, league := range season.Leagues {
<div class="flex items-center gap-2 bg-mantle border border-surface1 rounded-lg px-3 py-2">
<span class="text-sm text-text">{ league.Name }</span>
<span class="text-xs text-subtext0 font-mono">({ league.ShortName })</span>
if canRemoveLeague {
<button
type="button"
@click={ "window.dispatchEvent(new CustomEvent('confirm-action', { detail: { title: 'Remove League', message: 'Are you sure you want to remove " + league.Name + " from this season?', action: () => htmx.ajax('DELETE', '/seasons/" + season.ShortName + "/leagues/" + league.ShortName + "', { target: '#leagues-section', swap: 'outerHTML' }) } }))" }
class="text-red hover:text-red/75 hover:cursor-pointer ml-1"
>
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
}
</div>
}
</div>
</div>
}
<!-- Available Leagues to Add -->
if canAddLeague && len(allLeagues) > 0 {
{{
// Filter out already assigned leagues
availableLeagues := []*db.League{}
for _, league := range allLeagues {
if !assignedLeagueIDs[league.ID] {
availableLeagues = append(availableLeagues, league)
}
}
}}
if len(availableLeagues) > 0 {
<div>
<h3 class="text-sm font-medium text-subtext0 mb-2">Add League</h3>
// Create a map of assigned league IDs for quick lookup
assignedLeagueIDs := make(map[int]bool)
for _, league := range season.Leagues {
assignedLeagueIDs[league.ID] = true
}
}}
if canAddLeague || canRemoveLeague {
<div
id="leagues-section"
class="px-6 pb-6"
>
<div class="bg-surface0 border border-surface1 rounded-lg p-6">
<h2 class="text-2xl font-bold text-text mb-4">Leagues</h2>
<!-- Currently Assigned Leagues -->
if len(season.Leagues) > 0 {
<div class="mb-4">
<h3 class="text-sm font-medium text-subtext0 mb-2">Currently Assigned</h3>
<div class="flex flex-wrap gap-2">
for _, league := range availableLeagues {
<button
type="button"
hx-post={ "/seasons/" + season.ShortName + "/add-league/" + league.ShortName }
hx-target="#leagues-section"
hx-swap="outerHTML"
class="flex items-center gap-2 bg-surface1 hover:bg-surface2 border border-overlay0 rounded-lg px-3 py-2 transition hover:cursor-pointer"
>
for _, league := range season.Leagues {
<div class="flex items-center gap-2 bg-mantle border border-surface1 rounded-lg px-3 py-2">
<span class="text-sm text-text">{ league.Name }</span>
<span class="text-xs text-subtext0 font-mono">({ league.ShortName })</span>
<svg class="w-4 h-4 text-green" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
</svg>
</button>
if canRemoveLeague {
<button
type="button"
@click={ "window.dispatchEvent(new CustomEvent('confirm-action', { detail: { title: 'Remove League', message: 'Are you sure you want to remove " + league.Name + " from this season?', action: () => htmx.ajax('DELETE', '/seasons/" + season.ShortName + "/leagues/" + league.ShortName + "', { target: '#leagues-section', swap: 'outerHTML' }) } }))" }
class="text-red hover:text-red/75 hover:cursor-pointer ml-1"
>
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
}
</div>
}
</div>
</div>
}
}
<!-- Available Leagues to Add -->
if canAddLeague && len(allLeagues) > 0 {
{{
// Filter out already assigned leagues
availableLeagues := []*db.League{}
for _, league := range allLeagues {
if !assignedLeagueIDs[league.ID] {
availableLeagues = append(availableLeagues, league)
}
}
}}
if len(availableLeagues) > 0 {
<div>
<h3 class="text-sm font-medium text-subtext0 mb-2">Add League</h3>
<div class="flex flex-wrap gap-2">
for _, league := range availableLeagues {
<button
type="button"
hx-post={ "/seasons/" + season.ShortName + "/add-league/" + league.ShortName }
hx-target="#leagues-section"
hx-swap="outerHTML"
class="flex items-center gap-2 bg-surface1 hover:bg-surface2 border border-overlay0 rounded-lg px-3 py-2 transition hover:cursor-pointer"
>
<span class="text-sm text-text">{ league.Name }</span>
<span class="text-xs text-subtext0 font-mono">({ league.ShortName })</span>
<svg class="w-4 h-4 text-green" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
</svg>
</button>
}
</div>
</div>
}
}
</div>
</div>
</div>
}
}
}

View File

@@ -125,3 +125,26 @@ templ leagueNavItem(section string, label string, activeSection string, season *
</a>
</li>
}
templ draftNavItem(section string, label string, activeSection string, season *db.Season, league *db.League) {
{{
isActive := section == activeSection
baseClasses := "inline-block px-6 py-3 transition-colors cursor-pointer border-b-2"
activeClasses := "border-blue text-blue font-semibold"
inactiveClasses := "border-transparent text-subtext0 hover:text-text hover:border-surface2"
displayURL := fmt.Sprintf("/seasons/%s/%s", season.ShortName, section)
postURL := fmt.Sprintf("/seasons/%s/leagues/%s/%s", season.ShortName, league.ShortName, section)
}}
<li class="inline-block">
<a
href={ templ.SafeURL(displayURL) }
hx-post={ postURL }
hx-target="#season-league-content"
hx-swap="innerHTML"
hx-push-url={ displayURL }
class={ baseClasses, templ.KV(activeClasses, isActive), templ.KV(inactiveClasses, !isActive) }
>
{ label }
</a>
</li>
}