Skip to content

Commit

Permalink
代码修改优化
Browse files Browse the repository at this point in the history
  • Loading branch information
hboostDuck committed Aug 30, 2023
1 parent 8b8099d commit 8032a27
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 96 deletions.
87 changes: 55 additions & 32 deletions cmd/api/handler/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand All @@ -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
}

Expand All @@ -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))
}
55 changes: 41 additions & 14 deletions cmd/api/handler/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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
}

Expand All @@ -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))
}
2 changes: 1 addition & 1 deletion cmd/api/rpc/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
61 changes: 35 additions & 26 deletions cmd/comment/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand All @@ -43,42 +48,46 @@ 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
}

// 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
Expand Down
Loading

0 comments on commit 8032a27

Please sign in to comment.