Skip to content

Commit

Permalink
2023/2/15 16:18 小修了一下代码风格, 提升了Response的复用率, 并添加了util.packResponse,用于将…
Browse files Browse the repository at this point in the history
…model打包为Response
  • Loading branch information
ccmail committed Feb 15, 2023
1 parent 168b429 commit 82ed90f
Show file tree
Hide file tree
Showing 39 changed files with 261 additions and 807 deletions.
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions README_cjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

2. ~~video方面的feed()内容~~
3. ~~get请求获取不到token,不清楚原因~~
4. 抓包, 查看访问author的publishlist等页面, 客户端发送的数据
4. 抓包, 查看访问author的publishList等页面, 客户端发送的数据
5. ~~没有对关注人数变更到负数的情况进行拦截~~
6. ~~评论之后feed流视频对应的评论数不会实时更新~~
7. 粉丝列表
Expand Down Expand Up @@ -54,8 +54,7 @@
5. **取消了`PublishList`的鉴权操作**, 业务场景如下:

> 1. 用户未登录, 希望访问某视频作者的个人主页
>
2. 用户已经登录, 希望查看自己的主页
> 2. 用户已经登录, 希望查看自己的主页
因为`publishList`请求时具有`user_id`, 因此暂时, 两个操作可以复用一个方法

Expand Down Expand Up @@ -97,8 +96,8 @@

我感觉合理的做法, 以添加redis为例

- 添加reids: 每次点赞请求写入redis中, 以每次feed流请求, 或者每15分钟, 将累积的点赞请求写入到mysql中
- 不添加redis: 按部就班实现功能, 需要一个东西记录点赞数或者点赞关系, 否则可能会面临大量的sql操作
- 添加redis: 每次点赞请求写入redis中, 以每次feed流请求, 或者每15分钟, 将累积的点赞请求写入到mysql中
- 不添加redis: 按部就班实现功能, 需要一个东西记录点赞数或者点赞关系, 否则可能会面临大量的sql操作

# 合并

Expand Down
24 changes: 12 additions & 12 deletions controller/commentCotroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/gin-gonic/gin"
)

// CommentAction 评论操作控制层
// Comment 评论操作控制层
func Comment(c *gin.Context) {
//1 数据处理
UserId, _ := c.Get("user_id")
Expand Down Expand Up @@ -40,16 +40,16 @@ func Comment(c *gin.Context) {
log.Panicln("controller-Comment: 发表评论失败,")
}

c.JSON(http.StatusOK, common.CommentActionResponse{
c.JSON(http.StatusOK, common.CommentActionBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "发表评论成功",
},
Comment: common.CommentResponse{
Comment: common.CommentResp{
ID: newComment.ID,
Content: newComment.CommentText,
CreateDate: newComment.CreatedAt.Format("01-02"),
User: common.CommenterInfo{
User: common.UserInfoResp{
UserID: commenter.ID,
Username: commenter.Name,
FollowCount: commenter.FollowCount,
Expand All @@ -63,7 +63,7 @@ func Comment(c *gin.Context) {
} else if actionType == "2" { //删除评论
commentIdStr := c.Query("comment_id")
commentId, _ := strconv.ParseInt(commentIdStr, 10, 64)
err := service.DeleteCommentService(uint(videoId), uint(commentId))
err := service.DeleteCommentService(uint(commentId), uint(videoId))

if err != nil {
c.JSON(http.StatusOK, common.BaseResponse{
Expand Down Expand Up @@ -122,9 +122,9 @@ func CommentList(c *gin.Context) {

// log.Println("userID: ", userID, ", videoID: ", videoID)

commentRespionseList, err := service.CommentListService(userID, uint(videoID))
commentResponseList, err := service.CommentListService(userID, uint(videoID))

// log.Println("commentRespionseList: ", commentRespionseList)
// log.Println("commentResponseList: ", commentResponseList)

if err != nil {
c.JSON(http.StatusOK, common.BaseResponse{
Expand All @@ -137,22 +137,22 @@ func CommentList(c *gin.Context) {
}

//响应返回
if len(commentRespionseList) == 0 {
c.JSON(http.StatusOK, common.CommentListResponse{
if len(commentResponseList) == 0 {
c.JSON(http.StatusOK, common.CommentListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "该视频的评论列表为空",
},
CommentList: commentRespionseList,
CommentList: commentResponseList,
})
return
}
c.JSON(http.StatusOK, common.CommentListResponse{
c.JSON(http.StatusOK, common.CommentListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "获取评论列表成功",
},
CommentList: commentRespionseList,
CommentList: commentResponseList,
})

}
6 changes: 3 additions & 3 deletions controller/followerController.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func FollowList(ctx *gin.Context) {
log.Panicln("获取关注列表时失败")
return
}
ctx.JSON(http.StatusOK, common.UserInfoListResp{
ctx.JSON(http.StatusOK, common.UserInfoListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "请求发布列表成功!",
Expand Down Expand Up @@ -126,7 +126,7 @@ func FollowerList(ctx *gin.Context) {
log.Panicln("获取粉丝列表时失败")
return
}
ctx.JSON(http.StatusOK, common.UserInfoListResp{
ctx.JSON(http.StatusOK, common.UserInfoListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "请求粉丝列表成功!",
Expand Down Expand Up @@ -164,7 +164,7 @@ func FriendList(ctx *gin.Context) {
log.Panicln("获取好友列表时失败")
return
}
ctx.JSON(http.StatusOK, common.UserInfoListResp{
ctx.JSON(http.StatusOK, common.UserInfoListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "请求好友列表成功!",
Expand Down
8 changes: 4 additions & 4 deletions controller/likeController.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func Like(c *gin.Context) {
} else if actionType == 2 {
err := service.CancelLikeService(userId, uint(videoId))
if err != nil {
log.Panicln("controller-Like: 取消点赞失败,", err)
c.JSON(http.StatusBadRequest, common.BaseResponse{
StatusCode: 1,
StatusMsg: err.Error(),
})
log.Panicln("controller-Like: 取消点赞失败,", err)
return
}
log.Println("取消点赞成功")
Expand Down Expand Up @@ -83,7 +83,7 @@ func LikeList(c *gin.Context) {
// log.Println("returnList:", returnList)

if err != nil {
c.JSON(http.StatusBadRequest, common.LikeListResponse{
c.JSON(http.StatusBadRequest, common.VideoListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 1,
StatusMsg: "获取喜欢列表失败",
Expand All @@ -94,14 +94,14 @@ func LikeList(c *gin.Context) {
return
} else {
if len(returnList) == 0 {
c.JSON(http.StatusOK, common.LikeListResponse{
c.JSON(http.StatusOK, common.VideoListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "该用户未点赞任何视频",
},
})
}
c.JSON(http.StatusOK, common.LikeListResponse{
c.JSON(http.StatusOK, common.VideoListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "获取喜欢列表成功",
Expand Down
10 changes: 5 additions & 5 deletions controller/messageController.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Message(c *gin.Context) {
log.Panicln("controller-Message: 发送消息失败,解析actionType时出错," + err.Error())
}

message_text := c.Query("content")
messageText := c.Query("content")

if actionType != 1 {
c.JSON(http.StatusOK, common.BaseResponse{
Expand All @@ -47,7 +47,7 @@ func Message(c *gin.Context) {
log.Panicln("controller-Message: 发送消息失败,未知的 actionType: " + actionTypeStr)
}

err = service.SendMessageService(senderID, uint(receiverID), message_text)
err = service.SendMessageService(senderID, uint(receiverID), messageText)
if err != nil {
c.JSON(http.StatusOK, common.BaseResponse{
StatusCode: 1,
Expand All @@ -62,7 +62,7 @@ func Message(c *gin.Context) {

}

// 消息记录控制层
// MessageList 消息记录控制层
func MessageList(c *gin.Context) {
getUserId, _ := c.Get("user_id")
var senderID uint
Expand All @@ -89,7 +89,7 @@ func MessageList(c *gin.Context) {
}

if len(messageResponseList) == 0 {
c.JSON(http.StatusOK, common.MessageListResponse{
c.JSON(http.StatusOK, common.MessageListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "获取消息记录成功,消息历史为空",
Expand All @@ -99,7 +99,7 @@ func MessageList(c *gin.Context) {
return
}

c.JSON(http.StatusOK, common.MessageListResponse{
c.JSON(http.StatusOK, common.MessageListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "获取消息记录成功",
Expand Down
16 changes: 0 additions & 16 deletions controller/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func InitRouter() *gin.Engine {
publishGroup := g.Group("/publish")
{
publishGroup.POST("/action/", middleware.JwtMiddleware(), Publish)
//publishGroup.GET("/list/", middleware.JwtMiddleware(), PublishList)
publishGroup.GET("/list/", middleware.JwtMiddleware(), PublishList)

}
Expand All @@ -49,22 +48,7 @@ func InitRouter() *gin.Engine {
followGroup.GET("/follow/list/", middleware.JwtMiddleware(), FollowList)
followGroup.GET("/follower/list/", middleware.JwtMiddleware(), FollowerList)
followGroup.GET("/friend/list/", middleware.JwtMiddleware(), FriendList)
//followGroup.GET("/follow/list/", FollowList)
}
// // comment路由组
// commentGroup := g.Group("/comment")
// {
// commentGroup.POST("/action/", middleware.JwtMiddleware(), controller.CommentAction)
// commentGroup.GET("/list/", middleware.JwtMiddleware(), controller.CommentList)
// }

// // relation路由组
// relationGroup := g.Group("relation")
// {
// relationGroup.POST("/action/", middleware.JwtMiddleware(), controller.RelationAction)
// relationGroup.GET("/follow/list/", middleware.JwtMiddleware(), controller.FollowList)
// relationGroup.GET("/follower/list/", middleware.JwtMiddleware(), controller.FollowerList)
// }

// message路由组
messageGroup := g.Group("/message")
Expand Down
24 changes: 12 additions & 12 deletions controller/userController.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ func UserRegister(c *gin.Context) {
//3.返回响应
if err != nil {
log.Println("controller-UserRegister: 注册失败: 用户名: ", username, ", 密码: ", password)
c.JSON(http.StatusOK, common.UserRegisterResponse{
c.JSON(http.StatusOK, common.UserSignBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 1,
StatusMsg: err.Error(),
},
UserIdTokenResponse: common.UserIdTokenResponse{},
UserIdTokenResp: common.UserIdTokenResp{},
})
return
}
log.Println("用户", username, "注册成功。")
c.JSON(http.StatusOK, common.UserRegisterResponse{
c.JSON(http.StatusOK, common.UserSignBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "注册成功"},
UserIdTokenResponse: registerResponse,
UserIdTokenResp: registerResponse,
})
}

Expand All @@ -50,23 +50,23 @@ func UserLogin(c *gin.Context) {
// 用户不存在返回对应的错误
if err != nil {
log.Println("controller-UserInfo: 用户登录失败,", err)
c.JSON(http.StatusOK, common.UserLoginResponse{
c.JSON(http.StatusOK, common.UserSignBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 1,
StatusMsg: err.Error(),
},
UserIdTokenResponse: common.UserIdTokenResponse{},
UserIdTokenResp: common.UserIdTokenResp{},
})
return
}

// 用户存在,返回相应的id和token
log.Println("用户", username, "登录成功。")
c.JSON(http.StatusOK, common.UserLoginResponse{
c.JSON(http.StatusOK, common.UserSignBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "登陆成功"},
UserIdTokenResponse: userLoginResponse,
UserIdTokenResp: userLoginResponse,
})
}

Expand All @@ -79,12 +79,12 @@ func UserInfo(c *gin.Context) {
// 用户不存在返回对应的错误
if err != nil {
log.Println("controller-UserInfo: 查找用户信息失败", err)
c.JSON(http.StatusOK, common.UserInfoResponse{
c.JSON(http.StatusOK, common.UserInfoBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 1,
StatusMsg: err.Error(),
},
UserList: common.UserInfoQueryResponse{},
UserInfo: common.UserInfoResp{},
})
return
}
Expand All @@ -97,12 +97,12 @@ func UserInfo(c *gin.Context) {

// 用户存在,返回相应的id和token
log.Println("获取用户id", rawId, "的信息成功。")
c.JSON(http.StatusOK, common.UserInfoResponse{
c.JSON(http.StatusOK, common.UserInfoBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "获取用户信息成功",
},
UserList: userInfoResponse,
UserInfo: userInfoResponse,
})

}
5 changes: 2 additions & 3 deletions controller/videoController.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ func PublishList(ctx *gin.Context) {
StatusCode: 1,
StatusMsg: "获取发布作品详情时失败",
})
//log.Panicln("获取发布作品详情时失败", videoList, err)
return
}
ctx.JSON(http.StatusOK, common.VideoListResponse{
ctx.JSON(http.StatusOK, common.VideoListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "请求发布列表成功!",
Expand All @@ -115,7 +114,7 @@ func Feed(ctx *gin.Context) {
return
}

ctx.JSON(http.StatusOK, common.FeedVideoListResp{
ctx.JSON(http.StatusOK, common.FeedVideoListBaseResp{
BaseResponse: common.BaseResponse{
StatusCode: 0,
StatusMsg: "获取Feed流成功",
Expand Down
Loading

0 comments on commit 82ed90f

Please sign in to comment.