From 8032a2714d518f28bb7dbebcb701c525ea52b174 Mon Sep 17 00:00:00 2001 From: hxy Date: Wed, 30 Aug 2023 19:02:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/api/handler/comment.go | 87 +++++++++++++++++++++++------------- cmd/api/handler/message.go | 55 +++++++++++++++++------ cmd/api/rpc/comment.go | 2 +- cmd/comment/handler.go | 61 ++++++++++++++----------- cmd/message/handler.go | 51 ++++++++++++--------- config/comment.yml | 2 +- config/message.yml | 2 +- internal/response/comment.go | 15 +++++++ internal/response/message.go | 15 +++++++ pkg/helper/constant/const.go | 8 ++++ 10 files changed, 202 insertions(+), 96 deletions(-) diff --git a/cmd/api/handler/comment.go b/cmd/api/handler/comment.go index d2bb266..6779e6e 100644 --- a/cmd/api/handler/comment.go +++ b/cmd/api/handler/comment.go @@ -5,6 +5,7 @@ import ( "github.com/Tiktok-Lite/kotkit/cmd/api/rpc" "github.com/Tiktok-Lite/kotkit/internal/response" "github.com/Tiktok-Lite/kotkit/kitex_gen/comment" + "github.com/Tiktok-Lite/kotkit/pkg/helper/constant" "github.com/Tiktok-Lite/kotkit/pkg/log" "github.com/cloudwego/hertz/pkg/app" "net/http" @@ -15,13 +16,23 @@ import ( func CommentList(ctx context.Context, c *app.RequestContext) { logger := log.Logger() - videoID := c.Query("video_id") - token := c.Query("token") - id, err := strconv.ParseInt(videoID, 10, 64) + videoId := c.Query("video_id") + if videoId == "" { + logger.Errorf("Illegal input: empty video_id.") + ResponseError(c, http.StatusBadRequest, response.PackCommentListError("video_id不能为空")) + return + } + id, err := strconv.ParseInt(videoId, 10, 64) if err != nil { logger.Errorf("failed to parse video_id: %v", err) - ResponseError(c, http.StatusBadRequest, - response.PackBaseError("请检查您的输入是否合法")) + ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("请检查您的输入是否合法")) + return + } + + token := c.Query("token") + if token == "" { + logger.Errorf("Illegal input: empty token.") + ResponseError(c, http.StatusBadRequest, response.PackCommentListError("token不能为空")) return } req := &comment.DouyinCommentListRequest{ @@ -30,34 +41,46 @@ func CommentList(ctx context.Context, c *app.RequestContext) { } resp, err := rpc.CommentList(ctx, req) if err != nil { - logger.Errorf("failed to call rpc: %v", err) - ResponseError(c, http.StatusInternalServerError, - response.PackBaseError("评论信息获取失败,服务器内部问题")) + logger.Errorf("error occurs when calling rpc: %v", err) + ResponseError(c, http.StatusInternalServerError, response.PackCommentListError(resp.StatusMsg)) return } - - ResponseSuccess(c, response.PackCommentListSuccess(resp.CommentList, "评论信息获取成功")) + ResponseSuccess(c, response.PackCommentListSuccess(resp.CommentList, "评论列表获取成功")) } func CommentAction(ctx context.Context, c *app.RequestContext) { logger := log.Logger() - token := c.Query("token") videoId := c.Query("video_id") - actionType := c.Query("action_type") - + if videoId == "" { + logger.Errorf("Illegal input: empty video_id.") + ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("video_id不能为空")) + return + } id, err := strconv.ParseInt(videoId, 10, 64) if err != nil { logger.Errorf("failed to parse video_id: %v", err) - ResponseError(c, http.StatusBadRequest, - response.PackBaseError("请检查您的输入是否合法")) + ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("请检查video_id是否合法")) + return + } + + token := c.Query("token") + if token == "" { + logger.Errorf("Illegal input: empty token.") + ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("token不能为空")) + return + } + + actionType := c.Query("action_type") + if actionType == "" { + logger.Errorf("Illegal input: empty action_type.") + ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("action_type不能为空")) return } act, err := strconv.Atoi(actionType) if err != nil { - logger.Errorf("failed to parse video_id: %v", err) - ResponseError(c, http.StatusBadRequest, - response.PackBaseError("请检查您的输入是否合法")) + logger.Errorf("failed to parse action_type: %v", err) + ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("请检查您的输入是否合法")) return } @@ -66,37 +89,37 @@ func CommentAction(ctx context.Context, c *app.RequestContext) { VideoId: id, ActionType: int32(act), } - msg := "" - if act == 1 { + if act == constant.PostCommentCode { content := c.Query("comment_text") s := strings.TrimSpace(content) if len(s) == 0 { - ResponseError(c, http.StatusInternalServerError, - response.PackBaseError("获取失败")) + ResponseError(c, http.StatusInternalServerError, response.PackCommentActionError("评论不能为空")) return } req.CommentText = s - msg = "评论成功" - } else { + } + if act == constant.DeleteCommentCode { commentId := c.Query("comment_id") - cid, err := strconv.Atoi(commentId) + if commentId == "" { + logger.Errorf("Illegal input: empty comment_id.") + ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("comment_id不能为空")) + return + } + cid, err := strconv.ParseInt(commentId, 10, 64) if err != nil { - ResponseError(c, http.StatusInternalServerError, - response.PackBaseError("获取失败")) + ResponseError(c, http.StatusInternalServerError, response.PackCommentActionError("请检查comment_id是否合法")) return } - cid64 := int64(cid) - req.CommentId = cid64 - msg = "删除评论成功" + req.CommentId = cid } resp, err := rpc.CommentAction(ctx, req) if err != nil { logger.Errorf("failed to call rpc: %v", err) ResponseError(c, http.StatusInternalServerError, - response.PackBaseError("评论操作失败,服务器内部问题")) + response.PackCommentActionError(resp.StatusMsg)) return } - ResponseSuccess(c, response.PackCommentActionSuccess(resp.Comment, msg)) + ResponseSuccess(c, response.PackCommentActionSuccess(resp.Comment, resp.StatusMsg)) } diff --git a/cmd/api/handler/message.go b/cmd/api/handler/message.go index 32d4031..16025cc 100644 --- a/cmd/api/handler/message.go +++ b/cmd/api/handler/message.go @@ -5,6 +5,7 @@ import ( "github.com/Tiktok-Lite/kotkit/cmd/api/rpc" "github.com/Tiktok-Lite/kotkit/internal/response" "github.com/Tiktok-Lite/kotkit/kitex_gen/message" + "github.com/Tiktok-Lite/kotkit/pkg/helper/constant" "github.com/Tiktok-Lite/kotkit/pkg/log" "github.com/cloudwego/hertz/pkg/app" "net/http" @@ -15,15 +16,27 @@ import ( func Chat(ctx context.Context, c *app.RequestContext) { logger := log.Logger() - toUserId := c.Query("to_user_id") token := c.Query("token") + if token == "" { + logger.Errorf("Illegal input: empty token.") + ResponseError(c, http.StatusBadRequest, response.PackMessageListError("token不能为空")) + return + } + + toUserId := c.Query("to_user_id") + if toUserId == "" { + logger.Errorf("Illegal input: empty to_user_id.") + ResponseError(c, http.StatusBadRequest, response.PackMessageListError("to_user_id不能为空")) + return + } id, err := strconv.ParseInt(toUserId, 10, 64) if err != nil { - logger.Errorf("failed to parse video_id: %v", err) + logger.Errorf("failed to parse to_user_id: %v", err) ResponseError(c, http.StatusBadRequest, - response.PackBaseError("请检查您的输入是否合法")) + response.PackMessageListError("请检查您的输入是否合法")) return } + req := &message.DouyinMessageChatRequest{ Token: token, ToUserId: id, @@ -32,7 +45,7 @@ func Chat(ctx context.Context, c *app.RequestContext) { if err != nil { logger.Errorf("failed to call rpc: %v", err) ResponseError(c, http.StatusInternalServerError, - response.PackBaseError("聊天列表获取失败,服务器内部问题")) + response.PackMessageListError(resp.StatusMsg)) return } @@ -42,48 +55,62 @@ func Chat(ctx context.Context, c *app.RequestContext) { func MessageAction(ctx context.Context, c *app.RequestContext) { logger := log.Logger() + token := c.Query("token") + if token == "" { + logger.Errorf("Illegal input: empty token.") + ResponseError(c, http.StatusBadRequest, response.PackMessageListError("token不能为空")) + return + } + toUserId := c.Query("to_user_id") + if toUserId == "" { + logger.Errorf("Illegal input: empty to_user_id.") + ResponseError(c, http.StatusBadRequest, response.PackMessageListError("to_user_id不能为空")) + return + } id, err := strconv.ParseInt(toUserId, 10, 64) if err != nil { logger.Errorf("failed to parse to_user_id: %v", err) ResponseError(c, http.StatusBadRequest, - response.PackBaseError("请检查您的输入是否合法")) + response.PackMessageListError("请检查您的输入是否合法")) return } actionType := c.Query("action_type") + if actionType == "" { + logger.Errorf("Illegal input: empty action_type.") + ResponseError(c, http.StatusBadRequest, response.PackMessageListError("action_type不能为空")) + return + } act, err := strconv.Atoi(actionType) if err != nil { logger.Errorf("failed to parse action_type: %v", err) - ResponseError(c, http.StatusBadRequest, - response.PackBaseError("请检查您的输入是否合法")) + ResponseError(c, http.StatusBadRequest, response.PackMessageListError("请检查您的输入是否合法")) return } - token := c.Query("token") req := &message.DouyinMessageActionRequest{ Token: token, ToUserId: id, ActionType: int32(act), } - if act == 1 { + if act == constant.PostMessageCode { content := c.Query("content") s := strings.TrimSpace(content) if len(s) == 0 { ResponseError(c, http.StatusInternalServerError, - response.PackBaseError("消息不能为空")) + response.PackMessageListError("消息不能为空")) return } req.Content = s } - - _, err = rpc.MessageAction(ctx, req) + resp, err := rpc.MessageAction(ctx, req) if err != nil { logger.Errorf("failed to call rpc: %v", err) ResponseError(c, http.StatusInternalServerError, - response.PackBaseError("发送消息失败,服务器内部问题")) + response.PackMessageListError(resp.StatusMsg)) return } - ResponseSuccess(c, response.PackMessageActionSuccess("发送成功")) + ResponseSuccess(c, response.PackMessageActionSuccess(resp.StatusMsg)) } diff --git a/cmd/api/rpc/comment.go b/cmd/api/rpc/comment.go index 5016411..030bede 100644 --- a/cmd/api/rpc/comment.go +++ b/cmd/api/rpc/comment.go @@ -36,7 +36,7 @@ func InitComment(config *viper.Viper) { func CommentList(ctx context.Context, req *comment.DouyinCommentListRequest) (*comment.DouyinCommentListResponse, error) { resp, err := commentClient.CommentList(ctx, req) if err != nil { - return nil, err + return resp, err } if resp.StatusCode == constant.StatusErrorCode { return resp, fmt.Errorf(resp.StatusMsg) diff --git a/cmd/comment/handler.go b/cmd/comment/handler.go index 88f7089..2e32a55 100644 --- a/cmd/comment/handler.go +++ b/cmd/comment/handler.go @@ -5,7 +5,6 @@ import ( "github.com/Tiktok-Lite/kotkit/cmd/comment/pack" "github.com/Tiktok-Lite/kotkit/internal/db" "github.com/Tiktok-Lite/kotkit/internal/model" - "github.com/Tiktok-Lite/kotkit/kitex_gen/comment" "github.com/Tiktok-Lite/kotkit/pkg/helper/constant" "github.com/Tiktok-Lite/kotkit/pkg/log" @@ -18,23 +17,29 @@ type CommentServiceImpl struct{} func (s *CommentServiceImpl) CommentAction(ctx context.Context, req *comment.DouyinCommentActionRequest) (resp *comment.DouyinCommentActionResponse, err error) { logger := log.Logger() - res := &comment.DouyinCommentActionResponse{ - StatusCode: constant.StatusOKCode, - StatusMsg: "success", - Comment: nil, - } claims, err := Jwt.ParseToken(req.Token) if err != nil { logger.Errorf("Error occurs when parsing token. %v", err) - res.StatusMsg = "token错误" - return res, err + res := &comment.DouyinCommentActionResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "token 解析错误", + } + return res, nil } - if req.ActionType != 1 && req.ActionType != 2 { - res.StatusCode = constant.StatusErrorCode - res.StatusMsg = "ActionType错误" + if req.ActionType != constant.PostCommentCode && req.ActionType != constant.DeleteCommentCode { + res := &comment.DouyinCommentActionResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "ActionType错误", + Comment: nil, + } return res, nil } - if req.ActionType == 1 { + res := &comment.DouyinCommentActionResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "success", + Comment: nil, + } + if req.ActionType == constant.PostCommentCode { c := model.Comment{ Content: req.CommentText, VideoID: uint(req.VideoId), @@ -43,18 +48,19 @@ func (s *CommentServiceImpl) CommentAction(ctx context.Context, req *comment.Dou err := db.AddComment(&c) if err != nil { logger.Errorf("Error occurs when add comment to database. %v", err) - res.StatusMsg = "评论添加失败" + res.StatusCode = constant.StatusErrorCode + res.StatusMsg = "发布评论失败" } - return res, err } - if req.ActionType == 2 { + if req.ActionType == constant.DeleteCommentCode { err := db.DeleteCommentById(req.CommentId) if err != nil { logger.Errorf("Error occurs when delete comment to database. %v", err) - res.StatusMsg = "评论删除失败" + res.StatusCode = constant.StatusErrorCode + res.StatusMsg = "删除评论失败" } - return res, err + return res, nil } return res, nil @@ -62,23 +68,26 @@ func (s *CommentServiceImpl) CommentAction(ctx context.Context, req *comment.Dou // CommentList implements the CommentServiceImpl interface. func (s *CommentServiceImpl) CommentList(ctx context.Context, req *comment.DouyinCommentListRequest) (resp *comment.DouyinCommentListResponse, err error) { - res := &comment.DouyinCommentListResponse{ - StatusCode: constant.StatusOKCode, - StatusMsg: "成功获取评论", - } _, err = Jwt.ParseToken(req.Token) if err != nil { logger.Errorf("Error occurs when parsing token. %v", err) - res.StatusMsg = "token错误" - return res, err + res := &comment.DouyinCommentListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "token 解析错误", + } + return res, nil + } + res := &comment.DouyinCommentListResponse{ + StatusCode: constant.StatusOKCode, + StatusMsg: "成功获取评论列表", } - comments, err := db.QueryCommentByVideoID(req.VideoId) - if err != nil { logger.Errorf("Error occurs when querying comment list from database. %v", err) - return nil, err + res.StatusCode = constant.StatusErrorCode + res.StatusMsg = "查询评论列表失败" + return res, nil } commentList := pack.CommentList(comments) res.CommentList = commentList diff --git a/cmd/message/handler.go b/cmd/message/handler.go index aa876e7..4532af1 100644 --- a/cmd/message/handler.go +++ b/cmd/message/handler.go @@ -17,22 +17,27 @@ type MessageServiceImpl struct{} // MessageChat implements the MessageServiceImpl interface. func (s *MessageServiceImpl) MessageChat(ctx context.Context, req *message.DouyinMessageChatRequest) (resp *message.DouyinMessageChatResponse, err error) { + parseToken, err := Jwt.ParseToken(req.Token) + if err != nil { + logger.Errorf("Error occurs when parsing token. %v", err) + res := &message.DouyinMessageChatResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "token 解析错误", + } + return res, nil + } + res := &message.DouyinMessageChatResponse{ StatusCode: constant.StatusOKCode, StatusMsg: "success", } - token := req.Token - parseToken, err := Jwt.ParseToken(token) - if err != nil { - logger.Errorf("Error occurs when parsing token. %v", err) - res.StatusMsg = "token错误" - return res, err - } userId := parseToken.Id chat, err := db.QueryMessageList(userId, req.ToUserId) if err != nil { logger.Errorf("Error occurs when querying chat list from database. %v", err) - return nil, err + res.StatusCode = constant.StatusErrorCode + res.StatusMsg = "查询消息列表失败" + return res, nil } chatList := pack.ChatList(chat) res.MessageList = chatList @@ -43,33 +48,37 @@ func (s *MessageServiceImpl) MessageChat(ctx context.Context, req *message.Douyi func (s *MessageServiceImpl) MessageAction(ctx context.Context, req *message.DouyinMessageActionRequest) (resp *message.DouyinMessageActionResponse, err error) { logger := log.Logger() + parseToken, err := Jwt.ParseToken(req.Token) + if err != nil { + logger.Errorf("Error occurs when parsing token. %v", err) + res := &message.DouyinMessageActionResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "token 解析错误", + } + return res, nil + } res := &message.DouyinMessageActionResponse{ StatusCode: constant.StatusOKCode, StatusMsg: "success", } - claims, err := Jwt.ParseToken(req.Token) - if err != nil { - logger.Errorf("Error occurs when parsing token. %v", err) - res.StatusMsg = "token错误" - return res, err - } - if req.ActionType == 1 { + if req.ActionType == constant.PostMessageCode { c := model.Message{ Content: req.Content, ToUserID: uint(req.ToUserId), - FromUserID: uint(claims.Id), + FromUserID: uint(parseToken.Id), CreateTime: new(time.Time).Format("01-02"), } err := db.SendMessage(&c) if err != nil { logger.Errorf("Error occurs when add message to database. %v", err) + res.StatusCode = constant.StatusErrorCode res.StatusMsg = "发送消息失败" + return res, nil } - return res, err - } else { - res.StatusCode = constant.StatusErrorCode - res.StatusMsg = "ActionType错误" - return res, err + return res, nil } + res.StatusCode = constant.StatusErrorCode + res.StatusMsg = "ActionType错误" + return res, nil } diff --git a/config/comment.yml b/config/comment.yml index fbee80f..61b3502 100644 --- a/config/comment.yml +++ b/config/comment.yml @@ -1,4 +1,4 @@ server: name: "comment service" host: 127.0.0.1 - port: 8084 \ No newline at end of file + port: 8086 \ No newline at end of file diff --git a/config/message.yml b/config/message.yml index bd1ecf1..a8a0db5 100644 --- a/config/message.yml +++ b/config/message.yml @@ -1,4 +1,4 @@ server: name: "message service" host: 127.0.0.1 - port: 8085 \ No newline at end of file + port: 8087 \ No newline at end of file diff --git a/internal/response/comment.go b/internal/response/comment.go index 40b3875..4d4a92c 100644 --- a/internal/response/comment.go +++ b/internal/response/comment.go @@ -27,3 +27,18 @@ func PackCommentActionSuccess(comment *comment.Comment, msg string) Comment { Comment: comment, } } +func PackCommentListError(errorMsg string) CommentList { + base := PackBaseError(errorMsg) + return CommentList{ + Base: base, + CommentList: nil, + } +} + +func PackCommentActionError(errorMsg string) Comment { + base := PackBaseError(errorMsg) + return Comment{ + Base: base, + Comment: nil, + } +} diff --git a/internal/response/message.go b/internal/response/message.go index 37c2aa1..bdc454d 100644 --- a/internal/response/message.go +++ b/internal/response/message.go @@ -28,3 +28,18 @@ func PackMessageActionSuccess(msg string) Message { Base: base, } } + +func PackMessageListError(msg string) MessageList { + base := PackBaseSuccess(msg) + return MessageList{ + Base: base, + MessageList: nil, + } +} + +func PackMessageActionError(msg string) Message { + base := PackBaseSuccess(msg) + return Message{ + Base: base, + } +} diff --git a/pkg/helper/constant/const.go b/pkg/helper/constant/const.go index 82a6002..3851689 100644 --- a/pkg/helper/constant/const.go +++ b/pkg/helper/constant/const.go @@ -24,3 +24,11 @@ const ( FavoriteCode = 1 UnFavoriteCode = 2 ) +const ( + PostCommentCode = 1 + DeleteCommentCode = 2 +) + +const ( + PostMessageCode = 1 +)