- Refactored repository structures to use interfaces instead of concrete types, enhancing flexibility and testability. - Updated various repository methods to accept interfaces, allowing for better dependency management. - Modified wire generation to accommodate new repository interfaces, ensuring proper service injection throughout the application. - Enhanced handler methods to utilize DTOs for filtering and data handling, improving code clarity and maintainability.
192 lines
4.8 KiB
Go
192 lines
4.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"carrot_bbs/internal/dto"
|
|
"carrot_bbs/internal/pkg/response"
|
|
"carrot_bbs/internal/service"
|
|
)
|
|
|
|
// SystemMessageHandler 系统消息处理器
|
|
type SystemMessageHandler struct {
|
|
systemMsgService service.SystemMessageService
|
|
}
|
|
|
|
// NewSystemMessageHandler 创建系统消息处理器
|
|
func NewSystemMessageHandler(
|
|
systemMsgService service.SystemMessageService,
|
|
) *SystemMessageHandler {
|
|
return &SystemMessageHandler{
|
|
systemMsgService: systemMsgService,
|
|
}
|
|
}
|
|
|
|
// GetSystemMessages 获取系统消息列表
|
|
// GET /api/v1/messages/system
|
|
func (h *SystemMessageHandler) GetSystemMessages(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
// 检查是否使用游标分页
|
|
_, cursorExists := c.GetQuery("cursor")
|
|
if cursorExists {
|
|
h.GetSystemMessagesByCursor(c)
|
|
return
|
|
}
|
|
|
|
// 偏移分页(向后兼容)
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
// 获取当前用户的系统通知
|
|
notifications, total, err := h.systemMsgService.GetNotifications(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)
|
|
}
|
|
|
|
// GetSystemMessagesByCursor 游标分页获取系统消息列表
|
|
func (h *SystemMessageHandler) GetSystemMessagesByCursor(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
// 解析游标分页请求
|
|
cursorStr := c.Query("cursor")
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
// 获取当前用户的系统通知(使用兼容旧版接口)
|
|
notifications, nextCursor, hasMore, err := h.systemMsgService.GetNotificationsByCursor(userID, cursorStr, pageSize)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to get system messages")
|
|
return
|
|
}
|
|
|
|
// 转换为响应格式
|
|
items := make([]*dto.SystemMessageResponse, 0)
|
|
for _, n := range notifications {
|
|
resp := dto.SystemNotificationToResponse(n)
|
|
items = append(items, resp)
|
|
}
|
|
|
|
// 构建游标分页响应
|
|
cursorResp := &dto.SystemMessageCursorPageResponse{
|
|
Items: items,
|
|
NextCursor: nextCursor,
|
|
PrevCursor: "",
|
|
HasMore: hasMore,
|
|
}
|
|
|
|
response.Success(c, cursorResp)
|
|
}
|
|
|
|
// 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.systemMsgService.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.systemMsgService.MarkAsRead(notificationID, userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to mark as read")
|
|
return
|
|
}
|
|
|
|
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.systemMsgService.MarkAllAsRead(userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to mark all as read")
|
|
return
|
|
}
|
|
|
|
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.systemMsgService.DeleteNotification(notificationID, userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to delete notification")
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "notification deleted", nil)
|
|
}
|