From bf9dcd1247723c9ed7f699a341c72870f4dd0202 Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Tue, 6 Feb 2024 21:37:06 -0800 Subject: [PATCH] lets-go:9.3 sessions --- snippetbox/cmd/web/handlers.go | 2 ++ snippetbox/cmd/web/helpers.go | 1 + snippetbox/cmd/web/templates.go | 1 + snippetbox/ui/html/base.tmpl | 3 +++ 4 files changed, 7 insertions(+) diff --git a/snippetbox/cmd/web/handlers.go b/snippetbox/cmd/web/handlers.go index 64fa536..bc2736c 100644 --- a/snippetbox/cmd/web/handlers.go +++ b/snippetbox/cmd/web/handlers.go @@ -97,5 +97,7 @@ func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request return } + app.sessionManager.Put(r.Context(), "flash", "Snippet successfully created!") + http.Redirect(w, r, fmt.Sprintf("/snippet/view/%d", id), http.StatusSeeOther) } diff --git a/snippetbox/cmd/web/helpers.go b/snippetbox/cmd/web/helpers.go index 6fb5e0c..342efe7 100644 --- a/snippetbox/cmd/web/helpers.go +++ b/snippetbox/cmd/web/helpers.go @@ -16,6 +16,7 @@ import ( func (app *application )newTemplateData(r *http.Request) templateData { return templateData{ CurrentYear: time.Now().Year(), + Flash: app.sessionManager.PopString(r.Context(), "flash"), } } diff --git a/snippetbox/cmd/web/templates.go b/snippetbox/cmd/web/templates.go index b69cc61..7751f24 100644 --- a/snippetbox/cmd/web/templates.go +++ b/snippetbox/cmd/web/templates.go @@ -13,6 +13,7 @@ type templateData struct { Snippet models.Snippet Snippets []models.Snippet Form any + Flash string } // humanDate ... diff --git a/snippetbox/ui/html/base.tmpl b/snippetbox/ui/html/base.tmpl index 3e0369d..47adb0d 100644 --- a/snippetbox/ui/html/base.tmpl +++ b/snippetbox/ui/html/base.tmpl @@ -14,6 +14,9 @@ {{template "nav" .}}
+ {{with .Flash}} +
{{.}}
+ {{end}} {{template "main" .}}