96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"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"
|
|
"github.com/pkg/errors"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
// ForfeitFixture handles POST /fixtures/{fixture_id}/forfeit
|
|
// Creates a finalized forfeit result for the fixture. Requires fixtures.manage permission.
|
|
func ForfeitFixture(
|
|
s *hws.Server,
|
|
conn *db.DB,
|
|
) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fixtureID, err := strconv.Atoi(r.PathValue("fixture_id"))
|
|
if err != nil {
|
|
throw.BadRequest(s, w, r, "Invalid fixture ID", err)
|
|
return
|
|
}
|
|
|
|
getter, ok := validation.ParseFormOrNotify(s, w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
forfeitType := getter.String("forfeit_type").TrimSpace().Required().Value
|
|
forfeitTeam := getter.String("forfeit_team").TrimSpace().Value
|
|
forfeitReason := getter.String("forfeit_reason").TrimSpace().Value
|
|
if !getter.ValidateAndNotify(s, w, r) {
|
|
return
|
|
}
|
|
|
|
// Validate forfeit type
|
|
if forfeitType != db.ForfeitTypeMutual && forfeitType != db.ForfeitTypeOutright {
|
|
notify.Warn(s, w, r, "Invalid Forfeit Type", "Forfeit type must be 'mutual' or 'outright'.", nil)
|
|
return
|
|
}
|
|
|
|
// Validate forfeit team for outright forfeits
|
|
if forfeitType == db.ForfeitTypeOutright {
|
|
if forfeitTeam != "home" && forfeitTeam != "away" {
|
|
notify.Warn(s, w, r, "Missing Team", "An outright forfeit requires specifying which team forfeited.", nil)
|
|
return
|
|
}
|
|
}
|
|
|
|
if ok := conn.WithNotifyTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
|
fixture, err := db.GetFixture(ctx, tx, fixtureID)
|
|
if err != nil {
|
|
if db.IsBadRequest(err) {
|
|
respond.NotFound(w, errors.Wrap(err, "db.GetFixture"))
|
|
return false, nil
|
|
}
|
|
return false, errors.Wrap(err, "db.GetFixture")
|
|
}
|
|
|
|
// Check if a result already exists
|
|
existing, err := db.GetFixtureResult(ctx, tx, fixtureID)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "db.GetFixtureResult")
|
|
}
|
|
if existing != nil {
|
|
notify.Warn(s, w, r, "Result Exists",
|
|
"A result already exists for this fixture. Discard it first to record a forfeit.", nil)
|
|
return false, nil
|
|
}
|
|
|
|
user := db.CurrentUser(ctx)
|
|
|
|
_, err = db.CreateForfeitResult(ctx, tx, fixture, forfeitType, forfeitTeam, forfeitReason, user.ID, db.NewAuditFromRequest(r))
|
|
if err != nil {
|
|
if db.IsBadRequest(err) {
|
|
notify.Warn(s, w, r, "Cannot Forfeit", err.Error(), nil)
|
|
return false, nil
|
|
}
|
|
return false, errors.Wrap(err, "db.CreateForfeitResult")
|
|
}
|
|
return true, nil
|
|
}); !ok {
|
|
return
|
|
}
|
|
|
|
notify.SuccessWithDelay(s, w, r, "Forfeit Recorded", "The forfeit has been recorded and finalized.", nil)
|
|
respond.HXRedirect(w, "/fixtures/%d", fixtureID)
|
|
})
|
|
}
|