added leagues
This commit is contained in:
37
internal/db/league.go
Normal file
37
internal/db/league.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type League struct {
|
||||
bun.BaseModel `bun:"table:leagues,alias:l"`
|
||||
|
||||
ID int `bun:"id,pk,autoincrement"`
|
||||
Name string `bun:"name,unique,notnull"`
|
||||
ShortName string `bun:"short_name,unique,notnull"`
|
||||
Description string `bun:"description"`
|
||||
|
||||
Seasons []Season `bun:"m2m:season_leagues,join:League=Season"`
|
||||
}
|
||||
|
||||
type SeasonLeague struct {
|
||||
SeasonID int `bun:",pk"`
|
||||
Season *Season `bun:"rel:belongs-to,join:season_id=id"`
|
||||
LeagueID int `bun:",pk"`
|
||||
League *League `bun:"rel:belongs-to,join:league_id=id"`
|
||||
}
|
||||
|
||||
func GetLeagues(ctx context.Context, tx bun.Tx) ([]*League, error) {
|
||||
return GetList[League](tx).Relation("Seasons").GetAll(ctx)
|
||||
}
|
||||
|
||||
func GetLeague(ctx context.Context, tx bun.Tx, shortname string) (*League, error) {
|
||||
if shortname == "" {
|
||||
return nil, errors.New("shortname cannot be empty")
|
||||
}
|
||||
return GetByField[League](tx, "short_name", shortname).Relation("Seasons").Get(ctx)
|
||||
}
|
||||
@@ -19,14 +19,18 @@ type Season struct {
|
||||
EndDate bun.NullTime `bun:"end_date"`
|
||||
FinalsStartDate bun.NullTime `bun:"finals_start_date"`
|
||||
FinalsEndDate bun.NullTime `bun:"finals_end_date"`
|
||||
SlapVersion string `bun:"slap_version,notnull,default:'rebound'"`
|
||||
|
||||
Leagues []League `bun:"m2m:season_leagues,join:Season=League"`
|
||||
}
|
||||
|
||||
// NewSeason returns a new season. It does not add it to the database
|
||||
func NewSeason(name, shortname string, start time.Time) *Season {
|
||||
func NewSeason(name, version, shortname string, start time.Time) *Season {
|
||||
season := &Season{
|
||||
Name: name,
|
||||
ShortName: strings.ToUpper(shortname),
|
||||
StartDate: start.Truncate(time.Hour * 24),
|
||||
Name: name,
|
||||
ShortName: strings.ToUpper(shortname),
|
||||
StartDate: start.Truncate(time.Hour * 24),
|
||||
SlapVersion: version,
|
||||
}
|
||||
return season
|
||||
}
|
||||
@@ -38,18 +42,19 @@ func ListSeasons(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*List[Seas
|
||||
bun.OrderDesc,
|
||||
"start_date",
|
||||
}
|
||||
return GetList[Season](tx).GetPaged(ctx, pageOpts, defaults)
|
||||
return GetList[Season](tx).Relation("Leagues").GetPaged(ctx, pageOpts, defaults)
|
||||
}
|
||||
|
||||
func GetSeason(ctx context.Context, tx bun.Tx, shortname string) (*Season, error) {
|
||||
if shortname == "" {
|
||||
return nil, errors.New("short_name not provided")
|
||||
}
|
||||
return GetByField[Season](tx, "short_name", shortname).Get(ctx)
|
||||
return GetByField[Season](tx, "short_name", shortname).Relation("Leagues").Get(ctx)
|
||||
}
|
||||
|
||||
// Update updates the season struct. It does not insert to the database
|
||||
func (s *Season) Update(start, end, finalsStart, finalsEnd time.Time) {
|
||||
func (s *Season) Update(version string, start, end, finalsStart, finalsEnd time.Time) {
|
||||
s.SlapVersion = version
|
||||
s.StartDate = start.Truncate(time.Hour * 24)
|
||||
if !end.IsZero() {
|
||||
s.EndDate.Time = end.Truncate(time.Hour * 24)
|
||||
|
||||
Reference in New Issue
Block a user