Skip to content

Commit

Permalink
[Feature] 增加评论和消息功能 (#41)
Browse files Browse the repository at this point in the history
* message 和 comment 功能代码提交

* message 和 comment 功能代码提交

* db调用代码修改

* db调用代码修改 etcd修改

* 代码修改优化

---------

Co-authored-by: Shiyuan Ji <[email protected]>
  • Loading branch information
hboostDuck and Centurybbx authored Aug 30, 2023
1 parent ba66a9e commit d9a7cd7
Show file tree
Hide file tree
Showing 39 changed files with 4,484 additions and 0 deletions.
125 changes: 125 additions & 0 deletions cmd/api/handler/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package handler

import (
"context"
"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"
"strconv"
"strings"
)

func CommentList(ctx context.Context, c *app.RequestContext) {
logger := log.Logger()

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.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{
Token: token,
VideoId: id,
}
resp, err := rpc.CommentList(ctx, req)
if err != nil {
logger.Errorf("error occurs when calling rpc: %v", err)
ResponseError(c, http.StatusInternalServerError, response.PackCommentListError(resp.StatusMsg))
return
}
ResponseSuccess(c, response.PackCommentListSuccess(resp.CommentList, "评论列表获取成功"))
}

func CommentAction(ctx context.Context, c *app.RequestContext) {
logger := log.Logger()

videoId := c.Query("video_id")
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.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 action_type: %v", err)
ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("请检查您的输入是否合法"))
return
}

req := &comment.DouyinCommentActionRequest{
Token: token,
VideoId: id,
ActionType: int32(act),
}
if act == constant.PostCommentCode {
content := c.Query("comment_text")
s := strings.TrimSpace(content)
if len(s) == 0 {
ResponseError(c, http.StatusInternalServerError, response.PackCommentActionError("评论不能为空"))
return
}
req.CommentText = s
}
if act == constant.DeleteCommentCode {
commentId := c.Query("comment_id")
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.PackCommentActionError("请检查comment_id是否合法"))
return
}
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.PackCommentActionError(resp.StatusMsg))
return
}

ResponseSuccess(c, response.PackCommentActionSuccess(resp.Comment, resp.StatusMsg))
}
116 changes: 116 additions & 0 deletions cmd/api/handler/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package handler

import (
"context"
"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"
"strconv"
"strings"
)

func Chat(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.PackMessageListError("请检查您的输入是否合法"))
return
}

req := &message.DouyinMessageChatRequest{
Token: token,
ToUserId: id,
}
resp, err := rpc.MessageList(ctx, req)
if err != nil {
logger.Errorf("failed to call rpc: %v", err)
ResponseError(c, http.StatusInternalServerError,
response.PackMessageListError(resp.StatusMsg))
return
}

ResponseSuccess(c, response.PackMessageListSuccess(resp.MessageList, "聊天列表获取成功"))
}

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.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.PackMessageListError("请检查您的输入是否合法"))
return
}

req := &message.DouyinMessageActionRequest{
Token: token,
ToUserId: id,
ActionType: int32(act),
}
if act == constant.PostMessageCode {
content := c.Query("content")
s := strings.TrimSpace(content)
if len(s) == 0 {
ResponseError(c, http.StatusInternalServerError,
response.PackMessageListError("消息不能为空"))
return
}
req.Content = s
}
resp, err := rpc.MessageAction(ctx, req)
if err != nil {
logger.Errorf("failed to call rpc: %v", err)
ResponseError(c, http.StatusInternalServerError,
response.PackMessageListError(resp.StatusMsg))
return
}

ResponseSuccess(c, response.PackMessageActionSuccess(resp.StatusMsg))
}
10 changes: 10 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func apiRegister(hz *server.Hertz) {
favorite.POST("/action/", handler.FavoriteAction)
favorite.GET("/list/", handler.FavoriteList)
}
comment := douyin.Group("/comment")
{
comment.GET("/list/", handler.CommentList)
comment.POST("/action/", handler.CommentAction)
}
message := douyin.Group("/message")
{
message.GET("/chat/", handler.Chat)
message.POST("/action/", handler.MessageAction)
}
}
}

Expand Down
56 changes: 56 additions & 0 deletions cmd/api/rpc/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package rpc

import (
"context"
"fmt"
"github.com/Tiktok-Lite/kotkit/kitex_gen/comment"
"github.com/Tiktok-Lite/kotkit/kitex_gen/comment/commentservice"
"github.com/Tiktok-Lite/kotkit/pkg/etcd"
"github.com/Tiktok-Lite/kotkit/pkg/helper/constant"
"github.com/cloudwego/kitex/client"
"github.com/spf13/viper"
)

var (
commentClient commentservice.Client
)

func InitComment(config *viper.Viper) {
r, err := etcd.Resolver()
if err != nil {
logger.Errorf("Error occurs when creating etcd resolver: %v", err)
panic(err)
}

commentServiceName := config.GetString("server.name")

c, err := commentservice.NewClient(commentServiceName, client.WithResolver(r))

if err != nil {
logger.Errorf("Error occurs when creating login client: %v", err)
panic(err)
}
commentClient = c
}

func CommentList(ctx context.Context, req *comment.DouyinCommentListRequest) (*comment.DouyinCommentListResponse, error) {
resp, err := commentClient.CommentList(ctx, req)
if err != nil {
return resp, err
}
if resp.StatusCode == constant.StatusErrorCode {
return resp, fmt.Errorf(resp.StatusMsg)
}
return resp, nil
}
func CommentAction(ctx context.Context, req *comment.DouyinCommentActionRequest) (*comment.DouyinCommentActionResponse, error) {
resp, err := commentClient.CommentAction(ctx, req)
if err != nil {
return resp, err
}
if resp.StatusCode == constant.StatusErrorCode {
return resp, fmt.Errorf(resp.StatusMsg)
}

return resp, nil
}
7 changes: 7 additions & 0 deletions cmd/api/rpc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ func InitRPC() {

relationConfig := conf.LoadConfig(constant.DefaultRelationConfigName)
InitRelation(relationConfig)

commentConfig := conf.LoadConfig(constant.DefaultCommentConfigName)
InitComment(commentConfig)

messageConfig := conf.LoadConfig(constant.DefaultMessageConfigName)
InitMessage(messageConfig)

}
56 changes: 56 additions & 0 deletions cmd/api/rpc/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package rpc

import (
"context"
"fmt"
"github.com/Tiktok-Lite/kotkit/kitex_gen/message"
"github.com/Tiktok-Lite/kotkit/kitex_gen/message/messageservice"
"github.com/Tiktok-Lite/kotkit/pkg/etcd"
"github.com/Tiktok-Lite/kotkit/pkg/helper/constant"
"github.com/cloudwego/kitex/client"
"github.com/spf13/viper"
)

var (
messageClient messageservice.Client
)

func InitMessage(config *viper.Viper) {
r, err := etcd.Resolver()
if err != nil {
logger.Errorf("Error occurs when creating etcd resolver: %v", err)
panic(err)
}

messageServiceName := config.GetString("server.name")

c, err := messageservice.NewClient(messageServiceName, client.WithResolver(r))

if err != nil {
logger.Errorf("Error occurs when creating login client: %v", err)
panic(err)
}
messageClient = c
}

func MessageList(ctx context.Context, req *message.DouyinMessageChatRequest) (*message.DouyinMessageChatResponse, error) {
resp, err := messageClient.MessageChat(ctx, req)
if err != nil {
return nil, err
}
if resp.StatusCode == constant.StatusErrorCode {
return resp, fmt.Errorf(resp.StatusMsg)
}
return resp, nil
}
func MessageAction(ctx context.Context, req *message.DouyinMessageActionRequest) (*message.DouyinMessageActionResponse, error) {
resp, err := messageClient.MessageAction(ctx, req)
if err != nil {
return resp, err
}
if resp.StatusCode == constant.StatusErrorCode {
return resp, fmt.Errorf(resp.StatusMsg)
}

return resp, nil
}
13 changes: 13 additions & 0 deletions cmd/comment/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
RUN_NAME="comment"

mkdir -p output/bin
cp script/* output/
chmod +x output/bootstrap.sh

if [ "$IS_SYSTEM_TEST_ENV" != "1" ]; then
go build -o output/bin/${RUN_NAME}
else
go test -c -covermode=set -o output/bin/${RUN_NAME} -coverpkg=./...
fi

Loading

0 comments on commit d9a7cd7

Please sign in to comment.