From d40244b3d150897f2df86d04f113b1067762990a Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Fri, 3 Dec 2021 16:44:04 -0500 Subject: [PATCH] Added easier setter funcs for flash messages. --- controllers/contact.go | 4 ++-- controllers/login.go | 2 +- controllers/register.go | 2 +- msg/msg.go | 36 ++++++++++++++++++++++++++---------- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/controllers/contact.go b/controllers/contact.go index d53da89..80d6bf5 100644 --- a/controllers/contact.go +++ b/controllers/contact.go @@ -20,7 +20,7 @@ func (a *Contact) Get(c echo.Context) error { } func (a *Contact) Post(c echo.Context) error { - msg.Set(c, msg.Success, "Thank you for contacting us!") - msg.Set(c, msg.Info, "We will respond to you shortly.") + msg.Success(c, "Thank you for contacting us!") + msg.Info(c, "We will respond to you shortly.") return a.Redirect(c, "home") } diff --git a/controllers/login.go b/controllers/login.go index 0a9e8dd..942e5dc 100644 --- a/controllers/login.go +++ b/controllers/login.go @@ -20,6 +20,6 @@ func (l *Login) Get(c echo.Context) error { } func (l *Login) Post(c echo.Context) error { - msg.Set(c, msg.Danger, "Invalid credentials. Please try again.") + msg.Danger(c, "Invalid credentials. Please try again.") return l.Get(c) } diff --git a/controllers/register.go b/controllers/register.go index 05b8552..356c618 100644 --- a/controllers/register.go +++ b/controllers/register.go @@ -20,6 +20,6 @@ func (r *Register) Get(c echo.Context) error { } func (r *Register) Post(c echo.Context) error { - msg.Set(c, msg.Danger, "Registration is currently disabled.") + msg.Danger(c, "Registration is currently disabled.") return r.Get(c) } diff --git a/msg/msg.go b/msg/msg.go index 9cab6ce..3140680 100644 --- a/msg/msg.go +++ b/msg/msg.go @@ -9,23 +9,34 @@ import ( type Type string const ( - Success Type = "success" - Info Type = "info" - Warning Type = "warning" - Danger Type = "danger" + TypeSuccess Type = "success" + TypeInfo Type = "info" + TypeWarning Type = "warning" + TypeDanger Type = "danger" ) -// TODO: Error handling +// TODO: Error handling and cleanup -func getSession(c echo.Context) *sessions.Session { - sess, _ := session.Get("msg", c) - return sess +func Success(c echo.Context, message string) { + Set(c, TypeSuccess, message) +} + +func Info(c echo.Context, message string) { + Set(c, TypeInfo, message) +} + +func Warning(c echo.Context, message string) { + Set(c, TypeWarning, message) +} + +func Danger(c echo.Context, message string) { + Set(c, TypeDanger, message) } // Set adds a new message into the cookie storage. -func Set(c echo.Context, typ Type, value string) { +func Set(c echo.Context, typ Type, message string) { sess := getSession(c) - sess.AddFlash(value, string(typ)) + sess.AddFlash(message, string(typ)) _ = sess.Save(c.Request(), c.Response()) } @@ -47,3 +58,8 @@ func Get(c echo.Context, typ Type) []string { } return nil } + +func getSession(c echo.Context) *sessions.Session { + sess, _ := session.Get("msg", c) + return sess +}