scaffolding for new seasons

This commit is contained in:
2026-02-02 19:12:14 +11:00
parent 5b4fe91c55
commit 67bf84c3a6
21 changed files with 520 additions and 41 deletions

View File

@@ -3,6 +3,8 @@ package db
import (
"context"
"database/sql"
"strings"
"time"
"github.com/pkg/errors"
"github.com/uptrace/bun"
@@ -11,9 +13,13 @@ import (
type Season struct {
bun.BaseModel `bun:"table:seasons,alias:s"`
ID int `bun:"id,pk,autoincrement"`
Name string `bun:"name,unique"`
ShortName string `bun:"short_name,unique"`
ID int `bun:"id,pk,autoincrement"`
Name string `bun:"name,unique"`
ShortName string `bun:"short_name,unique"`
StartDate time.Time `bun:"start_date,notnull"`
EndDate bun.NullTime `bun:"end_date"`
FinalsStartDate bun.NullTime `bun:"finals_start_date"`
FinalsEndDate bun.NullTime `bun:"finals_end_date"`
}
type SeasonList struct {
@@ -22,7 +28,7 @@ type SeasonList struct {
PageOpts PageOpts
}
func NewSeason(ctx context.Context, tx bun.Tx, name, shortname string) (*Season, error) {
func NewSeason(ctx context.Context, tx bun.Tx, name, shortname string, start time.Time) (*Season, error) {
if name == "" {
return nil, errors.New("name cannot be empty")
}
@@ -31,7 +37,8 @@ func NewSeason(ctx context.Context, tx bun.Tx, name, shortname string) (*Season,
}
season := &Season{
Name: name,
ShortName: shortname,
ShortName: strings.ToUpper(shortname),
StartDate: start.Truncate(time.Hour * 24),
}
_, err := tx.NewInsert().
Model(season).
@@ -81,3 +88,38 @@ func ListSeasons(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*SeasonLis
}
return sl, nil
}
func GetSeason(ctx context.Context, tx bun.Tx, shortname string) (*Season, error) {
var season *Season
err := tx.NewSelect().
Model(season).
Where("short_name = ?", strings.ToUpper(shortname)).
Limit(1).
Scan(ctx)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrap(err, "tx.NewSelect")
}
return season, nil
}
func IsSeasonNameUnique(ctx context.Context, tx bun.Tx, name string) (bool, error) {
count, err := tx.NewSelect().
Model((*Season)(nil)).
Where("name = ?", name).
Count(ctx)
if err != nil {
return false, errors.Wrap(err, "tx.NewSelect")
}
return count == 0, nil
}
func IsSeasonShortNameUnique(ctx context.Context, tx bun.Tx, shortname string) (bool, error) {
count, err := tx.NewSelect().
Model((*Season)(nil)).
Where("short_name = ?", shortname).
Count(ctx)
if err != nil {
return false, errors.Wrap(err, "tx.NewSelect")
}
return count == 0, nil
}