// Package accountMessageHandler // Path: /internal/handlers/account/messages // File: read.go // ToDo: Remove SQL // add LIMIT/OFFSET in service package accountMessageHandler import ( "bytes" "net/http" "strconv" templateHandlers "synlotto-website/internal/handlers/template" templateHelpers "synlotto-website/internal/helpers/template" errors "synlotto-website/internal/http/error" "synlotto-website/internal/logging" "synlotto-website/internal/platform/bootstrap" "github.com/gin-gonic/gin" "github.com/justinas/nosurf" ) // GET /account/messages // Renders: web/templates/account/messages/index.html func (h *AccountMessageHandlers) List(c *gin.Context) { app := c.MustGet("app").(*bootstrap.App) sm := app.SessionManager userID := mustUserID(c) // 1) Parse page param (default 1) page := 1 if ps := c.Query("page"); ps != "" { if n, err := strconv.Atoi(ps); err == nil && n > 0 { page = n } } pageSize := 20 // 2) Count total for this user (so TotalPages is accurate) totalPages, totalCount := templateHelpers.GetTotalPages( app.DB, "user_messages", "WHERE recipientId = ? AND is_archived = FALSE", []interface{}{userID}, pageSize, ) if page > totalPages { page = totalPages } // 3) Fetch (existing service returns all inbox items) msgsAll, err := h.Svc.ListInbox(userID) if err != nil { logging.Info("❌ list inbox error: %v", err) c.String(http.StatusInternalServerError, "Failed to load messages") return } // 4) Slice in-memory for now (until you add LIMIT/OFFSET in service) start := (page - 1) * pageSize if start > len(msgsAll) { start = len(msgsAll) } end := start + pageSize if end > len(msgsAll) { end = len(msgsAll) } msgs := msgsAll[start:end] // 5) Build context with paging + CSRF + session-driven user meta 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"] = "Messages" ctx["Messages"] = msgs ctx["CurrentPage"] = page ctx["TotalPages"] = totalPages ctx["TotalCount"] = totalCount ctx["PageRange"] = templateHelpers.MakePageRange(1, totalPages) // 6) Render (Buffer to avoid “headers already written” on error tmpl := templateHelpers.LoadTemplateFiles("layout.html", "web/templates/account/messages/index.html") var buf bytes.Buffer if err := tmpl.ExecuteTemplate(&buf, "layout", ctx); err != nil { logging.Info("❌ Template render error: %v", err) c.String(http.StatusInternalServerError, "Error rendering messages page") return } c.Data(http.StatusOK, "text/html; charset=utf-8", buf.Bytes()) } // Renders: web/templates/account/messages/read.html func (h *AccountMessageHandlers) ReadGet(c *gin.Context) { app := c.MustGet("app").(*bootstrap.App) sm := app.SessionManager userID := mustUserID(c) idStr := c.Query("id") id, err := strconv.ParseInt(idStr, 10, 64) if err != nil || id <= 0 { errors.RenderStatus(c, sm, http.StatusNotFound) return } msg, err := h.Svc.GetByID(userID, id) if err != nil || msg == nil { errors.RenderStatus(c, sm, http.StatusNotFound) return } data := templateHandlers.BuildTemplateData(app, c.Writer, c.Request) ctx := templateHelpers.TemplateContext(c.Writer, c.Request, data) ctx["CSRFToken"] = nosurf.Token(c.Request) ctx["Title"] = msg.Subject ctx["Message"] = msg tmpl := templateHelpers.LoadTemplateFiles("layout.html", "web/templates/account/messages/read.html") var buf bytes.Buffer if err := tmpl.ExecuteTemplate(&buf, "layout", ctx); err != nil { logging.Info("❌ Template render error: %v", err) c.String(http.StatusInternalServerError, "Error rendering message") return } c.Data(http.StatusOK, "text/html; charset=utf-8", buf.Bytes()) }