mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
34 lines
949 B
Go
34 lines
949 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
conf "github.com/muety/wakapi/config"
|
||
|
"github.com/muety/wakapi/models/view"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
func SetError(r *http.Request, w http.ResponseWriter, message string) {
|
||
|
setMessage(r, w, message, "error")
|
||
|
}
|
||
|
|
||
|
func SetSuccess(r *http.Request, w http.ResponseWriter, message string) {
|
||
|
setMessage(r, w, message, "success")
|
||
|
}
|
||
|
|
||
|
func WithSessionMessages[T view.BasicViewModel](vm T, r *http.Request, w http.ResponseWriter) T {
|
||
|
session, _ := conf.GetSessionStore().Get(r, conf.SessionKeyDefault)
|
||
|
if errors := session.Flashes("error"); len(errors) > 0 {
|
||
|
vm.SetError(errors[0].(string))
|
||
|
}
|
||
|
if successes := session.Flashes("success"); len(successes) > 0 {
|
||
|
vm.SetSuccess(successes[0].(string))
|
||
|
}
|
||
|
session.Save(r, w)
|
||
|
return vm
|
||
|
}
|
||
|
|
||
|
func setMessage(r *http.Request, w http.ResponseWriter, message, key string) {
|
||
|
session, _ := conf.GetSessionStore().Get(r, conf.SessionKeyDefault)
|
||
|
session.AddFlash(message, key)
|
||
|
session.Save(r, w)
|
||
|
}
|