Continued work around getting messages and notifications cleaned up since moving to MySQL and changing to Gin, SCS, NoSurf.

This commit is contained in:
2025-10-29 15:22:05 +00:00
parent 0b2883a494
commit b41e92629b
8 changed files with 402 additions and 109 deletions

View File

@@ -1,98 +1,14 @@
// Package accountMessageHandler
// Path: /internal/handlers/account/messages
// File: list.go
// ToDo: helpers for reading getting messages shouldn't really be here. ---
package accountMessageHandler
import (
"net/http"
"github.com/gin-gonic/gin"
)
// GET /account/messages
// Renders: web/templates/account/messages/index.html
func (h *AccountMessageHandlers) List(c *gin.Context) {
userID := mustUserID(c) // replace with your auth/user extraction
msgs, err := h.Svc.ListInbox(userID)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "failed to load messages"})
return
}
c.HTML(http.StatusOK, "account/messages/index.html", gin.H{
"title": "Messages",
"messages": msgs,
})
}
// 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",
})
}
// POST /account/messages/add
func (h *AccountMessageHandlers) AddPost(c *gin.Context) {
userID := mustUserID(c)
var in 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,
})
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,
})
return
}
// Redirect back to inbox
c.Redirect(http.StatusSeeOther, "/account/messages")
}
// --- Optional extras since you have read.html and archived.html ---
// GET /account/messages/:id
// Renders: web/templates/account/messages/read.html
func (h *AccountMessageHandlers) ReadGet(c *gin.Context) {
userID := mustUserID(c)
id, err := parseIDParam(c, "id")
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
msg, err := h.Svc.GetByID(userID, id)
if err != nil || msg == nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.HTML(http.StatusOK, "account/messages/read.html", gin.H{
"title": msg.Subject,
"message": msg,
})
}
// GET /account/messages/archived
// Renders: web/templates/account/messages/archived.html
func (h *AccountMessageHandlers) ArchivedList(c *gin.Context) {
userID := mustUserID(c)
msgs, err := h.Svc.ListArchived(userID)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "failed to load archived messages"})
return
}
c.HTML(http.StatusOK, "account/messages/archived.html", gin.H{
"title": "Archived Messages",
"messages": msgs,
})
}
// --- helpers ---
func mustUserID(c *gin.Context) int64 {
// Pull from your auth middleware/session. Panic-unsafe alternative:
if v, ok := c.Get("userID"); ok {