极简抖音笔记1 | 青训营笔记

169 阅读2分钟

这是我参与「第三届青训营 -后端场」笔记创作活动的第4篇笔记

经过全组的沟通,选择了极简抖音项目作为本次青训营的最终项目考核。 项目接口如下

  1. 基础接口:视频流接口、用户注册登录、用户信息、投稿接口、发布列表;
  2. 扩展接口I:点赞操作、点赞列表、评论操作、评论列表;
  3. 扩展接口II:关注操作、关注列表、粉丝列表。

我负责评论操作和评论列表部分的代码,本篇笔记只涉及评论操作,评论列表在下一篇笔记中。

DAO层: 包含与数据库相关的数据接口、操作等

type Comment struct {
	gorm.Model
	UserID      int64
	User        User
	CommentText string
	VideoId  int64
	IsDelete bool `gorm:"DEFAULT:false"`
}

func (c *Comment) conn() *gorm.DB {
	db, err := gorm.Open(common.DRIVER, common.DSN)
	if err != nil {
		panic(err)
	}
	db.AutoMigrate(&Comment{})
	return db
}

func (c *Comment) CommentAdd(userid int64, param *dto.CommentActionInput) (User, error) {
	db := c.conn()
	defer db.Close()
	user, err := c.FindVideo(db, param.VideoId)
	if err != nil {
		return user, err
	}

	c.UserID = userid
	c.CommentText = param.CommentText
	c.VideoId = param.VideoId
	err = db.Save(c).Error
	if err != nil {
		return user, err
	}
	return user, nil
}

func (c *Comment) CommentDelte(userid int64, param *dto.CommentActionInput) (User, error) {
	db := c.conn()
	defer db.Close()
	user, err := c.FindVideo(db, param.VideoId)
	if err != nil {
		return user, err
	}
	c.UserID = userid
	c.CommentText = param.CommentText
	c.VideoId = param.VideoId
	c.ID = uint(param.CommentId)
	err = db.Delete(c).Error
	if err != nil {
		return user, err
	}
	return user, nil
}


DTO层: 包含输入输出的数据结构、操作等

type CommentActionInput struct {
	Token       string `json:"token"  form:"token"`
	VideoId     int64  `json:"video_id,omitempty"  form:"video_id"`
	ActionType  string `json:"action_type,omitempty"  form:"action_type"`
	CommentText string `json:"comment_text,omitempty"  form:"comment_text"`
	CommentId   int64  `json:"comment_id,omitempty"  form:"comment_id"`
}


type Comment struct {
	Id         int64  `json:"id,omitempty"`
	User       User   `json:"user"`
	Content    string `json:"content,omitempty"`
	CreateDate string `json:"create_date,omitempty"`
}

type CommentActionResponse struct {
	Response
	Comment Comment `json:"comment,omitempty"`
}
func (u *CommentActionResponse) ResponseError(statusCode int32, statusMsg string) {
	u.Response = Response{StatusCode: statusCode, StatusMsg: statusMsg}
}

Controller层: 总控制、调用

type CommentListResponse struct {
	Response
	CommentList []Comment `json:"comment_list,omitempty"`
}


func CommentAction(c *gin.Context) {
	token := c.Query("token")
	c.JSON(http.StatusOK, token)
	params := &dto.CommentActionInput{}
	out := &dto.CommentActionResponse{}
	if err := params.GetValidParams(c); err != nil {
		out.ResponseError(common.ParamsErr, common.ParamsErrMsg)
		c.JSON(http.StatusOK, out)
		return
	}
	if user, err := CheckToken(params.Token); err == nil {
		//发布评论
		if params.ActionType == "1" {
			//dao写入数据库
			com := &dao.Comment{}
			userObject, err := com.CommentAdd(int64(user.ID), params)
			if err != nil {
				out.ResponseError(common.SqlAddErr, common.SqlAddErrMsg)
				c.JSON(http.StatusOK, out)
				return
			}
			userIdString := fmt.Sprintf("%s#", strconv.Itoa(int(user.Model.ID)))
			isFollow := strings.Contains(userObject.FollowerList, userIdString)
			timeLayout := "01-02"
			createTime := com.CreatedAt.Format(timeLayout)
			//dto组装json,以下省略
			
		}
		//删除评论
		if params.ActionType == "2" {
			//删除数据库中的某些值
			com := &dao.Comment{}
			userObject, err := com.CommentDelte(int64(user.ID), params)
			userIdString := fmt.Sprintf("%s#", strconv.Itoa(int(user.Model.ID)))
			isFollow := strings.Contains(userObject.FollowerList, userIdString)
			timeLayout := "01-02"
			createTime := com.CreatedAt.Format(timeLayout)
			if err != nil {
				out.ResponseError(common.SqlDelErr, common.SqlDelErrMsg)
				c.JSON(http.StatusOK, out)
				return
			}
			//dto组装json,以下省略
		}
		c.JSON(http.StatusOK, out)
		return
	}
	c.JSON(http.StatusOK, out)
	return

}