package handler import ( "net/http" "strconv" "github.com/freefire/jiang13-bbs/middleware" "github.com/gin-gonic/gin" ) // Notifications 获取当前用户的通知列表(分页) func (h *Handlers) Notifications(c *gin.Context) { claims := middleware.CurrentUser(c) page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) size, _ := strconv.Atoi(c.DefaultQuery("size", "20")) list, total, err := h.Notification.List(claims.ID, page, size) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{ "notifications": nonNilSlice(list), "total": total, "page": page, "size": size, }) } // UnreadCount 获取未读通知数 func (h *Handlers) UnreadCount(c *gin.Context) { claims := middleware.CurrentUser(c) count, err := h.Notification.UnreadCount(claims.ID) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"count": count}) } // MarkRead 标记单条通知为已读 func (h *Handlers) MarkRead(c *gin.Context) { claims := middleware.CurrentUser(c) id, err := strconv.ParseUint(c.Param("id"), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "无效的通知 ID"}) return } if err := h.Notification.MarkRead(uint(id), claims.ID); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "已标记为已读"}) } // MarkAllRead 标记所有通知为已读 func (h *Handlers) MarkAllRead(c *gin.Context) { claims := middleware.CurrentUser(c) if err := h.Notification.MarkAllRead(claims.ID); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "已全部标记为已读"}) }