38 lines
1002 B
Go
38 lines
1002 B
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"`
|
|
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)
|
|
}
|