Files
backend/internal/handler/system_message_handler.go

155 lines
4.0 KiB
Go
Raw Normal View History

package handler
import (
"strconv"
"carrot_bbs/internal/cache"
"github.com/gin-gonic/gin"
"carrot_bbs/internal/dto"
"carrot_bbs/internal/pkg/response"
"carrot_bbs/internal/repository"
"carrot_bbs/internal/service"
)
// SystemMessageHandler 系统消息处理器
type SystemMessageHandler struct {
systemMsgService service.SystemMessageService
notifyRepo *repository.SystemNotificationRepository
}
// NewSystemMessageHandler 创建系统消息处理器
func NewSystemMessageHandler(
systemMsgService service.SystemMessageService,
notifyRepo *repository.SystemNotificationRepository,
) *SystemMessageHandler {
return &SystemMessageHandler{
systemMsgService: systemMsgService,
notifyRepo: notifyRepo,
}
}
// GetSystemMessages 获取系统消息列表
// GET /api/v1/messages/system
func (h *SystemMessageHandler) GetSystemMessages(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"))
// 获取当前用户的系统通知(从独立表中获取)
notifications, total, err := h.notifyRepo.GetByReceiverID(userID, page, pageSize)
if err != nil {
response.InternalServerError(c, "failed to get system messages")
return
}
// 转换为响应格式
result := make([]*dto.SystemMessageResponse, 0)
for _, n := range notifications {
resp := dto.SystemNotificationToResponse(n)
result = append(result, resp)
}
response.Paginated(c, result, total, page, pageSize)
}
// GetUnreadCount 获取系统消息未读数
// GET /api/v1/messages/system/unread-count
func (h *SystemMessageHandler) GetUnreadCount(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
// 获取当前用户的未读通知数
unreadCount, err := h.notifyRepo.GetUnreadCount(userID)
if err != nil {
response.InternalServerError(c, "failed to get unread count")
return
}
response.Success(c, &dto.SystemUnreadCountResponse{
UnreadCount: unreadCount,
})
}
// MarkAsRead 标记系统消息为已读
// PUT /api/v1/messages/system/:id/read
func (h *SystemMessageHandler) MarkAsRead(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
notificationIDStr := c.Param("id")
notificationID, err := strconv.ParseInt(notificationIDStr, 10, 64)
if err != nil {
response.BadRequest(c, "invalid notification id")
return
}
// 标记为已读
err = h.notifyRepo.MarkAsRead(notificationID, userID)
if err != nil {
response.InternalServerError(c, "failed to mark as read")
return
}
cache.InvalidateUnreadSystem(cache.GetCache(), userID)
response.SuccessWithMessage(c, "marked as read", nil)
}
// MarkAllAsRead 标记所有系统消息为已读
// PUT /api/v1/messages/system/read-all
func (h *SystemMessageHandler) MarkAllAsRead(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
// 标记当前用户所有通知为已读
err := h.notifyRepo.MarkAllAsRead(userID)
if err != nil {
response.InternalServerError(c, "failed to mark all as read")
return
}
cache.InvalidateUnreadSystem(cache.GetCache(), userID)
response.SuccessWithMessage(c, "all messages marked as read", nil)
}
// DeleteSystemMessage 删除系统消息
// DELETE /api/v1/messages/system/:id
func (h *SystemMessageHandler) DeleteSystemMessage(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
response.Unauthorized(c, "")
return
}
notificationIDStr := c.Param("id")
notificationID, err := strconv.ParseInt(notificationIDStr, 10, 64)
if err != nil {
response.BadRequest(c, "invalid notification id")
return
}
// 删除通知
err = h.notifyRepo.Delete(notificationID, userID)
if err != nil {
response.InternalServerError(c, "failed to delete notification")
return
}
cache.InvalidateUnreadSystem(cache.GetCache(), userID)
response.SuccessWithMessage(c, "notification deleted", nil)
}