big ole refactor
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"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/validation"
|
||||
@@ -18,7 +17,6 @@ import (
|
||||
|
||||
func NewTeamPage(
|
||||
s *hws.Server,
|
||||
conn *bun.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
renderSafely(teamsview.NewPage(), s, r, w)
|
||||
@@ -27,8 +25,7 @@ func NewTeamPage(
|
||||
|
||||
func NewTeamSubmit(
|
||||
s *hws.Server,
|
||||
conn *bun.DB,
|
||||
audit *auditlog.Logger,
|
||||
conn *db.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
getter, ok := validation.ParseFormOrNotify(s, w, r)
|
||||
@@ -38,10 +35,10 @@ func NewTeamSubmit(
|
||||
name := getter.String("name").
|
||||
TrimSpace().Required().
|
||||
MaxLength(25).MinLength(3).Value
|
||||
shortname := getter.String("short_name").
|
||||
shortName := getter.String("short_name").
|
||||
TrimSpace().Required().
|
||||
MaxLength(3).MinLength(3).Value
|
||||
altShortname := getter.String("alt_short_name").
|
||||
altShortName := getter.String("alt_short_name").
|
||||
TrimSpace().Required().
|
||||
MaxLength(3).MinLength(3).Value
|
||||
color := getter.String("color").
|
||||
@@ -51,22 +48,21 @@ func NewTeamSubmit(
|
||||
}
|
||||
|
||||
// Check that short names are different
|
||||
if shortname == altShortname {
|
||||
if shortName == altShortName {
|
||||
notify.Warn(s, w, r, "Invalid Short Names", "Short name and alternative short name must be different.", nil)
|
||||
return
|
||||
}
|
||||
|
||||
nameUnique := false
|
||||
shortNameComboUnique := false
|
||||
var team *db.Team
|
||||
if ok := db.WithNotifyTx(s, w, r, conn, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
if ok := conn.WithNotifyTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
nameUnique, err = db.IsUnique(ctx, tx, (*db.Team)(nil), "name", name)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.IsTeamNameUnique")
|
||||
}
|
||||
|
||||
shortNameComboUnique, err = db.TeamShortNamesUnique(ctx, tx, shortname, altShortname)
|
||||
shortNameComboUnique, err = db.TeamShortNamesUnique(ctx, tx, shortName, altShortName)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.TeamShortNamesUnique")
|
||||
}
|
||||
@@ -74,15 +70,9 @@ func NewTeamSubmit(
|
||||
if !nameUnique || !shortNameComboUnique {
|
||||
return true, nil
|
||||
}
|
||||
team = &db.Team{
|
||||
Name: name,
|
||||
ShortName: shortname,
|
||||
AltShortName: altShortname,
|
||||
Color: color,
|
||||
}
|
||||
err = db.Insert(tx, team).WithAudit(r, audit.Callback()).Exec(ctx)
|
||||
_, err = db.NewTeam(ctx, tx, name, shortName, altShortName, color, db.NewAudit(r, nil))
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.Insert")
|
||||
return false, errors.Wrap(err, "db.NewTeam")
|
||||
}
|
||||
return true, nil
|
||||
}); !ok {
|
||||
|
||||
Reference in New Issue
Block a user