diff --git a/pkg/contacts.go b/pkg/contacts.go index 01215c1..9b6cee5 100644 --- a/pkg/contacts.go +++ b/pkg/contacts.go @@ -407,3 +407,36 @@ func (form *contactForm) MustFillFromDatabase(ctx context.Context, conn *Conn, s form.Country, form.Tags)) } + +func ServeEditContactTags(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, "/contacts/"+slug+"/tags"), slug, locale) + if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `select array_to_string(tags, ',') from contact where slug = $1`, form.Slug).Scan(&form.Tags)) { + http.NotFound(w, r) + return + } + mustRenderStandaloneTemplate(w, r, "tags/edit.gohtml", form) +} + +func HandleUpdateContactTags(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, "/contacts/"+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 contact 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 9a8093d..cae758d 100644 --- a/pkg/router.go +++ b/pkg/router.go @@ -20,6 +20,8 @@ func NewRouter(db *Db) http.Handler { companyRouter.POST("/contacts", HandleAddContact) companyRouter.GET("/contacts/:slug", GetContactForm) companyRouter.PUT("/contacts/:slug", HandleUpdateContact) + companyRouter.PUT("/contacts/:slug/tags", HandleUpdateContactTags) + companyRouter.GET("/contacts/:slug/tags/edit", ServeEditContactTags) companyRouter.GET("/products", IndexProducts) companyRouter.POST("/products", HandleAddProduct) companyRouter.GET("/products/:slug", GetProductForm) diff --git a/web/template/contacts/index.gohtml b/web/template/contacts/index.gohtml index fac7646..d4fc4bb 100644 --- a/web/template/contacts/index.gohtml +++ b/web/template/contacts/index.gohtml @@ -51,10 +51,14 @@ {{ .Name }} {{ .Email }} {{ .Phone }} - + {{- range $index, $tag := .Tags }} {{- if gt $index 0 }}, {{ end -}} - {{ . }} + {{ . }} {{- end }}