Continued work on messages and notifications.
This commit is contained in:
@@ -7,38 +7,101 @@ package accountMessageHandler
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
domain "synlotto-website/internal/domain/messages"
|
||||
templateHelpers "synlotto-website/internal/helpers/template"
|
||||
|
||||
"synlotto-website/internal/logging"
|
||||
"synlotto-website/internal/models"
|
||||
"synlotto-website/internal/platform/bootstrap"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/justinas/nosurf"
|
||||
)
|
||||
|
||||
// GET /account/messages/add
|
||||
// Renders: web/templates/account/messages/send.html
|
||||
func (h *AccountMessageHandlers) AddGet(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "account/messages/send.html", gin.H{
|
||||
"title": "Send Message",
|
||||
})
|
||||
app := c.MustGet("app").(*bootstrap.App)
|
||||
sm := app.SessionManager
|
||||
|
||||
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, models.TemplateData{})
|
||||
if f := sm.PopString(c.Request.Context(), "flash"); f != "" {
|
||||
ctx["Flash"] = f
|
||||
}
|
||||
ctx["CSRFToken"] = nosurf.Token(c.Request)
|
||||
ctx["Title"] = "Send Message"
|
||||
|
||||
tmpl := templateHelpers.LoadTemplateFiles(
|
||||
"layout.html",
|
||||
"web/templates/account/messages/send.html",
|
||||
)
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
if err := tmpl.ExecuteTemplate(c.Writer, "layout", ctx); err != nil {
|
||||
logging.Info("❌ Template render error: %v", err)
|
||||
c.String(http.StatusInternalServerError, "Error rendering send message page")
|
||||
}
|
||||
}
|
||||
|
||||
// POST /account/messages/add
|
||||
func (h *AccountMessageHandlers) AddPost(c *gin.Context) {
|
||||
app := c.MustGet("app").(*bootstrap.App)
|
||||
sm := app.SessionManager
|
||||
|
||||
userID := mustUserID(c)
|
||||
var in CreateMessageInput
|
||||
|
||||
var in domain.CreateMessageInput
|
||||
if err := c.ShouldBind(&in); err != nil {
|
||||
// Re-render form with validation errors
|
||||
c.HTML(http.StatusBadRequest, "account/messages/send.html", gin.H{
|
||||
"title": "Send Message",
|
||||
"error": "Please correct the errors below.",
|
||||
"form": in,
|
||||
})
|
||||
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, models.TemplateData{})
|
||||
if f := sm.PopString(c.Request.Context(), "flash"); f != "" {
|
||||
ctx["Flash"] = f
|
||||
}
|
||||
ctx["CSRFToken"] = nosurf.Token(c.Request)
|
||||
ctx["Title"] = "Send Message"
|
||||
ctx["Error"] = "Please correct the errors below."
|
||||
ctx["Form"] = in
|
||||
|
||||
tmpl := templateHelpers.LoadTemplateFiles(
|
||||
"layout.html",
|
||||
"web/templates/account/messages/send.html",
|
||||
)
|
||||
|
||||
c.Status(http.StatusBadRequest)
|
||||
if err := tmpl.ExecuteTemplate(c.Writer, "layout", ctx); err != nil {
|
||||
logging.Info("❌ Template render error: %v", err)
|
||||
c.String(http.StatusInternalServerError, "Error rendering send message page")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := h.Svc.Create(userID, in); err != nil {
|
||||
c.HTML(http.StatusInternalServerError, "account/messages/send.html", gin.H{
|
||||
"title": "Send Message",
|
||||
"error": "Could not send message.",
|
||||
"form": in,
|
||||
})
|
||||
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, models.TemplateData{})
|
||||
if f := sm.PopString(c.Request.Context(), "flash"); f != "" {
|
||||
ctx["Flash"] = f
|
||||
}
|
||||
ctx["CSRFToken"] = nosurf.Token(c.Request)
|
||||
ctx["Title"] = "Send Message"
|
||||
ctx["Error"] = "Could not send message."
|
||||
ctx["Form"] = in
|
||||
|
||||
tmpl := templateHelpers.LoadTemplateFiles(
|
||||
"layout.html",
|
||||
"web/templates/account/messages/send.html",
|
||||
)
|
||||
|
||||
c.Status(http.StatusInternalServerError)
|
||||
if err := tmpl.ExecuteTemplate(c.Writer, "layout", ctx); err != nil {
|
||||
logging.Info("❌ Template render error: %v", err)
|
||||
c.String(http.StatusInternalServerError, "Error rendering send message page")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Optional: set a flash message for success (since you already PopString elsewhere)
|
||||
// If you're using scs/v2, Put is available:
|
||||
sm.Put(c.Request.Context(), "flash", "Message sent!")
|
||||
|
||||
// Redirect back to inbox
|
||||
c.Redirect(http.StatusSeeOther, "/account/messages")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user