package db import ( "context" "strings" "time" "github.com/pkg/errors" "github.com/uptrace/bun" ) type Season struct { bun.BaseModel `bun:"table:seasons,alias:s"` ID int `bun:"id,pk,autoincrement"` Name string `bun:"name,unique,notnull"` ShortName string `bun:"short_name,unique,notnull"` 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"` } // NewSeason returns a new season. It does not add it to the database func NewSeason(name, shortname string, start time.Time) *Season { season := &Season{ Name: name, ShortName: strings.ToUpper(shortname), StartDate: start.Truncate(time.Hour * 24), } return season } func ListSeasons(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*List[Season], error) { defaults := &PageOpts{ 1, 10, bun.OrderDesc, "start_date", } return GetList[Season](tx, pageOpts, defaults).GetAll(ctx) } 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).GetFirst(ctx) } // Update updates the season struct. It does not insert to the database func (s *Season) Update(start, end, finalsStart, finalsEnd time.Time) { s.StartDate = start.Truncate(time.Hour * 24) if !end.IsZero() { s.EndDate.Time = end.Truncate(time.Hour * 24) } if !finalsStart.IsZero() { s.FinalsStartDate.Time = finalsStart.Truncate(time.Hour * 24) } if !finalsEnd.IsZero() { s.FinalsEndDate.Time = finalsEnd.Truncate(time.Hour * 24) } }