Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] support favorite action and favorite list #37

Merged
merged 4 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions cmd/api/handler/favorite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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/favorite"
"github.com/Tiktok-Lite/kotkit/pkg/log"
"github.com/cloudwego/hertz/pkg/app"
"net/http"
"strconv"
)

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

token := c.Query("token")
videoId := c.Query("video_id")
actionType := c.Query("action_type")

if token == "" {
logger.Errorf("Illegal input: empty token.")
ResponseError(c, http.StatusBadRequest, response.PackFavoriteActionError("token不能为空"))
return
}

if videoId == "" {
logger.Errorf("Illegal input: empty video_id.")
ResponseError(c, http.StatusBadRequest, response.PackFavoriteActionError("video_id不能为空"))
return
}

vid, err := strconv.ParseInt(videoId, 10, 64)
if err != nil {
logger.Errorf("failed to parse video_id: %v", err)
ResponseError(c, http.StatusBadRequest,
response.PackFavoriteActionError("请检查您的输入是否合法"))
return
}

if actionType == "" {
logger.Errorf("Illegal input: empty action_type.")
ResponseError(c, http.StatusBadRequest, response.PackFavoriteActionError("action_type不能为空"))
return
}
action, err := strconv.ParseInt(actionType, 10, 64)
if err != nil {
logger.Errorf("failed to parse action_type: %v", err)
ResponseError(c, http.StatusBadRequest,
response.PackFavoriteActionError("请检查您的输入是否合法"))
return
}

req := &favorite.FavoriteActionRequest{
Token: token,
VideoId: vid,
ActionType: int32(action),
}
resp, err := rpc.FavoriteAction(ctx, req)
if err != nil {
logger.Errorf("error occurs when calling rpc: %v", err)
ResponseError(c, http.StatusInternalServerError, response.PackFavoriteActionError(resp.StatusMsg))
return
}

ResponseSuccess(c, response.PackFavoriteActionSuccess(resp.StatusMsg))
}

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

token := c.Query("token")
userId := c.Query("user_id")

if token == "" {
logger.Errorf("Illegal input: empty token.")
ResponseError(c, http.StatusBadRequest, response.PackFavoriteListError("token不能为空"))
return
}

if userId == "" {
logger.Errorf("Illegal input: empty user_id.")
ResponseError(c, http.StatusBadRequest, response.PackFavoriteListError("user_id不能为空"))
return
}
uid, err := strconv.ParseInt(userId, 10, 64)
if err != nil {
logger.Errorf("failed to parse user_id: %v", err)
ResponseError(c, http.StatusBadRequest,
response.PackFavoriteListError("请检查您的输入是否合法"))
return
}

req := &favorite.FavoriteListRequest{
UserId: uid,
Token: token,
}
resp, err := rpc.FavoriteList(ctx, req)
if err != nil {
logger.Errorf("error occurs when calling rpc: %v", err)
ResponseError(c, http.StatusInternalServerError, response.PackFavoriteListError(*resp.StatusMsg))
return
}

ResponseSuccess(c, response.PackFavoriteListSuccess(resp.VideoList, *resp.StatusMsg))
}
5 changes: 5 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func apiRegister(hz *server.Hertz) {
relation.POST("/action/", handler.RelationAction)
}
douyin.GET("/feed/", handler.Feed)
favorite := douyin.Group("/favorite")
{
favorite.POST("/action/", handler.FavoriteAction)
favorite.GET("/list/", handler.FavoriteList)
}
}
}

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

import (
"context"
"errors"
"fmt"
"github.com/Tiktok-Lite/kotkit/kitex_gen/favorite"
"github.com/Tiktok-Lite/kotkit/kitex_gen/favorite/favoriteservice"
"github.com/Tiktok-Lite/kotkit/pkg/helper/constant"
"github.com/cloudwego/kitex/client"
"github.com/spf13/viper"
)

var (
favoriteClient favoriteservice.Client
)

func InitFavorite(config *viper.Viper) {
favoriteServiceName := config.GetString("server.name")
favoriteServiceAddr := fmt.Sprintf("%s:%d", config.GetString("server.host"), config.GetInt("server.port"))

c, err := favoriteservice.NewClient(favoriteServiceName, client.WithHostPorts(favoriteServiceAddr))
if err != nil {
panic(err)
}

favoriteClient = c
}

func FavoriteAction(ctx context.Context, req *favorite.FavoriteActionRequest) (*favorite.FavoriteActionResponse, error) {
resp, err := favoriteClient.FavoriteAction(ctx, req)
if err != nil {
return resp, err
}
if resp.StatusCode == constant.StatusErrorCode {
return resp, errors.New(resp.StatusMsg)
}

return resp, nil
}

func FavoriteList(ctx context.Context, req *favorite.FavoriteListRequest) (*favorite.FavoriteListResponse, error) {
resp, err := favoriteClient.FavoriteList(ctx, req)
if err != nil {
return resp, err
}
if resp.StatusCode == constant.StatusErrorCode {
return resp, errors.New(*resp.StatusMsg)
}

return resp, nil
}
3 changes: 3 additions & 0 deletions cmd/api/rpc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func InitRPC() {
loginConfig := conf.LoadConfig(constant.DefaultLoginConfigName)
InitLogin(loginConfig)

favoriteConfig := conf.LoadConfig(constant.DefaultFavoriteConfigName)
InitFavorite(favoriteConfig)

relationConfig := conf.LoadConfig(constant.DefaultRelationConfigName)
InitRelation(relationConfig)
}
13 changes: 13 additions & 0 deletions cmd/favorite/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
RUN_NAME="favorite"

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