diff --git a/pkg/products.go b/pkg/products.go index caeb174..f462d85 100644 --- a/pkg/products.go +++ b/pkg/products.go @@ -352,3 +352,36 @@ func HandleProductSearch(w http.ResponseWriter, r *http.Request, _ httprouter.Pa } mustRenderStandaloneTemplate(w, r, "products/search.gohtml", products) } + +func ServeEditProductTags(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, "/products/"+slug+"/tags"), slug, locale) + if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `select array_to_string(tags, ',') from product where slug = $1`, form.Slug).Scan(&form.Tags)) { + http.NotFound(w, r) + return + } + mustRenderStandaloneTemplate(w, r, "tags/edit.gohtml", form) +} + +func HandleUpdateProductTags(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, "/products/"+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 product 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 a83f61c..9a8093d 100644 --- a/pkg/router.go +++ b/pkg/router.go @@ -24,6 +24,8 @@ func NewRouter(db *Db) http.Handler { companyRouter.POST("/products", HandleAddProduct) companyRouter.GET("/products/:slug", GetProductForm) companyRouter.PUT("/products/:slug", HandleUpdateProduct) + companyRouter.PUT("/products/:slug/tags", HandleUpdateProductTags) + companyRouter.GET("/products/:slug/tags/edit", ServeEditProductTags) companyRouter.GET("/invoices", IndexInvoices) companyRouter.POST("/invoices", HandleAddInvoice) companyRouter.GET("/invoices/:slug", ServeInvoice) diff --git a/web/template/products/index.gohtml b/web/template/products/index.gohtml index 8aa4039..9814eb8 100644 --- a/web/template/products/index.gohtml +++ b/web/template/products/index.gohtml @@ -47,10 +47,14 @@ {{ .Name }} - + {{- range $index, $tag := .Tags }} {{- if gt $index 0 }}, {{ end -}} - {{ . }} + {{ . }} {{- end }} {{ .Price | formatPrice }}