added fixture scheduling

This commit is contained in:
2026-02-21 14:19:46 +11:00
parent 23a97787d5
commit b07c486421
13 changed files with 1703 additions and 18 deletions

View File

@@ -28,6 +28,35 @@ type Fixture struct {
League *League `bun:"rel:belongs-to,join:league_id=id"`
HomeTeam *Team `bun:"rel:belongs-to,join:home_team_id=id"`
AwayTeam *Team `bun:"rel:belongs-to,join:away_team_id=id"`
Schedules []*FixtureSchedule `bun:"rel:has-many,join:id=fixture_id"`
}
// CanSchedule checks if the user is a manager of one of the teams in the fixture.
// Returns (canSchedule, teamID) where teamID is the team the user manages (0 if not a manager).
func (f *Fixture) CanSchedule(ctx context.Context, tx bun.Tx, user *User) (bool, int, error) {
if user == nil || user.Player == nil {
return false, 0, nil
}
roster := new(TeamRoster)
err := tx.NewSelect().
Model(roster).
Column("team_id", "is_manager").
Where("team_id IN (?)", bun.In([]int{f.HomeTeamID, f.AwayTeamID})).
Where("season_id = ?", f.SeasonID).
Where("league_id = ?", f.LeagueID).
Where("player_id = ?", user.Player.ID).
Scan(ctx)
if err != nil {
if err.Error() == "sql: no rows in result set" {
return false, 0, nil
}
return false, 0, errors.Wrap(err, "tx.NewSelect")
}
if !roster.IsManager {
return false, 0, nil
}
return true, roster.TeamID, nil
}
func NewFixture(ctx context.Context, tx bun.Tx, seasonShortName, leagueShortName string,