58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
|
|
"everypublish/server/internal/api/response"
|
|
"everypublish/server/internal/models"
|
|
)
|
|
|
|
// NotificationHandler 站内通知处理器
|
|
type NotificationHandler struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
// List 我的通知 + 未读数
|
|
func (h *NotificationHandler) List(c *gin.Context) {
|
|
q := h.DB.Model(&models.Notification{}).Where("workspace_id = ? and user_id = ?", c.GetUint64("wsid"), c.GetUint64("uid"))
|
|
if k := c.Query("kind"); k != "" {
|
|
q = q.Where("kind = ?", k)
|
|
}
|
|
var unread int64
|
|
q.Where("`read` = ?", false).Count(&unread)
|
|
var list []models.Notification
|
|
q.Order("id desc").Limit(100).Find(&list)
|
|
response.OK(c, gin.H{"list": list, "unread": unread})
|
|
}
|
|
|
|
// Read 标记已读
|
|
func (h *NotificationHandler) Read(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 1001, "参数不合法")
|
|
return
|
|
}
|
|
if err = h.DB.Model(&models.Notification{}).
|
|
Where("id = ? and user_id = ?", id, c.GetUint64("uid")).
|
|
Update("read", true).Error; err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 5000, "更新失败")
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|
|
|
|
// ReadAll 全部已读
|
|
func (h *NotificationHandler) ReadAll(c *gin.Context) {
|
|
if err := h.DB.Model(&models.Notification{}).
|
|
Where("workspace_id = ? and user_id = ?", c.GetUint64("wsid"), c.GetUint64("uid")).
|
|
Update("read", true).Error; err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 5000, "更新失败")
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|