added teams
This commit is contained in:
@@ -22,6 +22,7 @@ type Season struct {
|
||||
SlapVersion string `bun:"slap_version,notnull,default:'rebound'"`
|
||||
|
||||
Leagues []League `bun:"m2m:season_leagues,join:Season=League"`
|
||||
Teams []Team `bun:"m2m:team_participations,join:Season=Team"`
|
||||
}
|
||||
|
||||
// NewSeason returns a new season. It does not add it to the database
|
||||
@@ -49,7 +50,7 @@ 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).Relation("Leagues").Get(ctx)
|
||||
return GetByField[Season](tx, "short_name", shortname).Relation("Leagues").Relation("Teams").Get(ctx)
|
||||
}
|
||||
|
||||
// Update updates the season struct. It does not insert to the database
|
||||
@@ -66,3 +67,39 @@ func (s *Season) Update(version string, start, end, finalsStart, finalsEnd time.
|
||||
s.FinalsEndDate.Time = finalsEnd.Truncate(time.Hour * 24)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Season) MapTeamsToLeagues(ctx context.Context, tx bun.Tx) ([]LeagueWithTeams, error) {
|
||||
// For each league, get the teams
|
||||
leaguesWithTeams := make([]LeagueWithTeams, len(s.Leagues))
|
||||
for i, league := range s.Leagues {
|
||||
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 = ?", s.ID, league.ID).
|
||||
Order("t.name ASC").
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "tx.NewSelect")
|
||||
}
|
||||
leaguesWithTeams[i] = LeagueWithTeams{
|
||||
League: &league,
|
||||
Teams: teams,
|
||||
}
|
||||
}
|
||||
return leaguesWithTeams, nil
|
||||
}
|
||||
|
||||
type LeagueWithTeams struct {
|
||||
League *League
|
||||
Teams []*Team
|
||||
}
|
||||
|
||||
func (s *Season) HasLeague(leagueID int) bool {
|
||||
for _, league := range s.Leagues {
|
||||
if league.ID == leagueID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user