Refactor the processing of the campsite form in a common function
This commit is contained in:
parent
ad161f57b2
commit
186a5fdb38
|
@ -134,46 +134,34 @@ func (page *campsiteIndex) MustRender(w http.ResponseWriter, r *http.Request, us
|
|||
|
||||
func addCampsite(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
||||
f := newCampsiteForm(r.Context(), conn)
|
||||
if err := f.Parse(r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := user.VerifyCSRFToken(r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if !f.Valid(user.Locale) {
|
||||
if !httplib.IsHTMxRequest(r) {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
}
|
||||
f.MustRender(w, r, user, company)
|
||||
return
|
||||
}
|
||||
if _, err := conn.AddCampsite(r.Context(), f.CampsiteType.String(), f.Label.Val); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
processCampsiteForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
||||
_, err := tx.AddCampsite(r.Context(), f.CampsiteType.String(), f.Label.Val)
|
||||
return err
|
||||
})
|
||||
httplib.Redirect(w, r, "/admin/campsites", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func editCampsite(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *campsiteForm) {
|
||||
if err := f.Parse(r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
processCampsiteForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
||||
return tx.EditCampsite(r.Context(), f.ID, f.CampsiteType.String(), f.Label.Val, f.Active.Checked)
|
||||
})
|
||||
httplib.Redirect(w, r, "/admin/campsites", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func processCampsiteForm(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *campsiteForm, act func(ctx context.Context, tx *database.Tx) error) {
|
||||
if ok, err := form.Handle(f, w, r, user); err != nil {
|
||||
return
|
||||
}
|
||||
if err := user.VerifyCSRFToken(r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if !f.Valid(user.Locale) {
|
||||
if !httplib.IsHTMxRequest(r) {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
}
|
||||
} else if !ok {
|
||||
f.MustRender(w, r, user, company)
|
||||
return
|
||||
}
|
||||
if err := conn.EditCampsite(r.Context(), f.ID, f.CampsiteType.String(), f.Label.Val, f.Active.Checked); err != nil {
|
||||
|
||||
tx := conn.MustBegin(r.Context())
|
||||
defer tx.Rollback(r.Context())
|
||||
if err := act(r.Context(), tx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tx.MustCommit(r.Context())
|
||||
httplib.Redirect(w, r, "/admin/campsites", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
|
|
|
@ -11,12 +11,12 @@ import (
|
|||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func (c *Conn) AddCampsite(ctx context.Context, typeSlug string, label string) (int, error) {
|
||||
return c.GetInt(ctx, "select add_campsite($1, $2)", typeSlug, label)
|
||||
func (tx *Tx) AddCampsite(ctx context.Context, typeSlug string, label string) (int, error) {
|
||||
return tx.GetInt(ctx, "select add_campsite($1, $2)", typeSlug, label)
|
||||
}
|
||||
|
||||
func (c *Conn) EditCampsite(ctx context.Context, id int, typeSlug string, label string, active bool) error {
|
||||
_, err := c.Exec(ctx, "select edit_campsite($1, $2, $3, $4)", id, typeSlug, label, active)
|
||||
func (tx *Tx) EditCampsite(ctx context.Context, id int, typeSlug string, label string, active bool) error {
|
||||
_, err := tx.Exec(ctx, "select edit_campsite($1, $2, $3, $4)", id, typeSlug, label, active)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue