Add PUT method to expense’s URL and call edit_expense

This commit is contained in:
jordi fita mas 2023-05-05 10:59:35 +02:00
parent 4a9c3748cd
commit 49c41681ce
2 changed files with 30 additions and 0 deletions

View File

@ -237,3 +237,32 @@ func HandleAddExpense(w http.ResponseWriter, r *http.Request, _ httprouter.Param
conn.MustExec(r.Context(), "select add_expense($1, $2, $3, $4, $5, $6, $7)", company.Id, form.InvoiceDate, form.Invoicer, form.InvoiceNumber, form.Amount, taxes, form.Tags) conn.MustExec(r.Context(), "select add_expense($1, $2, $3, $4, $5, $6, $7)", company.Id, form.InvoiceDate, form.Invoicer, form.InvoiceNumber, form.Amount, taxes, form.Tags)
htmxRedirect(w, r, companyURI(company, "/expenses")) htmxRedirect(w, r, companyURI(company, "/expenses"))
} }
func HandleUpdateExpense(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
conn := getConn(r)
locale := getLocale(r)
company := mustGetCompany(r)
form := newExpenseForm(r.Context(), conn, locale, company)
if err := form.Parse(r); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := verifyCsrfTokenValid(r); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
slug := params[0].Value
if !form.Validate() {
if !IsHTMxRequest(r) {
w.WriteHeader(http.StatusUnprocessableEntity)
}
mustRenderEditExpenseForm(w, r, slug, form)
return
}
taxes := mustSliceAtoi(form.Tax.Selected)
if found := conn.MustGetText(r.Context(), "", "select edit_expense($1, $2, $3, $4, $5, $6, $7)", slug, form.InvoiceDate, form.Invoicer, form.InvoiceNumber, form.Amount, taxes, form.Tags); found == "" {
http.NotFound(w, r)
return
}
htmxRedirect(w, r, companyURI(company, "/expenses"))
}

View File

@ -37,6 +37,7 @@ func NewRouter(db *Db) http.Handler {
companyRouter.GET("/expenses", IndexExpenses) companyRouter.GET("/expenses", IndexExpenses)
companyRouter.POST("/expenses", HandleAddExpense) companyRouter.POST("/expenses", HandleAddExpense)
companyRouter.GET("/expenses/:slug", ServeExpenseForm) companyRouter.GET("/expenses/:slug", ServeExpenseForm)
companyRouter.PUT("/expenses/:slug", HandleUpdateExpense)
companyRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { companyRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
mustRenderMainTemplate(w, r, "dashboard.gohtml", nil) mustRenderMainTemplate(w, r, "dashboard.gohtml", nil)
}) })