big ole refactor
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"net/http"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/oslstats/internal/auditlog"
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
"git.haelnorr.com/h/oslstats/internal/notify"
|
||||
"git.haelnorr.com/h/oslstats/internal/validation"
|
||||
@@ -16,8 +15,7 @@ import (
|
||||
|
||||
func SeasonLeagueAddTeam(
|
||||
s *hws.Server,
|
||||
conn *bun.DB,
|
||||
audit *auditlog.Logger,
|
||||
conn *db.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seasonStr := r.PathValue("season_short_name")
|
||||
@@ -36,73 +34,12 @@ func SeasonLeagueAddTeam(
|
||||
var league *db.League
|
||||
var team *db.Team
|
||||
|
||||
if ok := db.WithNotifyTx(s, w, r, conn, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
if ok := conn.WithNotifyTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
|
||||
// Get season
|
||||
season, err = db.GetSeason(ctx, tx, seasonStr)
|
||||
team, season, league, err = db.NewTeamParticipation(ctx, tx, seasonStr, leagueStr, teamID, db.NewAudit(r, nil))
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetSeason")
|
||||
return false, errors.Wrap(err, "db.NewTeamParticipation")
|
||||
}
|
||||
if season == nil {
|
||||
notify.Warn(s, w, r, "Not Found", "Season not found.", nil)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Get league
|
||||
league, err = db.GetLeague(ctx, tx, leagueStr)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetLeague")
|
||||
}
|
||||
if league == nil {
|
||||
notify.Warn(s, w, r, "Not Found", "League not found.", nil)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if !season.HasLeague(league.ID) {
|
||||
notify.Warn(s, w, r, "Invalid League", "This league is not associated with this season.", nil)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Get team
|
||||
team, err = db.GetTeam(ctx, tx, teamID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetTeam")
|
||||
}
|
||||
if team == nil {
|
||||
notify.Warn(s, w, r, "Not Found", "Team not found.", nil)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Check if team is already in this season (in any league)
|
||||
var tpCount int
|
||||
tpCount, err = tx.NewSelect().
|
||||
Model((*db.TeamParticipation)(nil)).
|
||||
Where("season_id = ? AND team_id = ?", season.ID, team.ID).
|
||||
Count(ctx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "tx.NewSelect")
|
||||
}
|
||||
if tpCount > 0 {
|
||||
notify.Warn(s, w, r, "Already In Season", fmt.Sprintf(
|
||||
"Team '%s' is already participating in this season.",
|
||||
team.Name,
|
||||
), nil)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Add team to league
|
||||
participation := &db.TeamParticipation{
|
||||
SeasonID: season.ID,
|
||||
LeagueID: league.ID,
|
||||
TeamID: team.ID,
|
||||
}
|
||||
|
||||
err = db.Insert(tx, participation).WithAudit(r, audit.Callback()).Exec(ctx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.Insert")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user