76 lines
2.1 KiB
Go
76 lines
2.1 KiB
Go
package accountNotificationHandler
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
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"
|
||
)
|
||
|
||
// ToDo: functional also in messages needs to come out
|
||
func mustUserID(c *gin.Context) int64 {
|
||
// Pull from your auth middleware/session. Panic-unsafe alternative:
|
||
if v, ok := c.Get("userID"); ok {
|
||
if id, ok2 := v.(int64); ok2 {
|
||
return id
|
||
}
|
||
}
|
||
// Fallback for stubs:
|
||
return 1
|
||
}
|
||
|
||
// ToDo: functional also in messages needs to come out
|
||
func atoi64(s string) (int64, error) {
|
||
// small helper to keep imports focused
|
||
// replace with strconv.ParseInt in real code
|
||
var n int64
|
||
for _, ch := range []byte(s) {
|
||
if ch < '0' || ch > '9' {
|
||
return 0, &strconvNumErr{}
|
||
}
|
||
n = n*10 + int64(ch-'0')
|
||
}
|
||
return n, nil
|
||
}
|
||
|
||
type strconvNumErr struct{}
|
||
|
||
func (e *strconvNumErr) Error() string { return "invalid number" }
|
||
|
||
// GET /account/notifications/:id
|
||
// Renders: web/templates/account/notifications/read.html
|
||
func (h *AccountNotificationHandlers) List(c *gin.Context) {
|
||
app := c.MustGet("app").(*bootstrap.App)
|
||
sm := app.SessionManager
|
||
|
||
userID := mustUserID(c)
|
||
notes, err := h.Svc.List(userID) // or ListAll/ListUnread – use your method name
|
||
if err != nil {
|
||
logging.Info("❌ list notifications error: %v", err)
|
||
c.String(http.StatusInternalServerError, "Failed to load notifications")
|
||
return
|
||
}
|
||
|
||
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"] = "Notifications"
|
||
ctx["Notifications"] = notes
|
||
|
||
tmpl := templateHelpers.LoadTemplateFiles("layout.html", "web/templates/account/notifications/index.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 notifications page")
|
||
}
|
||
}
|