Files
oslstats/internal/db/league.go

46 lines
1.3 KiB
Go

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" json:"id"`
Name string `bun:"name,unique,notnull" json:"name"`
ShortName string `bun:"short_name,unique,notnull" json:"short_name"`
Description string `bun:"description" json:"description"`
Seasons []Season `bun:"m2m:season_leagues,join:League=Season" json:"-"`
Teams []Team `bun:"m2m:team_participations,join:League=Team" json:"-"`
}
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)
}
func NewLeague(ctx context.Context, tx bun.Tx, name, shortname, description string, audit *AuditMeta) (*League, error) {
league := &League{
Name: name,
ShortName: shortname,
Description: description,
}
err := Insert(tx, league).
WithAudit(audit, nil).Exec(ctx)
if err != nil {
return nil, errors.Wrap(err, "db.Insert")
}
return league, nil
}