Announcement

👇Official Account👇

Welcome to join the group & private message

Article first/tail QR code

Skip to content

Project 3: 文章模块

学习目标

  • 实现文章 CRUD 和搜索功能

核心技术点

功能技术方案
文章存储GORM + MySQL/PostgreSQL
全文搜索Elasticsearch / PostgreSQL tsvector
标签系统多对多关联表
分页游标分页(cursor-based)

游标分页实现

go
type CursorPage struct {
    Cursor  string `json:"cursor" form:"cursor"`
    Limit   int    `json:"limit" form:"limit" binding:"max=100"`
}

func (r *ArticleRepository) List(ctx context.Context, cursor string, limit int) ([]model.Article, string, error) {
    var articles []model.Article
    query := r.db.WithContext(ctx).
        Model(&model.Article{}).
        Order("id DESC").
        Limit(limit + 1)

    if cursor != "" {
        query = query.Where("id < ?", cursor)
    }

    result := query.Find(&articles)

    var nextCursor string
    if len(articles) > limit {
        nextCursor = strconv.Itoa(articles[limit-1].ID)
        articles = articles[:limit]
    }

    return articles, nextCursor, result.Error
}

API 接口

方法路径说明
GET/api/v1/articles文章列表(分页)
POST/api/v1/articles创建文章
GET/api/v1/articles/:id文章详情
PUT/api/v1/articles/:id更新文章
DELETE/api/v1/articles/:id删除文章
GET/api/v1/articles/search全文搜索

上次更新于: