added fixture scheduling
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user