diff --git a/pkg/expenses.go b/pkg/expenses.go index 698f12c..e09a037 100644 --- a/pkg/expenses.go +++ b/pkg/expenses.go @@ -372,3 +372,36 @@ func (form *expenseFilterForm) Parse(r *http.Request) error { form.TagsCondition.FillValue(r) return nil } + +func ServeEditExpenseTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) { + conn := getConn(r) + locale := getLocale(r) + company := getCompany(r) + slug := params[0].Value + form := newTagsForm(companyURI(company, "/expenses/"+slug+"/tags"), slug, locale) + if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `select array_to_string(tags, ',') from expense where slug = $1`, form.Slug).Scan(&form.Tags)) { + http.NotFound(w, r) + return + } + mustRenderStandaloneTemplate(w, r, "tags/edit.gohtml", form) +} + +func HandleUpdateExpenseTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) { + locale := getLocale(r) + conn := getConn(r) + company := getCompany(r) + slug := params[0].Value + form := newTagsForm(companyURI(company, "/expenses/"+slug+"/tags/edit"), slug, locale) + 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 + } + if conn.MustGetText(r.Context(), "", "update expense set tags = $1 where slug = $2 returning slug", form.Tags, form.Slug) == "" { + http.NotFound(w, r) + } + mustRenderStandaloneTemplate(w, r, "tags/view.gohtml", form) +} diff --git a/pkg/invoices.go b/pkg/invoices.go index a20274d..4da3a25 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -1030,13 +1030,15 @@ func handleInvoiceAction(w http.ResponseWriter, r *http.Request, action string, } type tagsForm struct { - Slug string - Tags *TagsField + Action string + Slug string + Tags *TagsField } -func newTagsForm(slug string, locale *Locale) *tagsForm { +func newTagsForm(uri string, slug string, locale *Locale) *tagsForm { return &tagsForm{ - Slug: slug, + Action: uri, + Slug: slug, Tags: &TagsField{ Name: "tags-" + slug, Label: pgettext("input", "Tags", locale), @@ -1055,7 +1057,9 @@ func (form *tagsForm) Parse(r *http.Request) error { func ServeEditInvoiceTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) { conn := getConn(r) locale := getLocale(r) - form := newTagsForm(params[0].Value, locale) + company := getCompany(r) + slug := params[0].Value + form := newTagsForm(companyURI(company, "/invoices/"+slug+"/tags"), slug, locale) if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `select array_to_string(tags, ',') from invoice where slug = $1`, form.Slug).Scan(&form.Tags)) { http.NotFound(w, r) return @@ -1066,7 +1070,9 @@ func ServeEditInvoiceTags(w http.ResponseWriter, r *http.Request, params httprou func HandleUpdateInvoiceTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) { locale := getLocale(r) conn := getConn(r) - form := newTagsForm(params[0].Value, locale) + company := getCompany(r) + slug := params[0].Value + form := newTagsForm(companyURI(company, "/invoices/"+slug+"/tags/edit"), slug, locale) if err := form.Parse(r); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return @@ -1075,8 +1081,7 @@ func HandleUpdateInvoiceTags(w http.ResponseWriter, r *http.Request, params http http.Error(w, err.Error(), http.StatusForbidden) return } - slug := conn.MustGetText(r.Context(), "", "update invoice set tags = $1 where slug = $2 returning slug", form.Tags, form.Slug) - if slug == "" { + if conn.MustGetText(r.Context(), "", "update invoice set tags = $1 where slug = $2 returning slug", form.Tags, form.Slug) == "" { http.NotFound(w, r) } mustRenderStandaloneTemplate(w, r, "tags/view.gohtml", form) diff --git a/pkg/router.go b/pkg/router.go index 9d00f18..a83f61c 100644 --- a/pkg/router.go +++ b/pkg/router.go @@ -38,6 +38,8 @@ func NewRouter(db *Db) http.Handler { companyRouter.POST("/expenses", HandleAddExpense) companyRouter.GET("/expenses/:slug", ServeExpenseForm) companyRouter.PUT("/expenses/:slug", HandleUpdateExpense) + companyRouter.PUT("/expenses/:slug/tags", HandleUpdateExpenseTags) + companyRouter.GET("/expenses/:slug/tags/edit", ServeEditExpenseTags) companyRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { mustRenderMainTemplate(w, r, "dashboard.gohtml", nil) }) diff --git a/web/template/expenses/index.gohtml b/web/template/expenses/index.gohtml index ce33f5e..d5a6b06 100644 --- a/web/template/expenses/index.gohtml +++ b/web/template/expenses/index.gohtml @@ -54,7 +54,11 @@