added season edits
This commit is contained in:
126
internal/handlers/season_edit.go
Normal file
126
internal/handlers/season_edit.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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/permissions"
|
||||
"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 *bun.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seasonStr := r.PathValue("season_short_name")
|
||||
var season *db.Season
|
||||
if ok := db.WithReadTx(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")
|
||||
}
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
if season == nil {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return
|
||||
}
|
||||
renderSafely(seasonsview.EditPage(season), s, r, w)
|
||||
})
|
||||
}
|
||||
|
||||
func SeasonEditSubmit(
|
||||
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")
|
||||
|
||||
getter, ok := validation.ParseFormOrNotify(s, w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
format := timefmt.NewBuilder().
|
||||
DayNumeric2().Slash().
|
||||
MonthNumeric2().Slash().
|
||||
Year4().Build()
|
||||
|
||||
startDate := getter.Time("start_date", format).Required().Value
|
||||
endDate := getter.Time("end_date", format).Value
|
||||
finalsStartDate := getter.Time("finals_start_date", format).Value
|
||||
finalsEndDate := getter.Time("finals_end_date", format).Value
|
||||
|
||||
if !getter.ValidateAndNotify(s, w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
var season *db.Season
|
||||
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 true, nil
|
||||
}
|
||||
|
||||
// Update only the date fields
|
||||
season.StartDate = startDate
|
||||
if !endDate.IsZero() {
|
||||
season.EndDate = bun.NullTime{Time: endDate}
|
||||
} else {
|
||||
season.EndDate = bun.NullTime{}
|
||||
}
|
||||
if !finalsStartDate.IsZero() {
|
||||
season.FinalsStartDate = bun.NullTime{Time: finalsStartDate}
|
||||
} else {
|
||||
season.FinalsStartDate = bun.NullTime{}
|
||||
}
|
||||
if !finalsEndDate.IsZero() {
|
||||
season.FinalsEndDate = bun.NullTime{Time: finalsEndDate}
|
||||
} else {
|
||||
season.FinalsEndDate = bun.NullTime{}
|
||||
}
|
||||
|
||||
err = db.UpdateSeason(ctx, tx, season)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.UpdateSeason")
|
||||
}
|
||||
user := db.CurrentUser(ctx)
|
||||
err = audit.LogSuccess(ctx, tx, user, permissions.SeasonsCreate.String(),
|
||||
"season", season.ID, nil, r)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "audit.LogSuccess")
|
||||
}
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if season == nil {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return
|
||||
}
|
||||
|
||||
notify.Success(s, w, r, "Season Updated", fmt.Sprintf("Successfully updated season: %s", season.Name), nil)
|
||||
w.Header().Set("HX-Redirect", fmt.Sprintf("/seasons/%s", season.ShortName))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user