big ole refactor
This commit is contained in:
@@ -19,13 +19,6 @@ type League struct {
|
||||
Teams []Team `bun:"m2m:team_participations,join:League=Team"`
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -37,41 +30,16 @@ func GetLeague(ctx context.Context, tx bun.Tx, shortname string) (*League, error
|
||||
return GetByField[League](tx, "short_name", shortname).Relation("Seasons").Get(ctx)
|
||||
}
|
||||
|
||||
// GetSeasonLeague retrieves a specific season-league combination with teams
|
||||
func GetSeasonLeague(ctx context.Context, tx bun.Tx, seasonShortName, leagueShortName string) (*Season, *League, []*Team, error) {
|
||||
if seasonShortName == "" {
|
||||
return nil, nil, nil, errors.New("season short_name cannot be empty")
|
||||
func NewLeague(ctx context.Context, tx bun.Tx, name, shortname, description string, audit *AuditMeta) (*League, error) {
|
||||
league := &League{
|
||||
Name: name,
|
||||
ShortName: shortname,
|
||||
Description: description,
|
||||
}
|
||||
if leagueShortName == "" {
|
||||
return nil, nil, nil, errors.New("league short_name cannot be empty")
|
||||
}
|
||||
|
||||
// Get the season
|
||||
season, err := GetSeason(ctx, tx, seasonShortName)
|
||||
err := Insert(tx, league).
|
||||
WithAudit(audit, nil).Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(err, "GetSeason")
|
||||
return nil, errors.Wrap(err, "db.Insert")
|
||||
}
|
||||
|
||||
// Get the league
|
||||
league, err := GetLeague(ctx, tx, leagueShortName)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(err, "GetLeague")
|
||||
}
|
||||
if season == nil || league == nil || !season.HasLeague(league.ID) {
|
||||
return nil, nil, nil, nil
|
||||
}
|
||||
|
||||
// Get all teams participating in this season+league
|
||||
var teams []*Team
|
||||
err = tx.NewSelect().
|
||||
Model(&teams).
|
||||
Join("INNER JOIN team_participations AS tp ON tp.team_id = t.id").
|
||||
Where("tp.season_id = ? AND tp.league_id = ?", season.ID, league.ID).
|
||||
Order("t.name ASC").
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(err, "tx.Select teams")
|
||||
}
|
||||
|
||||
return season, league, teams, nil
|
||||
return league, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user