added leagues
This commit is contained in:
134
internal/handlers/season_leagues.go
Normal file
134
internal/handlers/season_leagues.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"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"
|
||||
)
|
||||
|
||||
func SeasonAddLeague(
|
||||
s *hws.Server,
|
||||
conn *bun.DB,
|
||||
audit *auditlog.Logger,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seasonStr := r.PathValue("season_short_name")
|
||||
leagueStr := r.PathValue("league_short_name")
|
||||
|
||||
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 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")
|
||||
}
|
||||
|
||||
// Reload season with updated leagues
|
||||
season, err = db.GetSeason(ctx, tx, seasonStr)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetSeason")
|
||||
}
|
||||
|
||||
allLeagues, err = db.GetLeagues(ctx, tx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetLeagues")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
notify.Success(s, w, r, "League Added", "League successfully added to season", nil)
|
||||
renderSafely(seasonsview.LeaguesSection(season, allLeagues), s, r, w)
|
||||
})
|
||||
}
|
||||
|
||||
func SeasonRemoveLeague(
|
||||
s *hws.Server,
|
||||
conn *bun.DB,
|
||||
audit *auditlog.Logger,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seasonStr := r.PathValue("season_short_name")
|
||||
leagueStr := r.PathValue("league_short_name")
|
||||
|
||||
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 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")
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// Reload season with updated leagues
|
||||
season, err = db.GetSeason(ctx, tx, seasonStr)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetSeason")
|
||||
}
|
||||
|
||||
allLeagues, err = db.GetLeagues(ctx, tx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetLeagues")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
notify.Success(s, w, r, "League Removed", "League successfully removed from season", nil)
|
||||
renderSafely(seasonsview.LeaguesSection(season, allLeagues), s, r, w)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user