Compare commits
3 Commits
251080cbe5
...
49c41681ce
Author | SHA1 | Date |
---|---|---|
jordi fita mas | 49c41681ce | |
jordi fita mas | 4a9c3748cd | |
jordi fita mas | 73497eb051 |
|
@ -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)
|
||||
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"))
|
||||
}
|
||||
|
|
|
@ -137,13 +137,16 @@ func HandleUpdateProduct(w http.ResponseWriter, r *http.Request, params httprout
|
|||
}
|
||||
slug := params[0].Value
|
||||
if !form.Validate() {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
if !IsHTMxRequest(r) {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
}
|
||||
mustRenderEditProductForm(w, r, slug, form)
|
||||
return
|
||||
}
|
||||
taxes := mustSliceAtoi(form.Tax.Selected)
|
||||
if ok := conn.MustGetBool(r.Context(), "select edit_product($1, $2, $3, $4, $5, $6)", slug, form.Name, form.Description, form.Price, taxes, form.Tags); !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
htmxRedirect(w, r, companyURI(company, "/products"))
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ func NewRouter(db *Db) http.Handler {
|
|||
companyRouter.GET("/expenses", IndexExpenses)
|
||||
companyRouter.POST("/expenses", HandleAddExpense)
|
||||
companyRouter.GET("/expenses/:slug", ServeExpenseForm)
|
||||
companyRouter.PUT("/expenses/:slug", HandleUpdateExpense)
|
||||
companyRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
mustRenderMainTemplate(w, r, "dashboard.gohtml", nil)
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue