package db import ( "context" "github.com/pkg/errors" "github.com/uptrace/bun" ) type Team struct { bun.BaseModel `bun:"table:teams,alias:t"` ID int `bun:"id,pk,autoincrement"` Name string `bun:"name,unique,notnull"` ShortName string `bun:"short_name,notnull,unique:short_names"` AltShortName string `bun:"alt_short_name,notnull,unique:short_names"` Color string `bun:"color"` Seasons []Season `bun:"m2m:team_participations,join:Team=Season"` 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 ListTeams(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*List[Team], error) { defaults := &PageOpts{ 1, 10, bun.OrderAsc, "name", } return GetList[Team](tx).GetPaged(ctx, pageOpts, defaults) } func GetTeam(ctx context.Context, tx bun.Tx, id int) (*Team, error) { if id == 0 { return nil, errors.New("id not provided") } return GetByID[Team](tx, id).Relation("Seasons").Relation("Leagues").Get(ctx) } 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). Count(ctx) if err != nil { return false, errors.Wrap(err, "tx.Select") } return count == 0, nil }