嵌套定义新结构体来满足新的业务

// 原来结构体
type Post struct {
    ID          int64     `json:"id" db:"post_id"`
    AuthorID    int64     `json:"author_id" db:"author_id"`
    CommunityID int64     `json:"community_id" db:"community_id" binding:"required"`
    Title       string    `json:"title" db:"title" binding:"required"`
    Content     string    `json:"content" db:"content" binding:"required"`
    Status      int32     `json:"status" db:"status"`
    CreateTime  time.Time `json:"create_time" db:"create_time"`
}

只能返回用户id等,
现在需要返回用户名以及帖子的详情
可以定义 帖子接口详情的结构体

// ApiPostDetail 帖子接口详情的结构体
type ApiPostDetail struct { // 如果原来定义的结构体不能满足现在的要求,可以嵌套定义接口专用的结构体
    AuthorName       string             `json:"author_name"`
    *Post                               // 嵌入帖子结构体
    *CommunityDetail  // 社区信息
}

Community,CommunityDetail见下

 // Community,CommunityDetail见下
type Community struct {
    ID   int64  `json:"id" db:"community_id"`
    Name string `json:"name" db:"community_name"`
}

type CommunityDetail struct {
    ID           int64     `json:"id" db:"community_id"`
    Name         string    `json:"name" db:"community_name"`
    Introduction string    `json:"introduction,omitempty" db:"introduction"` // omitempty:如果该字段没有的话就不展示了,json的技巧
    CreatTime    time.Time `json:"create_time" db:"create_time"`
}

logic层代码逻辑:

// GetPostById 根据id查看帖子详情数据
func GetPostById(pid int64) (data *models.ApiPostDetail, err error) {
    //data = new(models.ApiPostDetail)
    // 查询并组合接口需要的数据
    postData, err := mysql.GetPostById(pid)
    if err != nil {
        zap.L().Error("mysql.GetPostById() failed", zap.Error(err))
        return
    }
    // 根据作者id查询作者信息
    user, err := mysql.GetUserById(postData.AuthorID)
    if err != nil {
        zap.L().Error("mysql.GetUserById failed", zap.Int64("id", pid), zap.Error(err))
        return
    }
    // 根据社区ID查询社区详细信息
    community, err := mysql.GetCommunityDetailById(postData.CommunityID)
    if err != nil {
        zap.L().Error("mysql.GetCommunityDetailById failed", zap.Int64("id", pid), zap.Error(err))
        return
    }
    // 进行数据拼接,可以进结构体看,community依靠json 的tag就单独分出来了
    data = &models.ApiPostDetail{
        AuthorName:      user.UserName,
        Post:            postData,
        CommunityDetail: community,
    }
    return
}

上面的步骤做完,返回的数据就如下:

{
    "code": 1000,
    "msg": "success",
    "data": {
        "author_name": "livoi",
        "author_id": 8192098293518336,
        "community_id": 1,
        "title": "python web 开发",
        "content": "from the start",
        "status": 0,
        "name": "Go",
        "introduction": "Golang"
    }
}

但是发现,所有数据混杂在一起了,community和user混杂在一起,这不是我想要的。

json分开community, user的解决方案

// ApiPostDetail 帖子接口详情的结构体
type ApiPostDetail struct { // 如果原来定义的结构体不能满足现在的要求,可以嵌套定义接口专用的结构体
    AuthorName       string             `json:"author_name"`
    *Post                               // 嵌入帖子结构体
    *CommunityDetail `json:"community"` // 社区信息
}


这样之后,再返回的数据就是这样了:

{
    "code": 1000,
    "msg": "success",
    "data": {
        "author_name": "livoi",
        "id": 9292369245507584,
        "author_id": 8192098293518336,
        "community_id": 1,
        "title": "python web 开发",
        "content": "from the start",
        "status": 0,
        "create_time": "2021-12-27T23:24:33Z",
        "community": {
            "id": 1,
            "name": "Go",
            "introduction": "Golang",
            "create_time": "2021-12-24T08:10:10Z"
        }
    }
}
分类: go

浙公网安备33011302000604

辽ICP备20003309号