// Package accountMessageHandler // Path: /internal/handlers/account/messages // File: send.go package accountMessageHandler import ( "net/http" domain "synlotto-website/internal/domain/messages" templateHandlers "synlotto-website/internal/handlers/template" 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/send // Renders: web/templates/account/messages/send.html func (h *AccountMessageHandlers) SendGet(c *gin.Context) { app := c.MustGet("app").(*bootstrap.App) sm := app.SessionManager data := templateHandlers.BuildTemplateData(app, c.Writer, c.Request) ctx := templateHelpers.TemplateContext(c.Writer, c.Request, data) 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/send func (h *AccountMessageHandlers) SendPost(c *gin.Context) { app := c.MustGet("app").(*bootstrap.App) sm := app.SessionManager userID := mustUserID(c) var in domain.CreateMessageInput if err := c.ShouldBind(&in); err != nil { // Re-render form with validation errors data := templateHandlers.BuildTemplateData(app, c.Writer, c.Request) ctx := templateHelpers.TemplateContext(c.Writer, c.Request, data) 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 { 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 } sm.Put(c.Request.Context(), "flash", "Message sent!") // Redirect back to inbox c.Redirect(http.StatusSeeOther, "/account/messages") }