big ole refactor
This commit is contained in:
@@ -19,13 +19,19 @@ type Team struct {
|
||||
Leagues []League `bun:"m2m:team_participations,join:Team=League"`
|
||||
}
|
||||
|
||||
type TeamParticipation struct {
|
||||
SeasonID int `bun:",pk,unique:season_team"`
|
||||
Season *Season `bun:"rel:belongs-to,join:season_id=id"`
|
||||
LeagueID int `bun:",pk"`
|
||||
League *League `bun:"rel:belongs-to,join:league_id=id"`
|
||||
TeamID int `bun:",pk,unique:season_team"`
|
||||
Team *Team `bun:"rel:belongs-to,join:team_id=id"`
|
||||
func NewTeam(ctx context.Context, tx bun.Tx, name, shortName, altShortName, color string, audit *AuditMeta) (*Team, error) {
|
||||
team := &Team{
|
||||
Name: name,
|
||||
ShortName: shortName,
|
||||
AltShortName: altShortName,
|
||||
Color: color,
|
||||
}
|
||||
err := Insert(tx, team).
|
||||
WithAudit(audit, nil).Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "db.Insert")
|
||||
}
|
||||
return team, nil
|
||||
}
|
||||
|
||||
func ListTeams(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*List[Team], error) {
|
||||
@@ -45,14 +51,23 @@ func GetTeam(ctx context.Context, tx bun.Tx, id int) (*Team, error) {
|
||||
return GetByID[Team](tx, id).Relation("Seasons").Relation("Leagues").Get(ctx)
|
||||
}
|
||||
|
||||
func TeamShortNamesUnique(ctx context.Context, tx bun.Tx, shortname, altshortname string) (bool, error) {
|
||||
func TeamShortNamesUnique(ctx context.Context, tx bun.Tx, shortName, altShortName string) (bool, error) {
|
||||
// Check if this combination of short_name and alt_short_name exists
|
||||
count, err := tx.NewSelect().
|
||||
Model((*Team)(nil)).
|
||||
Where("short_name = ? AND alt_short_name = ?", shortname, altshortname).
|
||||
Where("short_name = ? AND alt_short_name = ?", shortName, altShortName).
|
||||
Count(ctx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "tx.Select")
|
||||
}
|
||||
return count == 0, nil
|
||||
}
|
||||
|
||||
func (t *Team) InSeason(seasonID int) bool {
|
||||
for _, season := range t.Seasons {
|
||||
if season.ID == seasonID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user