fixed some migration issues and added generics for update and insert
This commit is contained in:
@@ -43,10 +43,7 @@ func NotificationWS(
|
||||
if err != nil {
|
||||
logError(s, "Notification error", errors.Wrap(err, "notifyLoop"))
|
||||
}
|
||||
err = ws.CloseNow()
|
||||
if err != nil {
|
||||
logError(s, "Error closing websocket", errors.Wrap(err, "ws.CloseNow"))
|
||||
}
|
||||
_ = ws.CloseNow()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -71,6 +71,12 @@ func Register(
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.UpdateDiscordToken")
|
||||
}
|
||||
if shouldGrantAdmin(user, cfg.RBAC) {
|
||||
err := ensureUserHasAdminRole(ctx, tx, user)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "ensureUserHasAdminRole")
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"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"
|
||||
@@ -78,36 +77,14 @@ func SeasonEditSubmit(
|
||||
return false, errors.Wrap(err, "db.GetSeason")
|
||||
}
|
||||
if season == nil {
|
||||
return true, nil
|
||||
return false, errors.New("season does not exist")
|
||||
}
|
||||
|
||||
// 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)
|
||||
season.Update(startDate, endDate, finalsStartDate, finalsEndDate)
|
||||
err = db.Update(tx, season).WherePK().
|
||||
Column("start_date", "end_date", "finals_start_date", "finals_end_date").
|
||||
WithAudit(r, audit.Callback()).Exec(ctx)
|
||||
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 false, errors.Wrap(err, "db.Update")
|
||||
}
|
||||
return true, nil
|
||||
}); !ok {
|
||||
@@ -119,8 +96,8 @@ func SeasonEditSubmit(
|
||||
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)
|
||||
notify.SuccessWithDelay(s, w, r, "Season Updated", fmt.Sprintf("Successfully updated season: %s", season.Name), nil)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"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/validation"
|
||||
seasonsview "git.haelnorr.com/h/oslstats/internal/view/seasonsview"
|
||||
"git.haelnorr.com/h/timefmt"
|
||||
@@ -70,15 +69,10 @@ func NewSeasonSubmit(
|
||||
if !nameUnique || !shortNameUnique {
|
||||
return true, nil
|
||||
}
|
||||
season, err = db.NewSeason(ctx, tx, name, shortName, startDate)
|
||||
season = db.NewSeason(name, shortName, startDate)
|
||||
err = db.Insert(tx, season).WithAudit(r, audit.Callback()).Exec(ctx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.NewSeason")
|
||||
}
|
||||
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 false, errors.Wrap(err, "db.Insert")
|
||||
}
|
||||
return true, nil
|
||||
}); !ok {
|
||||
@@ -94,8 +88,8 @@ func NewSeasonSubmit(
|
||||
notify.Warn(s, w, r, "Duplicate Short Name", "This short name is already taken.", nil)
|
||||
return
|
||||
}
|
||||
notify.Success(s, w, r, "Season Created", fmt.Sprintf("Successfully created season: %s", name), nil)
|
||||
w.Header().Set("HX-Redirect", fmt.Sprintf("/seasons/%s", season.ShortName))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
notify.SuccessWithDelay(s, w, r, "Season Created", fmt.Sprintf("Successfully created season: %s", name), nil)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user