added teams
This commit is contained in:
105
internal/handlers/teams_new.go
Normal file
105
internal/handlers/teams_new.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"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"
|
||||
teamsview "git.haelnorr.com/h/oslstats/internal/view/teamsview"
|
||||
)
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
func NewTeamSubmit(
|
||||
s *hws.Server,
|
||||
conn *bun.DB,
|
||||
audit *auditlog.Logger,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
getter, ok := validation.ParseFormOrNotify(s, w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
name := getter.String("name").
|
||||
TrimSpace().Required().
|
||||
MaxLength(25).MinLength(3).Value
|
||||
shortname := getter.String("short_name").
|
||||
TrimSpace().Required().
|
||||
MaxLength(3).MinLength(3).Value
|
||||
altShortname := getter.String("alt_short_name").
|
||||
TrimSpace().Required().
|
||||
MaxLength(3).MinLength(3).Value
|
||||
color := getter.String("color").
|
||||
TrimSpace().MaxLength(7).Value
|
||||
if !getter.ValidateAndNotify(s, w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check that short names are different
|
||||
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) {
|
||||
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)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.TeamShortNamesUnique")
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.Insert")
|
||||
}
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if !nameUnique {
|
||||
notify.Warn(s, w, r, "Duplicate Name", "This team name is already taken.", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if !shortNameComboUnique {
|
||||
notify.Warn(s, w, r, "Duplicate Short Names", "This combination of short names is already taken.", nil)
|
||||
return
|
||||
}
|
||||
w.Header().Set("HX-Redirect", "/teams")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
notify.SuccessWithDelay(s, w, r, "Team Created", fmt.Sprintf("Successfully created team: %s", name), nil)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user