feat(api): add cursor-based pagination for system messages
- Add SystemMessageCursorPageResponse DTO for cursor pagination - Implement GetSystemMessagesByCursor handler method - Add GetByReceiverIDCursor repository method with cursor decoding - Maintain backward compatibility with existing offset-based pagination
This commit is contained in:
@@ -42,6 +42,14 @@ func (h *SystemMessageHandler) GetSystemMessages(c *gin.Context) {
|
||||
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"))
|
||||
|
||||
@@ -62,6 +70,43 @@ func (h *SystemMessageHandler) GetSystemMessages(c *gin.Context) {
|
||||
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.notifyRepo.GetByReceiverIDCursor(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) {
|
||||
|
||||
Reference in New Issue
Block a user