102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"git.haelnorr.com/h/oslstats/internal/db"
|
|
"git.haelnorr.com/h/oslstats/internal/notify"
|
|
"git.haelnorr.com/h/oslstats/internal/respond"
|
|
"git.haelnorr.com/h/oslstats/internal/throw"
|
|
"git.haelnorr.com/h/oslstats/internal/validation"
|
|
"git.haelnorr.com/h/oslstats/internal/view/seasonsview"
|
|
"git.haelnorr.com/h/timefmt"
|
|
"github.com/pkg/errors"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
func SeasonEditPage(
|
|
s *hws.Server,
|
|
conn *db.DB,
|
|
) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
seasonStr := r.PathValue("season_short_name")
|
|
var season *db.Season
|
|
var allLeagues []*db.League
|
|
if ok := conn.WithReadTx(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 {
|
|
if db.IsBadRequest(err) {
|
|
throw.NotFound(s, w, r, r.URL.Path)
|
|
return false, 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
|
|
}
|
|
renderSafely(seasonsview.EditPage(season, allLeagues), s, r, w)
|
|
})
|
|
}
|
|
|
|
func SeasonEditSubmit(
|
|
s *hws.Server,
|
|
conn *db.DB,
|
|
) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
seasonStr := r.PathValue("season_short_name")
|
|
|
|
getter, ok := validation.ParseFormOrNotify(s, w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
format := timefmt.NewBuilder().
|
|
DayNumeric2().Slash().
|
|
MonthNumeric2().Slash().
|
|
Year4().Build()
|
|
|
|
version := getter.String("slap_version").
|
|
TrimSpace().Required().AllowedValues([]string{"rebound", "slapshot1"}).Value
|
|
start := getter.Time("start_date", format).Required().Value
|
|
end := getter.Time("end_date", format).Value
|
|
finalsStart := getter.Time("finals_start_date", format).Value
|
|
finalsEnd := getter.Time("finals_end_date", format).Value
|
|
|
|
if !getter.ValidateAndNotify(s, w, r) {
|
|
return
|
|
}
|
|
|
|
var season *db.Season
|
|
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 {
|
|
if db.IsBadRequest(err) {
|
|
respond.NotFound(w, err)
|
|
return false, nil
|
|
}
|
|
return false, errors.Wrap(err, "db.GetSeason")
|
|
}
|
|
err = season.Update(ctx, tx, version, start, end, finalsStart, finalsEnd, db.NewAuditFromRequest(r))
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "season.Update")
|
|
}
|
|
return true, nil
|
|
}); !ok {
|
|
return
|
|
}
|
|
|
|
respond.HXRedirect(w, "/seasons/%s", season.ShortName)
|
|
notify.SuccessWithDelay(s, w, r, "Season Updated", fmt.Sprintf("Successfully updated season: %s", season.Name), nil)
|
|
})
|
|
}
|