这是我参与「第三届青训营 -后端场」笔记创作活动的第5篇笔记。 接上篇, 经过全组的沟通,选择了极简抖音项目作为本次青训营的最终项目考核。 项目接口如下
- 基础接口:视频流接口、用户注册登录、用户信息、投稿接口、发布列表;
- 扩展接口I:点赞操作、点赞列表、评论操作、评论列表;
- 扩展接口II:关注操作、关注列表、粉丝列表。
我负责评论操作和评论列表部分的代码,本篇笔记只涉及评论列表,评论操作在上一篇笔记中。
DAO层: 包含与数据库相关的数据接口、操作等
type CommentList struct {
gorm.Model
CommentList []Comment `json:"comment_list"` // 评论列表
VideoId int64
}
func (c *Comment) FindCommentList(db *gorm.DB, search *CommentList) (*[]dto.Comment, error) {
var com []dto.Comment
err := db.Where(search.VideoId).Find(&com).Error
if err != nil {
return nil, err
}
fmt.Println("FindCommentList")
fmt.Println(com)
return &com, nil
}
func (c *Comment) VideoCommentList(userid int64, param dto.CommentListRequire) (*[]Comment, User, error) {
db := c.conn()
defer db.Close()
var commentlist []Comment
user, err := c.FindVideo(db, param.VideoId)
c.UserID = userid
c.VideoId = param.VideoId
if err != nil {
return nil, user, err
}
err = db.Model(commentlist).Where("video_id = ?", param.VideoId).Preload("User").Find(&commentlist).Error
if err != nil {
return &commentlist, user, err
}
return &commentlist, user, nil
}
DTO层: 包含输入输出的数据结构、操作等
type CommentListResponse struct {
CommentList []Comment `json:"comment_list"` // 评论列表
Response
}
type CommentListRequire struct {
VideoId int64 `json:"video_id,omitempty" form:"video_id"`
}
func (u *CommentListResponse) ResponseSuccess(CommentList *[]Comment) {
u.StatusCode = common.SuccessCode
u.StatusMsg = "CommentList Success"
u.CommentList = *CommentList
}
func (u *CommentListResponse) ResponseError(statusCode int32, statusMsg string) {
u.Response = Response{StatusCode: statusCode, StatusMsg: statusMsg}
}
Controller层: 总控制、调用
type CommentListResponse struct {
Response
CommentList []Comment `json:"comment_list,omitempty"`
}
func CommentList(c *gin.Context) {
//获取参数,校验
token := c.Query("token")
videoid := c.Query("video_id")
videoidInt, _ := strconv.Atoi(videoid)
out := &dto.CommentListResponse{}
if user, err := CheckToken(token); err == nil {
var com dao.Comment
var params dto.CommentListRequire
com.VideoId = int64(videoidInt)
fmt.Println("com.VideoId:", com.VideoId)
//检查video_id
if _, err := com.VideoIdCheck(com); err == nil {
//text := c.Query("comment_text")
params.VideoId = com.VideoId
commentlist, userObject, err := com.VideoCommentList(int64(user.ID), params)
if err != nil && commentlist == nil {
out.ResponseError(common.SqlFindErr, common.SqlFindErrMsg)
c.JSON(http.StatusOK, out)
return
}
if err != nil && commentlist != nil {
out.ResponseError(common.SqlFindErr, common.SqlFindErrMsg)
c.JSON(http.StatusOK, out)
return
}
userIdString := fmt.Sprintf("%s#", strconv.Itoa(int(user.Model.ID)))
isFollow := strings.Contains(userObject.FollowerList, userIdString)
var outcommentlist []dto.Comment
for _, item := range *commentlist {
timeLayout := "01-02"
createtime := item.Model.CreatedAt.Format(timeLayout)
outcommentlist = append(outcommentlist,
dto.Comment{
Id: int64(item.Model.ID),
User: dto.User{
Id: int64(item.User.Model.ID),
Name: item.User.Name,
FollowCount: item.User.FollowCount,
FollowerCount: item.User.FollowerCount,
IsFollow: isFollow,
},
Content: item.CommentText,
CreateDate: createtime,
})
}
out.ResponseSuccess(&outcommentlist)
c.JSON(http.StatusOK, out)
return
}
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "Video doesn't exist"})
} else {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
}
}