110 lines
3.0 KiB
Go
110 lines
3.0 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.haelnorr.com/h/oslstats/internal/permissions"
|
|
"github.com/pkg/errors"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type SeasonLeague struct {
|
|
SeasonID int `bun:",pk"`
|
|
Season *Season `bun:"rel:belongs-to,join:season_id=id"`
|
|
LeagueID int `bun:",pk"`
|
|
League *League `bun:"rel:belongs-to,join:league_id=id"`
|
|
}
|
|
|
|
// GetSeasonLeague retrieves a specific season-league combination with teams
|
|
func GetSeasonLeague(ctx context.Context, tx bun.Tx, seasonShortName, leagueShortName string) (*Season, *League, []*Team, error) {
|
|
if seasonShortName == "" {
|
|
return nil, nil, nil, errors.New("season short_name cannot be empty")
|
|
}
|
|
if leagueShortName == "" {
|
|
return nil, nil, nil, errors.New("league short_name cannot be empty")
|
|
}
|
|
|
|
// Get the season
|
|
season, err := GetSeason(ctx, tx, seasonShortName)
|
|
if err != nil {
|
|
return nil, nil, nil, errors.Wrap(err, "GetSeason")
|
|
}
|
|
|
|
// Get the league
|
|
league, err := GetLeague(ctx, tx, leagueShortName)
|
|
if err != nil {
|
|
return nil, nil, nil, errors.Wrap(err, "GetLeague")
|
|
}
|
|
if !season.HasLeague(league.ID) {
|
|
return nil, nil, nil, BadRequestNotAssociated("season", "league", seasonShortName, leagueShortName)
|
|
}
|
|
|
|
// Get all teams participating in this season+league
|
|
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 = ?", season.ID, league.ID).
|
|
Order("t.name ASC").
|
|
Scan(ctx)
|
|
if err != nil {
|
|
return nil, nil, nil, errors.Wrap(err, "tx.Select teams")
|
|
}
|
|
|
|
return season, league, teams, nil
|
|
}
|
|
|
|
func NewSeasonLeague(ctx context.Context, tx bun.Tx, seasonShortName, leagueShortName string, audit *AuditMeta) error {
|
|
season, err := GetSeason(ctx, tx, seasonShortName)
|
|
if err != nil {
|
|
return errors.Wrap(err, "GetSeason")
|
|
}
|
|
league, err := GetLeague(ctx, tx, leagueShortName)
|
|
if err != nil {
|
|
return errors.Wrap(err, "GetLeague")
|
|
}
|
|
if season.HasLeague(league.ID) {
|
|
return BadRequestAssociated("season", "league", seasonShortName, leagueShortName)
|
|
}
|
|
seasonLeague := &SeasonLeague{
|
|
SeasonID: season.ID,
|
|
LeagueID: league.ID,
|
|
}
|
|
info := &AuditInfo{
|
|
string(permissions.SeasonsAddLeague),
|
|
"season",
|
|
season.ID,
|
|
map[string]any{"league_id": league.ID},
|
|
}
|
|
err = Insert(tx, seasonLeague).WithAudit(audit, info).Exec(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(err, "db.Insert")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Season) RemoveLeague(ctx context.Context, tx bun.Tx, leagueShortName string, audit *AuditMeta) error {
|
|
league, err := GetLeague(ctx, tx, leagueShortName)
|
|
if err != nil {
|
|
return errors.Wrap(err, "GetLeague")
|
|
}
|
|
if !s.HasLeague(league.ID) {
|
|
return errors.New("league not in season")
|
|
}
|
|
info := &AuditInfo{
|
|
string(permissions.SeasonsRemoveLeague),
|
|
"season",
|
|
s.ID,
|
|
map[string]any{"league_id": league.ID},
|
|
}
|
|
err = DeleteItem[SeasonLeague](tx).
|
|
Where("season_id = ?", s.ID).
|
|
Where("league_id = ?", league.ID).
|
|
WithAudit(audit, info).
|
|
Delete(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(err, "db.DeleteItem")
|
|
}
|
|
return nil
|
|
}
|