79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package accountNotificationHandler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func NewAccountNotificationHandlers(svc NotificationService) *AccountNotificationHandlers {
|
|
return &AccountNotificationHandlers{Svc: svc}
|
|
}
|
|
|
|
// GET /account/notifications
|
|
// Renders: web/templates/account/notifications/index.html
|
|
func (h *AccountNotificationHandlers) List(c *gin.Context) {
|
|
userID := mustUserID(c)
|
|
items, err := h.Svc.List(userID)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "failed to load notifications"})
|
|
return
|
|
}
|
|
c.HTML(http.StatusOK, "account/notifications/index.html", gin.H{
|
|
"title": "Notifications",
|
|
"notifications": items,
|
|
})
|
|
}
|
|
|
|
// --- Optional since you have read.html ---
|
|
|
|
// GET /account/notifications/:id
|
|
// Renders: web/templates/account/notifications/read.html
|
|
func (h *AccountNotificationHandlers) ReadGet(c *gin.Context) {
|
|
userID := mustUserID(c)
|
|
id, err := parseIDParam(c, "id")
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
return
|
|
}
|
|
nt, err := h.Svc.GetByID(userID, id)
|
|
if err != nil || nt == nil {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
return
|
|
}
|
|
c.HTML(http.StatusOK, "account/notifications/read.html", gin.H{
|
|
"title": nt.Title,
|
|
"notification": nt,
|
|
})
|
|
}
|
|
|
|
// --- helpers (same as in messages; consider sharing in a pkg) ---
|
|
|
|
func mustUserID(c *gin.Context) int64 {
|
|
if v, ok := c.Get("userID"); ok {
|
|
if id, ok2 := v.(int64); ok2 {
|
|
return id
|
|
}
|
|
}
|
|
return 1
|
|
}
|
|
|
|
func parseIDParam(c *gin.Context, name string) (int64, error) {
|
|
return atoi64(c.Param(name))
|
|
}
|
|
|
|
func atoi64(s string) (int64, error) {
|
|
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" }
|