big ole refactor
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
|
||||
"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/view/seasonsview"
|
||||
@@ -16,8 +15,7 @@ import (
|
||||
|
||||
func SeasonAddLeague(
|
||||
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")
|
||||
@@ -25,32 +23,10 @@ func SeasonAddLeague(
|
||||
|
||||
var season *db.Season
|
||||
var allLeagues []*db.League
|
||||
if ok := db.WithNotifyTx(s, w, r, conn, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
season, err = db.GetSeason(ctx, tx, seasonStr)
|
||||
if ok := conn.WithNotifyTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
err := db.NewSeasonLeague(ctx, tx, seasonStr, leagueStr, db.NewAudit(r, nil))
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetSeason")
|
||||
}
|
||||
if season == nil {
|
||||
return false, errors.New("season not found")
|
||||
}
|
||||
|
||||
league, err := db.GetLeague(ctx, tx, leagueStr)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetLeague")
|
||||
}
|
||||
if league == nil {
|
||||
return false, errors.New("league not found")
|
||||
}
|
||||
|
||||
// Create the many-to-many relationship
|
||||
seasonLeague := &db.SeasonLeague{
|
||||
SeasonID: season.ID,
|
||||
LeagueID: league.ID,
|
||||
}
|
||||
err = db.Insert(tx, seasonLeague).WithAudit(r, audit.Callback()).Exec(ctx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.Insert")
|
||||
return false, errors.Wrap(err, "db.NewSeasonLeague")
|
||||
}
|
||||
|
||||
// Reload season with updated leagues
|
||||
@@ -76,8 +52,7 @@ func SeasonAddLeague(
|
||||
|
||||
func SeasonRemoveLeague(
|
||||
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")
|
||||
@@ -85,7 +60,7 @@ func SeasonRemoveLeague(
|
||||
|
||||
var season *db.Season
|
||||
var allLeagues []*db.League
|
||||
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
|
||||
season, err = db.GetSeason(ctx, tx, seasonStr)
|
||||
if err != nil {
|
||||
@@ -94,22 +69,9 @@ func SeasonRemoveLeague(
|
||||
if season == nil {
|
||||
return false, errors.New("season not found")
|
||||
}
|
||||
|
||||
league, err := db.GetLeague(ctx, tx, leagueStr)
|
||||
err = season.RemoveLeague(ctx, tx, leagueStr, db.NewAudit(r, nil))
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetLeague")
|
||||
}
|
||||
if league == nil {
|
||||
return false, errors.New("league not found")
|
||||
}
|
||||
|
||||
// Delete the many-to-many relationship
|
||||
err = db.DeleteItem[db.SeasonLeague](tx).
|
||||
Where("season_id = ? AND league_id = ?", season.ID, league.ID).
|
||||
WithAudit(r, audit.Callback()).
|
||||
Delete(ctx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.DeleteItem")
|
||||
return false, errors.Wrap(err, "season.RemoveLeague")
|
||||
}
|
||||
|
||||
// Reload season with updated leagues
|
||||
|
||||
Reference in New Issue
Block a user