Set up project files and add .gitignore to exclude local build/runtime artifacts. Made-with: Cursor
133 lines
3.2 KiB
Go
133 lines
3.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"carrot_bbs/internal/pkg/response"
|
|
"carrot_bbs/internal/service"
|
|
)
|
|
|
|
// NotificationHandler 通知处理器
|
|
type NotificationHandler struct {
|
|
notificationService *service.NotificationService
|
|
}
|
|
|
|
// NewNotificationHandler 创建通知处理器
|
|
func NewNotificationHandler(notificationService *service.NotificationService) *NotificationHandler {
|
|
return &NotificationHandler{
|
|
notificationService: notificationService,
|
|
}
|
|
}
|
|
|
|
// GetNotifications 获取通知列表
|
|
func (h *NotificationHandler) GetNotifications(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
unreadOnly := c.Query("unread_only") == "true"
|
|
|
|
notifications, total, err := h.notificationService.GetByUserID(c.Request.Context(), userID, page, pageSize, unreadOnly)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to get notifications")
|
|
return
|
|
}
|
|
|
|
response.Paginated(c, notifications, total, page, pageSize)
|
|
}
|
|
|
|
// MarkAsRead 标记为已读
|
|
func (h *NotificationHandler) MarkAsRead(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
|
|
err := h.notificationService.MarkAsReadWithUserID(c.Request.Context(), id, userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to mark as read")
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "marked as read", nil)
|
|
}
|
|
|
|
// MarkAllAsRead 标记所有为已读
|
|
func (h *NotificationHandler) MarkAllAsRead(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
err := h.notificationService.MarkAllAsRead(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to mark all as read")
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "all marked as read", nil)
|
|
}
|
|
|
|
// GetUnreadCount 获取未读数量
|
|
func (h *NotificationHandler) GetUnreadCount(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
count, err := h.notificationService.GetUnreadCount(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to get unread count")
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{"count": count})
|
|
}
|
|
|
|
// DeleteNotification 删除通知
|
|
func (h *NotificationHandler) DeleteNotification(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
|
|
err := h.notificationService.DeleteNotification(c.Request.Context(), id, userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to delete notification")
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{"success": true})
|
|
}
|
|
|
|
// ClearAllNotifications 清空所有通知
|
|
func (h *NotificationHandler) ClearAllNotifications(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
err := h.notificationService.ClearAllNotifications(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to clear notifications")
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{"success": true})
|
|
}
|