From 1511eeeac4ab28ccbdb1b6b4d489d31eb10e8d9b Mon Sep 17 00:00:00 2001 From: Shiyuan Ji <919745273@qq.com> Date: Mon, 28 Aug 2023 16:51:12 +0800 Subject: [PATCH 1/3] [Feature] support favorite action and favorite list --- cmd/api/handler/favorite.go | 106 +++++ cmd/api/main.go | 5 + cmd/api/rpc/favorite.go | 52 ++ cmd/api/rpc/init.go | 3 + cmd/favorite/build.sh | 13 + cmd/favorite/handler.go | 225 +++++++++ cmd/favorite/kitex_info.yaml | 4 + cmd/favorite/main.go | 46 ++ cmd/favorite/script/bootstrap.sh | 22 + config/favorite.yml | 4 + idl/favorite.proto | 32 ++ internal/db/favorite.go | 94 ++++ internal/db/user.go | 36 ++ internal/db/video.go | 47 ++ internal/response/favorite.go | 42 ++ kitex_gen/favorite/favorite.pb.fast.go | 444 ++++++++++++++++++ kitex_gen/favorite/favorite.pb.go | 427 +++++++++++++++++ kitex_gen/favorite/favoriteservice/client.go | 55 +++ .../favoriteservice/favoriteservice.go | 376 +++++++++++++++ kitex_gen/favorite/favoriteservice/invoker.go | 24 + kitex_gen/favorite/favoriteservice/server.go | 20 + pkg/helper/constant/const.go | 20 +- 22 files changed, 2090 insertions(+), 7 deletions(-) create mode 100644 cmd/api/handler/favorite.go create mode 100644 cmd/api/rpc/favorite.go create mode 100644 cmd/favorite/build.sh create mode 100644 cmd/favorite/handler.go create mode 100644 cmd/favorite/kitex_info.yaml create mode 100644 cmd/favorite/main.go create mode 100644 cmd/favorite/script/bootstrap.sh create mode 100644 config/favorite.yml create mode 100644 idl/favorite.proto create mode 100644 internal/db/favorite.go create mode 100644 internal/response/favorite.go create mode 100644 kitex_gen/favorite/favorite.pb.fast.go create mode 100644 kitex_gen/favorite/favorite.pb.go create mode 100644 kitex_gen/favorite/favoriteservice/client.go create mode 100644 kitex_gen/favorite/favoriteservice/favoriteservice.go create mode 100644 kitex_gen/favorite/favoriteservice/invoker.go create mode 100644 kitex_gen/favorite/favoriteservice/server.go diff --git a/cmd/api/handler/favorite.go b/cmd/api/handler/favorite.go new file mode 100644 index 0000000..4ce0847 --- /dev/null +++ b/cmd/api/handler/favorite.go @@ -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)) +} diff --git a/cmd/api/main.go b/cmd/api/main.go index 5819748..2ca97dd 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -41,6 +41,11 @@ func apiRegister(hz *server.Hertz) { publish.POST("/action/", handler.PublishAction) } douyin.GET("/feed/", handler.Feed) + favorite := douyin.Group("/favorite") + { + favorite.POST("/action/", handler.FavoriteAction) + favorite.GET("/list/", handler.FavoriteList) + } } } diff --git a/cmd/api/rpc/favorite.go b/cmd/api/rpc/favorite.go new file mode 100644 index 0000000..2fbee99 --- /dev/null +++ b/cmd/api/rpc/favorite.go @@ -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 +} diff --git a/cmd/api/rpc/init.go b/cmd/api/rpc/init.go index 94cfa6b..37a3a1b 100644 --- a/cmd/api/rpc/init.go +++ b/cmd/api/rpc/init.go @@ -14,4 +14,7 @@ func InitRPC() { loginConfig := conf.LoadConfig(constant.DefaultLoginConfigName) InitLogin(loginConfig) + + favoriteConfig := conf.LoadConfig(constant.DefaultFavoriteConfigName) + InitFavorite(favoriteConfig) } diff --git a/cmd/favorite/build.sh b/cmd/favorite/build.sh new file mode 100644 index 0000000..fe40017 --- /dev/null +++ b/cmd/favorite/build.sh @@ -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 + diff --git a/cmd/favorite/handler.go b/cmd/favorite/handler.go new file mode 100644 index 0000000..021952e --- /dev/null +++ b/cmd/favorite/handler.go @@ -0,0 +1,225 @@ +package main + +import ( + "context" + "github.com/Tiktok-Lite/kotkit/internal/db" + favorite "github.com/Tiktok-Lite/kotkit/kitex_gen/favorite" + "github.com/Tiktok-Lite/kotkit/pkg/helper/constant" + "github.com/Tiktok-Lite/kotkit/pkg/helper/converter" + "github.com/Tiktok-Lite/kotkit/pkg/oss" +) + +// FavoriteServiceImpl implements the last service interface defined in the IDL. +type FavoriteServiceImpl struct{} + +// FavoriteAction implements the FavoriteServiceImpl interface. +func (s *FavoriteServiceImpl) FavoriteAction(ctx context.Context, req *favorite.FavoriteActionRequest) (*favorite.FavoriteActionResponse, error) { + token := req.Token + claims, err := Jwt.ParseToken(token) + if err != nil { + logger.Errorf("Failed to authenticate due to %v", err) + res := &favorite.FavoriteActionResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "鉴权失败,请检查您的token合法性", + } + return res, nil + } + + if req.ActionType != constant.FavoriteCode && req.ActionType != constant.UnFavoriteCode { + logger.Errorf("Failed to like video due to invalid action type") + res := &favorite.FavoriteActionResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "无效的操作类型", + } + return res, nil + } + + ownerId, err := db.QueryUserIdByVideoId(req.VideoId) + if err != nil { + logger.Errorf("Failed to like video due to %v", err) + res := &favorite.FavoriteActionResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "内部数据库错误", + } + return res, nil + } + + // 点赞操作 + if req.ActionType == constant.FavoriteCode { + err = db.AddLikeVideo(req.VideoId, claims.Id, int64(ownerId)) + if err != nil { + logger.Errorf("Failed to like video due to %v", err) + res := &favorite.FavoriteActionResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "内部数据库错误", + } + return res, nil + } + res := &favorite.FavoriteActionResponse{ + StatusCode: constant.StatusOKCode, + StatusMsg: "点赞成功", + } + return res, nil + } + + // 取消点赞操作 + err = db.DislikeVideo(req.VideoId, claims.Id, int64(ownerId)) + if err != nil { + logger.Errorf("Failed to like video due to %v", err) + res := &favorite.FavoriteActionResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: "内部数据库错误", + } + return res, nil + } + + res := &favorite.FavoriteActionResponse{ + StatusCode: constant.StatusOKCode, + StatusMsg: "取消点赞成功", + } + return res, nil +} + +// FavoriteList implements the FavoriteServiceImpl interface. +func (s *FavoriteServiceImpl) FavoriteList(ctx context.Context, req *favorite.FavoriteListRequest) (*favorite.FavoriteListResponse, error) { + token := req.Token + claims, err := Jwt.ParseToken(token) + if err != nil { + logger.Errorf("Failed to authenticate due to %v", err) + errorMsg := "鉴权失败,请检查您的token合法性" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &errorMsg, + } + return res, nil + } + + ids, err := db.QueryFavoriteVideoIdsByUserId(req.UserId) + if err == nil && ids == nil { + msg := "用户没有喜欢的视频" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusOKCode, + StatusMsg: &msg, + VideoList: nil, + } + return res, nil + } + if err != nil { + logger.Errorf("Failed to query favorite video ids due to %v", err) + errorMsg := "内部数据库错误" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &errorMsg, + } + return res, nil + } + + videos, err := db.QueryVideosByVideoIds(ids) + if err == nil && videos == nil { + msg := "用户没有喜欢的视频" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &msg, + VideoList: nil, + } + return res, nil + } + if err != nil { + logger.Errorf("Failed to query videos by video ids due to %v", err) + errorMsg := "内部数据库错误" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &errorMsg, + } + return res, nil + } + + // 处理videos中的点赞关系和minio中的视频url和封面url + for _, v := range videos { + liked, err := db.QueryVideoLikeRelation(int64(v.ID), claims.Id) + if err != nil { + logger.Errorf("Error occurs when querying video like relation from database. %v", err) + errorMsg := "内部数据库错误,获取视频失败" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &errorMsg, + VideoList: nil, + } + return res, nil + } + v.IsFavorite = liked + + // 获取author头像和背景图 + avatar, err := oss.GetObjectURL(oss.AvatarBucketName, v.Author.Avatar) + if err != nil { + logger.Errorf("Error occurs when getting avatar url from minio. %v", err) + errorMsg := "内部minio数据库错误,获取author头像失败" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &errorMsg, + VideoList: nil, + } + return res, nil + } + v.Author.Avatar = avatar + + bgImg, err := oss.GetObjectURL(oss.BackgroundImageBucketName, v.Author.BackgroundImage) + if err != nil { + logger.Errorf("Error occurs when getting background image url from minio. %v", err) + errorMsg := "内部minio数据库错误,获取author背景图失败" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &errorMsg, + VideoList: nil, + } + return res, nil + } + v.Author.BackgroundImage = bgImg + + playUrl, err := oss.GetObjectURL(oss.VideoBucketName, v.PlayURL) + if err != nil { + logger.Errorf("Error occurs when getting video url from minio. %v", err) + errorMsg := "内部minio数据库错误,获取视频失败" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &errorMsg, + VideoList: nil, + } + return res, nil + } + v.PlayURL = playUrl + + coverUrl, err := oss.GetObjectURL(oss.CoverBucketName, v.CoverURL) + if err != nil { + logger.Errorf("Error occurs when getting cover url from minio. %v", err) + errorMsg := "内部minio数据库错误,获取视频失败" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &errorMsg, + VideoList: nil, + } + return res, nil + } + v.CoverURL = coverUrl + } + + videoListProto, err := converter.ConvertVideoModelListToProto(videos) + if err != nil { + logger.Errorf("Error occurs when converting video lists to proto. %v", err) + errorMsg := "内部转换错误,获取视频失败" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusErrorCode, + StatusMsg: &errorMsg, + VideoList: nil, + } + return res, nil + } + + successMsg := "成功获取视频" + res := &favorite.FavoriteListResponse{ + StatusCode: constant.StatusOKCode, + StatusMsg: &successMsg, + VideoList: videoListProto, + } + return res, nil +} diff --git a/cmd/favorite/kitex_info.yaml b/cmd/favorite/kitex_info.yaml new file mode 100644 index 0000000..017dbfc --- /dev/null +++ b/cmd/favorite/kitex_info.yaml @@ -0,0 +1,4 @@ +kitexinfo: + ServiceName: 'favorite' + ToolVersion: 'v0.6.2' + diff --git a/cmd/favorite/main.go b/cmd/favorite/main.go new file mode 100644 index 0000000..f6bf8c5 --- /dev/null +++ b/cmd/favorite/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "github.com/Tiktok-Lite/kotkit/kitex_gen/favorite/favoriteservice" + "github.com/Tiktok-Lite/kotkit/pkg/conf" + "github.com/Tiktok-Lite/kotkit/pkg/helper/constant" + "github.com/Tiktok-Lite/kotkit/pkg/helper/jwt" + "github.com/Tiktok-Lite/kotkit/pkg/log" + "github.com/cloudwego/kitex/server" + "net" +) + +var ( + logger = log.Logger() + favoriteConfig = conf.LoadConfig(constant.DefaultFavoriteConfigName) + serviceName = favoriteConfig.GetString("server.name") + serviceAddr = fmt.Sprintf("%s:%d", favoriteConfig.GetString("server.host"), favoriteConfig.GetInt("server.port")) + jwtConfig = conf.LoadConfig(constant.DefaultLoginConfigName) + signingKey = jwtConfig.GetString("JWT.signingKey") + Jwt *jwt.JWT +) + +func init() { + Jwt = jwt.NewJWT([]byte(signingKey)) +} + +func main() { + addr, err := net.ResolveTCPAddr("tcp", serviceAddr) + if err != nil { + logger.Errorf("Error occurs when resolving favorite service address: %v", err) + panic(err) + } + + svr := favoriteservice.NewServer(new(FavoriteServiceImpl), + server.WithServiceAddr(addr), + ) + + err = svr.Run() + + if err != nil { + logger.Errorf("Error occurs when running favorite service server: %v", err) + panic(err) + } + logger.Infof("Favorite service server start successfully at %s", serviceAddr) +} diff --git a/cmd/favorite/script/bootstrap.sh b/cmd/favorite/script/bootstrap.sh new file mode 100644 index 0000000..45b3d69 --- /dev/null +++ b/cmd/favorite/script/bootstrap.sh @@ -0,0 +1,22 @@ +#! /usr/bin/env bash +CURDIR=$(cd $(dirname $0); pwd) + +if [ "X$1" != "X" ]; then + RUNTIME_ROOT=$1 +else + RUNTIME_ROOT=${CURDIR} +fi + +export KITEX_RUNTIME_ROOT=$RUNTIME_ROOT +export KITEX_LOG_DIR="$RUNTIME_ROOT/log" + +if [ ! -d "$KITEX_LOG_DIR/app" ]; then + mkdir -p "$KITEX_LOG_DIR/app" +fi + +if [ ! -d "$KITEX_LOG_DIR/rpc" ]; then + mkdir -p "$KITEX_LOG_DIR/rpc" +fi + +exec "$CURDIR/bin/favorite" + diff --git a/config/favorite.yml b/config/favorite.yml new file mode 100644 index 0000000..eeb1d77 --- /dev/null +++ b/config/favorite.yml @@ -0,0 +1,4 @@ +server: + name: "favorite service" + host: 127.0.0.1 + port: 8084 \ No newline at end of file diff --git a/idl/favorite.proto b/idl/favorite.proto new file mode 100644 index 0000000..5ae8cef --- /dev/null +++ b/idl/favorite.proto @@ -0,0 +1,32 @@ +syntax = "proto3"; +option go_package = "favorite"; +package favorite; + +import "idl/video.proto"; + +message FavoriteActionRequest { + string token = 1; // 用户鉴权token + int64 video_id = 2; // 视频id + int32 action_type = 3; // 1-点赞,2-取消点赞 +} + +message FavoriteActionResponse { + int32 status_code = 1; // 状态码,0-成功,其他值-失败 + string status_msg = 2; // 返回状态描述 +} + +message FavoriteListRequest { + int64 user_id = 1; // 用户id + string token = 2; // 用户鉴权token +} + +message FavoriteListResponse { + int32 status_code = 1; // 状态码,0-成功,其他值-失败 + optional string status_msg = 2; // 返回状态描述 + repeated video.Video video_list = 3; // 用户点赞视频列表 +} + +service FavoriteService { + rpc FavoriteAction(FavoriteActionRequest) returns (FavoriteActionResponse) {} + rpc FavoriteList(FavoriteListRequest) returns (FavoriteListResponse) {} +} \ No newline at end of file diff --git a/internal/db/favorite.go b/internal/db/favorite.go new file mode 100644 index 0000000..96cace8 --- /dev/null +++ b/internal/db/favorite.go @@ -0,0 +1,94 @@ +package db + +import ( + "github.com/Tiktok-Lite/kotkit/pkg/log" + "gorm.io/gorm" +) + +func AddLikeVideoRelation(vid, uid int64, tx *gorm.DB) error { + err := tx.Exec("INSERT INTO user_like_videos (video_id, user_id) VALUES (?, ?)", vid, uid).Error + if err != nil { + tx.Rollback() + return err + } + return nil +} + +func DeleteLikeVideoRelation(vid, uid int64, tx *gorm.DB) error { + err := tx.Exec("DELETE FROM user_like_videos WHERE video_id = ? AND user_id = ?", vid, uid).Error + if err != nil { + tx.Rollback() + return err + } + return nil +} + +// AddLikeVideo +// 当点赞时: +// 1. user_like_videos表中插入一条记录 +// 2. video表中的favorite_count字段+1 +// 3. 点赞user表中的favorite_count字段+1 +// 4. 拥有video的user表中的total_favorited字段+1 +func AddLikeVideo(vid, uid, ownerId int64) error { + logger := log.Logger() + + tx := DB().Begin() + err := AddLikeVideoRelation(vid, uid, tx) + if err != nil { + logger.Errorf("Failed to add like video relation due to %v", err) + return err + } + + err = AddVideoFavoriteCountById(vid, tx) + if err != nil { + logger.Errorf("Failed to add video favorite count due to %v", err) + return err + } + + err = AddUserFavoriteCountVideoById(uid, tx) + if err != nil { + logger.Errorf("Failed to add user favorite count due to %v", err) + return err + } + + err = AddUserTotalFavoritedById(ownerId, tx) + if err != nil { + logger.Errorf("Failed to add user total favorited due to %v", err) + return err + } + + tx.Commit() + return nil +} + +// DislikeVideo +// 当取消点赞时: +// 1. user_like_videos表中删除一条记录 +// 2. video表中的favorite_count字段-1 +// 3. 点赞user表中的favorite_count字段-1 +// 4. 拥有video的user表中的total_favorited字段-1 +func DislikeVideo(vid, uid, ownerId int64) error { + tx := DB().Begin() + err := DeleteLikeVideoRelation(vid, uid, tx) + if err != nil { + return err + } + + err = DeleteVideoFavoriteCountById(vid, tx) + if err != nil { + return err + } + + err = DeleteUserFavoriteCountVideoById(uid, tx) + if err != nil { + return err + } + + err = DeleteUserTotalFavoritedById(ownerId, tx) + if err != nil { + return err + } + + tx.Commit() + return nil +} diff --git a/internal/db/user.go b/internal/db/user.go index 8b6a1e5..442e159 100644 --- a/internal/db/user.go +++ b/internal/db/user.go @@ -66,3 +66,39 @@ func QueryUserByRelation(userID, followerID int64) (bool, error) { } return count > 0, nil } + +func AddUserFavoriteCountVideoById(uid int64, tx *gorm.DB) error { + var user model.User + if err := tx.Model(&user).Where("id = ?", uid).UpdateColumn("favorite_count", gorm.Expr("favorite_count + ?", 1)).Error; err != nil { + tx.Rollback() + return errors.New("failed to update favorite_count") + } + return nil +} + +func AddUserTotalFavoritedById(uid int64, tx *gorm.DB) error { + var user model.User + if err := tx.Model(&user).Where("id = ?", uid).UpdateColumn("total_favorited", gorm.Expr("total_favorited + ?", 1)).Error; err != nil { + tx.Rollback() + return errors.New("failed to update total_favorited") + } + return nil +} + +func DeleteUserFavoriteCountVideoById(uid int64, tx *gorm.DB) error { + var user model.User + if err := tx.Model(&user).Where("id = ?", uid).UpdateColumn("favorite_count", gorm.Expr("favorite_count - ?", 1)).Error; err != nil { + tx.Rollback() + return errors.New("failed to update favorite_count") + } + return nil +} + +func DeleteUserTotalFavoritedById(uid int64, tx *gorm.DB) error { + var user model.User + if err := tx.Model(&user).Where("id = ?", uid).UpdateColumn("total_favorited", gorm.Expr("total_favorited - ?", 1)).Error; err != nil { + tx.Rollback() + return errors.New("failed to update total_favorited") + } + return nil +} diff --git a/internal/db/video.go b/internal/db/video.go index f713b8e..e8aeb38 100644 --- a/internal/db/video.go +++ b/internal/db/video.go @@ -58,3 +58,50 @@ func QueryVideoLikeRelation(vid, uid int64) (bool, error) { } return count > 0, nil } + +func AddVideoFavoriteCountById(vid int64, tx *gorm.DB) error { + var video model.Video + if err := tx.Debug().Model(&video).Where("id = ?", vid).UpdateColumn("favorite_count", gorm.Expr("favorite_count + ?", 1)).Error; err != nil { + tx.Rollback() + return err + } + return nil +} + +func DeleteVideoFavoriteCountById(vid int64, tx *gorm.DB) error { + var video model.Video + if err := tx.Debug().Model(&video).Where("id = ?", vid).UpdateColumn("favorite_count", gorm.Expr("favorite_count - ?", 1)).Error; err != nil { + tx.Rollback() + return err + } + return nil +} + +func QueryUserIdByVideoId(vid int64) (uint, error) { + var video model.Video + if err := DB().Debug().Where("id = ?", vid).First(&video).Error; err != nil { + return 0, err + } + return video.UserID, nil +} + +func QueryFavoriteVideoIdsByUserId(uid int64) ([]int64, error) { + var videoIds []int64 + err := DB().Debug().Raw("SELECT video_id FROM user_like_videos WHERE user_id = ?", uid).Scan(&videoIds).Error + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, nil + } else if err != nil { + return nil, err + } + + return videoIds, nil +} + +func QueryVideosByVideoIds(videoIds []int64) ([]*model.Video, error) { + var videos []*model.Video + err := DB().Debug().Preload("Author").Where("id IN ?", videoIds).Find(&videos).Error + if err != nil { + return nil, err + } + return videos, nil +} diff --git a/internal/response/favorite.go b/internal/response/favorite.go new file mode 100644 index 0000000..d536f22 --- /dev/null +++ b/internal/response/favorite.go @@ -0,0 +1,42 @@ +package response + +import "github.com/Tiktok-Lite/kotkit/kitex_gen/video" + +type FavoriteAction struct { + Base +} + +type FavoriteList struct { + Base + VideoList []*video.Video `json:"video_list"` +} + +func PackFavoriteActionError(errorMsg string) FavoriteAction { + base := PackBaseError(errorMsg) + return FavoriteAction{ + Base: base, + } +} + +func PackFavoriteActionSuccess(msg string) FavoriteAction { + base := PackBaseSuccess(msg) + return FavoriteAction{ + Base: base, + } +} + +func PackFavoriteListError(errorMsg string) FavoriteList { + base := PackBaseError(errorMsg) + return FavoriteList{ + Base: base, + VideoList: nil, + } +} + +func PackFavoriteListSuccess(videoList []*video.Video, msg string) FavoriteList { + base := PackBaseSuccess(msg) + return FavoriteList{ + Base: base, + VideoList: videoList, + } +} diff --git a/kitex_gen/favorite/favorite.pb.fast.go b/kitex_gen/favorite/favorite.pb.fast.go new file mode 100644 index 0000000..aebe82d --- /dev/null +++ b/kitex_gen/favorite/favorite.pb.fast.go @@ -0,0 +1,444 @@ +// Code generated by Fastpb v0.0.2. DO NOT EDIT. + +package favorite + +import ( + fmt "fmt" + video "github.com/Tiktok-Lite/kotkit/kitex_gen/video" + fastpb "github.com/cloudwego/fastpb" +) + +var ( + _ = fmt.Errorf + _ = fastpb.Skip +) + +func (x *FavoriteActionRequest) FastRead(buf []byte, _type int8, number int32) (offset int, err error) { + switch number { + case 1: + offset, err = x.fastReadField1(buf, _type) + if err != nil { + goto ReadFieldError + } + case 2: + offset, err = x.fastReadField2(buf, _type) + if err != nil { + goto ReadFieldError + } + case 3: + offset, err = x.fastReadField3(buf, _type) + if err != nil { + goto ReadFieldError + } + default: + offset, err = fastpb.Skip(buf, _type, number) + if err != nil { + goto SkipFieldError + } + } + return offset, nil +SkipFieldError: + return offset, fmt.Errorf("%T cannot parse invalid wire-format data, error: %s", x, err) +ReadFieldError: + return offset, fmt.Errorf("%T read field %d '%s' error: %s", x, number, fieldIDToName_FavoriteActionRequest[number], err) +} + +func (x *FavoriteActionRequest) fastReadField1(buf []byte, _type int8) (offset int, err error) { + x.Token, offset, err = fastpb.ReadString(buf, _type) + return offset, err +} + +func (x *FavoriteActionRequest) fastReadField2(buf []byte, _type int8) (offset int, err error) { + x.VideoId, offset, err = fastpb.ReadInt64(buf, _type) + return offset, err +} + +func (x *FavoriteActionRequest) fastReadField3(buf []byte, _type int8) (offset int, err error) { + x.ActionType, offset, err = fastpb.ReadInt32(buf, _type) + return offset, err +} + +func (x *FavoriteActionResponse) FastRead(buf []byte, _type int8, number int32) (offset int, err error) { + switch number { + case 1: + offset, err = x.fastReadField1(buf, _type) + if err != nil { + goto ReadFieldError + } + case 2: + offset, err = x.fastReadField2(buf, _type) + if err != nil { + goto ReadFieldError + } + default: + offset, err = fastpb.Skip(buf, _type, number) + if err != nil { + goto SkipFieldError + } + } + return offset, nil +SkipFieldError: + return offset, fmt.Errorf("%T cannot parse invalid wire-format data, error: %s", x, err) +ReadFieldError: + return offset, fmt.Errorf("%T read field %d '%s' error: %s", x, number, fieldIDToName_FavoriteActionResponse[number], err) +} + +func (x *FavoriteActionResponse) fastReadField1(buf []byte, _type int8) (offset int, err error) { + x.StatusCode, offset, err = fastpb.ReadInt32(buf, _type) + return offset, err +} + +func (x *FavoriteActionResponse) fastReadField2(buf []byte, _type int8) (offset int, err error) { + x.StatusMsg, offset, err = fastpb.ReadString(buf, _type) + return offset, err +} + +func (x *FavoriteListRequest) FastRead(buf []byte, _type int8, number int32) (offset int, err error) { + switch number { + case 1: + offset, err = x.fastReadField1(buf, _type) + if err != nil { + goto ReadFieldError + } + case 2: + offset, err = x.fastReadField2(buf, _type) + if err != nil { + goto ReadFieldError + } + default: + offset, err = fastpb.Skip(buf, _type, number) + if err != nil { + goto SkipFieldError + } + } + return offset, nil +SkipFieldError: + return offset, fmt.Errorf("%T cannot parse invalid wire-format data, error: %s", x, err) +ReadFieldError: + return offset, fmt.Errorf("%T read field %d '%s' error: %s", x, number, fieldIDToName_FavoriteListRequest[number], err) +} + +func (x *FavoriteListRequest) fastReadField1(buf []byte, _type int8) (offset int, err error) { + x.UserId, offset, err = fastpb.ReadInt64(buf, _type) + return offset, err +} + +func (x *FavoriteListRequest) fastReadField2(buf []byte, _type int8) (offset int, err error) { + x.Token, offset, err = fastpb.ReadString(buf, _type) + return offset, err +} + +func (x *FavoriteListResponse) FastRead(buf []byte, _type int8, number int32) (offset int, err error) { + switch number { + case 1: + offset, err = x.fastReadField1(buf, _type) + if err != nil { + goto ReadFieldError + } + case 2: + offset, err = x.fastReadField2(buf, _type) + if err != nil { + goto ReadFieldError + } + case 3: + offset, err = x.fastReadField3(buf, _type) + if err != nil { + goto ReadFieldError + } + default: + offset, err = fastpb.Skip(buf, _type, number) + if err != nil { + goto SkipFieldError + } + } + return offset, nil +SkipFieldError: + return offset, fmt.Errorf("%T cannot parse invalid wire-format data, error: %s", x, err) +ReadFieldError: + return offset, fmt.Errorf("%T read field %d '%s' error: %s", x, number, fieldIDToName_FavoriteListResponse[number], err) +} + +func (x *FavoriteListResponse) fastReadField1(buf []byte, _type int8) (offset int, err error) { + x.StatusCode, offset, err = fastpb.ReadInt32(buf, _type) + return offset, err +} + +func (x *FavoriteListResponse) fastReadField2(buf []byte, _type int8) (offset int, err error) { + tmp, offset, err := fastpb.ReadString(buf, _type) + x.StatusMsg = &tmp + return offset, err +} + +func (x *FavoriteListResponse) fastReadField3(buf []byte, _type int8) (offset int, err error) { + var v video.Video + offset, err = fastpb.ReadMessage(buf, _type, &v) + if err != nil { + return offset, err + } + x.VideoList = append(x.VideoList, &v) + return offset, nil +} + +func (x *FavoriteActionRequest) FastWrite(buf []byte) (offset int) { + if x == nil { + return offset + } + offset += x.fastWriteField1(buf[offset:]) + offset += x.fastWriteField2(buf[offset:]) + offset += x.fastWriteField3(buf[offset:]) + return offset +} + +func (x *FavoriteActionRequest) fastWriteField1(buf []byte) (offset int) { + if x.Token == "" { + return offset + } + offset += fastpb.WriteString(buf[offset:], 1, x.GetToken()) + return offset +} + +func (x *FavoriteActionRequest) fastWriteField2(buf []byte) (offset int) { + if x.VideoId == 0 { + return offset + } + offset += fastpb.WriteInt64(buf[offset:], 2, x.GetVideoId()) + return offset +} + +func (x *FavoriteActionRequest) fastWriteField3(buf []byte) (offset int) { + if x.ActionType == 0 { + return offset + } + offset += fastpb.WriteInt32(buf[offset:], 3, x.GetActionType()) + return offset +} + +func (x *FavoriteActionResponse) FastWrite(buf []byte) (offset int) { + if x == nil { + return offset + } + offset += x.fastWriteField1(buf[offset:]) + offset += x.fastWriteField2(buf[offset:]) + return offset +} + +func (x *FavoriteActionResponse) fastWriteField1(buf []byte) (offset int) { + if x.StatusCode == 0 { + return offset + } + offset += fastpb.WriteInt32(buf[offset:], 1, x.GetStatusCode()) + return offset +} + +func (x *FavoriteActionResponse) fastWriteField2(buf []byte) (offset int) { + if x.StatusMsg == "" { + return offset + } + offset += fastpb.WriteString(buf[offset:], 2, x.GetStatusMsg()) + return offset +} + +func (x *FavoriteListRequest) FastWrite(buf []byte) (offset int) { + if x == nil { + return offset + } + offset += x.fastWriteField1(buf[offset:]) + offset += x.fastWriteField2(buf[offset:]) + return offset +} + +func (x *FavoriteListRequest) fastWriteField1(buf []byte) (offset int) { + if x.UserId == 0 { + return offset + } + offset += fastpb.WriteInt64(buf[offset:], 1, x.GetUserId()) + return offset +} + +func (x *FavoriteListRequest) fastWriteField2(buf []byte) (offset int) { + if x.Token == "" { + return offset + } + offset += fastpb.WriteString(buf[offset:], 2, x.GetToken()) + return offset +} + +func (x *FavoriteListResponse) FastWrite(buf []byte) (offset int) { + if x == nil { + return offset + } + offset += x.fastWriteField1(buf[offset:]) + offset += x.fastWriteField2(buf[offset:]) + offset += x.fastWriteField3(buf[offset:]) + return offset +} + +func (x *FavoriteListResponse) fastWriteField1(buf []byte) (offset int) { + if x.StatusCode == 0 { + return offset + } + offset += fastpb.WriteInt32(buf[offset:], 1, x.GetStatusCode()) + return offset +} + +func (x *FavoriteListResponse) fastWriteField2(buf []byte) (offset int) { + if x.StatusMsg == nil { + return offset + } + offset += fastpb.WriteString(buf[offset:], 2, x.GetStatusMsg()) + return offset +} + +func (x *FavoriteListResponse) fastWriteField3(buf []byte) (offset int) { + if x.VideoList == nil { + return offset + } + for i := range x.GetVideoList() { + offset += fastpb.WriteMessage(buf[offset:], 3, x.GetVideoList()[i]) + } + return offset +} + +func (x *FavoriteActionRequest) Size() (n int) { + if x == nil { + return n + } + n += x.sizeField1() + n += x.sizeField2() + n += x.sizeField3() + return n +} + +func (x *FavoriteActionRequest) sizeField1() (n int) { + if x.Token == "" { + return n + } + n += fastpb.SizeString(1, x.GetToken()) + return n +} + +func (x *FavoriteActionRequest) sizeField2() (n int) { + if x.VideoId == 0 { + return n + } + n += fastpb.SizeInt64(2, x.GetVideoId()) + return n +} + +func (x *FavoriteActionRequest) sizeField3() (n int) { + if x.ActionType == 0 { + return n + } + n += fastpb.SizeInt32(3, x.GetActionType()) + return n +} + +func (x *FavoriteActionResponse) Size() (n int) { + if x == nil { + return n + } + n += x.sizeField1() + n += x.sizeField2() + return n +} + +func (x *FavoriteActionResponse) sizeField1() (n int) { + if x.StatusCode == 0 { + return n + } + n += fastpb.SizeInt32(1, x.GetStatusCode()) + return n +} + +func (x *FavoriteActionResponse) sizeField2() (n int) { + if x.StatusMsg == "" { + return n + } + n += fastpb.SizeString(2, x.GetStatusMsg()) + return n +} + +func (x *FavoriteListRequest) Size() (n int) { + if x == nil { + return n + } + n += x.sizeField1() + n += x.sizeField2() + return n +} + +func (x *FavoriteListRequest) sizeField1() (n int) { + if x.UserId == 0 { + return n + } + n += fastpb.SizeInt64(1, x.GetUserId()) + return n +} + +func (x *FavoriteListRequest) sizeField2() (n int) { + if x.Token == "" { + return n + } + n += fastpb.SizeString(2, x.GetToken()) + return n +} + +func (x *FavoriteListResponse) Size() (n int) { + if x == nil { + return n + } + n += x.sizeField1() + n += x.sizeField2() + n += x.sizeField3() + return n +} + +func (x *FavoriteListResponse) sizeField1() (n int) { + if x.StatusCode == 0 { + return n + } + n += fastpb.SizeInt32(1, x.GetStatusCode()) + return n +} + +func (x *FavoriteListResponse) sizeField2() (n int) { + if x.StatusMsg == nil { + return n + } + n += fastpb.SizeString(2, x.GetStatusMsg()) + return n +} + +func (x *FavoriteListResponse) sizeField3() (n int) { + if x.VideoList == nil { + return n + } + for i := range x.GetVideoList() { + n += fastpb.SizeMessage(3, x.GetVideoList()[i]) + } + return n +} + +var fieldIDToName_FavoriteActionRequest = map[int32]string{ + 1: "Token", + 2: "VideoId", + 3: "ActionType", +} + +var fieldIDToName_FavoriteActionResponse = map[int32]string{ + 1: "StatusCode", + 2: "StatusMsg", +} + +var fieldIDToName_FavoriteListRequest = map[int32]string{ + 1: "UserId", + 2: "Token", +} + +var fieldIDToName_FavoriteListResponse = map[int32]string{ + 1: "StatusCode", + 2: "StatusMsg", + 3: "VideoList", +} + +var _ = video.File_idl_video_proto diff --git a/kitex_gen/favorite/favorite.pb.go b/kitex_gen/favorite/favorite.pb.go new file mode 100644 index 0000000..60ab239 --- /dev/null +++ b/kitex_gen/favorite/favorite.pb.go @@ -0,0 +1,427 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v3.21.12 +// source: idl/favorite.proto + +package favorite + +import ( + context "context" + video "github.com/Tiktok-Lite/kotkit/kitex_gen/video" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type FavoriteActionRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"` // 用户鉴权token + VideoId int64 `protobuf:"varint,2,opt,name=video_id,json=videoId,proto3" json:"video_id,omitempty"` // 视频id + ActionType int32 `protobuf:"varint,3,opt,name=action_type,json=actionType,proto3" json:"action_type,omitempty"` // 1-点赞,2-取消点赞 +} + +func (x *FavoriteActionRequest) Reset() { + *x = FavoriteActionRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_idl_favorite_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *FavoriteActionRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*FavoriteActionRequest) ProtoMessage() {} + +func (x *FavoriteActionRequest) ProtoReflect() protoreflect.Message { + mi := &file_idl_favorite_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use FavoriteActionRequest.ProtoReflect.Descriptor instead. +func (*FavoriteActionRequest) Descriptor() ([]byte, []int) { + return file_idl_favorite_proto_rawDescGZIP(), []int{0} +} + +func (x *FavoriteActionRequest) GetToken() string { + if x != nil { + return x.Token + } + return "" +} + +func (x *FavoriteActionRequest) GetVideoId() int64 { + if x != nil { + return x.VideoId + } + return 0 +} + +func (x *FavoriteActionRequest) GetActionType() int32 { + if x != nil { + return x.ActionType + } + return 0 +} + +type FavoriteActionResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + StatusCode int32 `protobuf:"varint,1,opt,name=status_code,json=statusCode,proto3" json:"status_code,omitempty"` // 状态码,0-成功,其他值-失败 + StatusMsg string `protobuf:"bytes,2,opt,name=status_msg,json=statusMsg,proto3" json:"status_msg,omitempty"` // 返回状态描述 +} + +func (x *FavoriteActionResponse) Reset() { + *x = FavoriteActionResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_idl_favorite_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *FavoriteActionResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*FavoriteActionResponse) ProtoMessage() {} + +func (x *FavoriteActionResponse) ProtoReflect() protoreflect.Message { + mi := &file_idl_favorite_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use FavoriteActionResponse.ProtoReflect.Descriptor instead. +func (*FavoriteActionResponse) Descriptor() ([]byte, []int) { + return file_idl_favorite_proto_rawDescGZIP(), []int{1} +} + +func (x *FavoriteActionResponse) GetStatusCode() int32 { + if x != nil { + return x.StatusCode + } + return 0 +} + +func (x *FavoriteActionResponse) GetStatusMsg() string { + if x != nil { + return x.StatusMsg + } + return "" +} + +type FavoriteListRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + UserId int64 `protobuf:"varint,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` // 用户id + Token string `protobuf:"bytes,2,opt,name=token,proto3" json:"token,omitempty"` // 用户鉴权token +} + +func (x *FavoriteListRequest) Reset() { + *x = FavoriteListRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_idl_favorite_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *FavoriteListRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*FavoriteListRequest) ProtoMessage() {} + +func (x *FavoriteListRequest) ProtoReflect() protoreflect.Message { + mi := &file_idl_favorite_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use FavoriteListRequest.ProtoReflect.Descriptor instead. +func (*FavoriteListRequest) Descriptor() ([]byte, []int) { + return file_idl_favorite_proto_rawDescGZIP(), []int{2} +} + +func (x *FavoriteListRequest) GetUserId() int64 { + if x != nil { + return x.UserId + } + return 0 +} + +func (x *FavoriteListRequest) GetToken() string { + if x != nil { + return x.Token + } + return "" +} + +type FavoriteListResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + StatusCode int32 `protobuf:"varint,1,opt,name=status_code,json=statusCode,proto3" json:"status_code,omitempty"` // 状态码,0-成功,其他值-失败 + StatusMsg *string `protobuf:"bytes,2,opt,name=status_msg,json=statusMsg,proto3,oneof" json:"status_msg,omitempty"` // 返回状态描述 + VideoList []*video.Video `protobuf:"bytes,3,rep,name=video_list,json=videoList,proto3" json:"video_list,omitempty"` // 用户点赞视频列表 +} + +func (x *FavoriteListResponse) Reset() { + *x = FavoriteListResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_idl_favorite_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *FavoriteListResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*FavoriteListResponse) ProtoMessage() {} + +func (x *FavoriteListResponse) ProtoReflect() protoreflect.Message { + mi := &file_idl_favorite_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use FavoriteListResponse.ProtoReflect.Descriptor instead. +func (*FavoriteListResponse) Descriptor() ([]byte, []int) { + return file_idl_favorite_proto_rawDescGZIP(), []int{3} +} + +func (x *FavoriteListResponse) GetStatusCode() int32 { + if x != nil { + return x.StatusCode + } + return 0 +} + +func (x *FavoriteListResponse) GetStatusMsg() string { + if x != nil && x.StatusMsg != nil { + return *x.StatusMsg + } + return "" +} + +func (x *FavoriteListResponse) GetVideoList() []*video.Video { + if x != nil { + return x.VideoList + } + return nil +} + +var File_idl_favorite_proto protoreflect.FileDescriptor + +var file_idl_favorite_proto_rawDesc = []byte{ + 0x0a, 0x12, 0x69, 0x64, 0x6c, 0x2f, 0x66, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x66, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x1a, 0x0f, + 0x69, 0x64, 0x6c, 0x2f, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, + 0x69, 0x0a, 0x15, 0x46, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x41, 0x63, 0x74, 0x69, 0x6f, + 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, + 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x19, + 0x0a, 0x08, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, + 0x52, 0x07, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x49, 0x64, 0x12, 0x1f, 0x0a, 0x0b, 0x61, 0x63, 0x74, + 0x69, 0x6f, 0x6e, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, + 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x22, 0x58, 0x0a, 0x16, 0x46, 0x61, + 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x63, + 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, + 0x73, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, + 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x74, 0x61, 0x74, 0x75, + 0x73, 0x4d, 0x73, 0x67, 0x22, 0x44, 0x0a, 0x13, 0x46, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, + 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x75, + 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x75, 0x73, + 0x65, 0x72, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x97, 0x01, 0x0a, 0x14, 0x46, + 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x63, 0x6f, + 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, + 0x43, 0x6f, 0x64, 0x65, 0x12, 0x22, 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x6d, + 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x09, 0x73, 0x74, 0x61, 0x74, + 0x75, 0x73, 0x4d, 0x73, 0x67, 0x88, 0x01, 0x01, 0x12, 0x2b, 0x0a, 0x0a, 0x76, 0x69, 0x64, 0x65, + 0x6f, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x76, + 0x69, 0x64, 0x65, 0x6f, 0x2e, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x52, 0x09, 0x76, 0x69, 0x64, 0x65, + 0x6f, 0x4c, 0x69, 0x73, 0x74, 0x42, 0x0d, 0x0a, 0x0b, 0x5f, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, + 0x5f, 0x6d, 0x73, 0x67, 0x32, 0xb9, 0x01, 0x0a, 0x0f, 0x46, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, + 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x55, 0x0a, 0x0e, 0x46, 0x61, 0x76, 0x6f, + 0x72, 0x69, 0x74, 0x65, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1f, 0x2e, 0x66, 0x61, 0x76, + 0x6f, 0x72, 0x69, 0x74, 0x65, 0x2e, 0x46, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x41, 0x63, + 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x20, 0x2e, 0x66, 0x61, + 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x2e, 0x46, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x41, + 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, + 0x4f, 0x0a, 0x0c, 0x46, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x12, + 0x1d, 0x2e, 0x66, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x2e, 0x46, 0x61, 0x76, 0x6f, 0x72, + 0x69, 0x74, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1e, + 0x2e, 0x66, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x2e, 0x46, 0x61, 0x76, 0x6f, 0x72, 0x69, + 0x74, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, + 0x42, 0x32, 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x54, + 0x69, 0x6b, 0x74, 0x6f, 0x6b, 0x2d, 0x4c, 0x69, 0x74, 0x65, 0x2f, 0x6b, 0x6f, 0x74, 0x6b, 0x69, + 0x74, 0x2f, 0x6b, 0x69, 0x74, 0x65, 0x78, 0x5f, 0x67, 0x65, 0x6e, 0x2f, 0x66, 0x61, 0x76, 0x6f, + 0x72, 0x69, 0x74, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_idl_favorite_proto_rawDescOnce sync.Once + file_idl_favorite_proto_rawDescData = file_idl_favorite_proto_rawDesc +) + +func file_idl_favorite_proto_rawDescGZIP() []byte { + file_idl_favorite_proto_rawDescOnce.Do(func() { + file_idl_favorite_proto_rawDescData = protoimpl.X.CompressGZIP(file_idl_favorite_proto_rawDescData) + }) + return file_idl_favorite_proto_rawDescData +} + +var file_idl_favorite_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_idl_favorite_proto_goTypes = []interface{}{ + (*FavoriteActionRequest)(nil), // 0: favorite.FavoriteActionRequest + (*FavoriteActionResponse)(nil), // 1: favorite.FavoriteActionResponse + (*FavoriteListRequest)(nil), // 2: favorite.FavoriteListRequest + (*FavoriteListResponse)(nil), // 3: favorite.FavoriteListResponse + (*video.Video)(nil), // 4: video.Video +} +var file_idl_favorite_proto_depIdxs = []int32{ + 4, // 0: favorite.FavoriteListResponse.video_list:type_name -> video.Video + 0, // 1: favorite.FavoriteService.FavoriteAction:input_type -> favorite.FavoriteActionRequest + 2, // 2: favorite.FavoriteService.FavoriteList:input_type -> favorite.FavoriteListRequest + 1, // 3: favorite.FavoriteService.FavoriteAction:output_type -> favorite.FavoriteActionResponse + 3, // 4: favorite.FavoriteService.FavoriteList:output_type -> favorite.FavoriteListResponse + 3, // [3:5] is the sub-list for method output_type + 1, // [1:3] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_idl_favorite_proto_init() } +func file_idl_favorite_proto_init() { + if File_idl_favorite_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_idl_favorite_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*FavoriteActionRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_idl_favorite_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*FavoriteActionResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_idl_favorite_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*FavoriteListRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_idl_favorite_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*FavoriteListResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + file_idl_favorite_proto_msgTypes[3].OneofWrappers = []interface{}{} + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_idl_favorite_proto_rawDesc, + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_idl_favorite_proto_goTypes, + DependencyIndexes: file_idl_favorite_proto_depIdxs, + MessageInfos: file_idl_favorite_proto_msgTypes, + }.Build() + File_idl_favorite_proto = out.File + file_idl_favorite_proto_rawDesc = nil + file_idl_favorite_proto_goTypes = nil + file_idl_favorite_proto_depIdxs = nil +} + +var _ context.Context + +// Code generated by Kitex v0.6.2. DO NOT EDIT. + +type FavoriteService interface { + FavoriteAction(ctx context.Context, req *FavoriteActionRequest) (res *FavoriteActionResponse, err error) + FavoriteList(ctx context.Context, req *FavoriteListRequest) (res *FavoriteListResponse, err error) +} diff --git a/kitex_gen/favorite/favoriteservice/client.go b/kitex_gen/favorite/favoriteservice/client.go new file mode 100644 index 0000000..089b698 --- /dev/null +++ b/kitex_gen/favorite/favoriteservice/client.go @@ -0,0 +1,55 @@ +// Code generated by Kitex v0.6.2. DO NOT EDIT. + +package favoriteservice + +import ( + "context" + favorite "github.com/Tiktok-Lite/kotkit/kitex_gen/favorite" + client "github.com/cloudwego/kitex/client" + callopt "github.com/cloudwego/kitex/client/callopt" +) + +// Client is designed to provide IDL-compatible methods with call-option parameter for kitex framework. +type Client interface { + FavoriteAction(ctx context.Context, Req *favorite.FavoriteActionRequest, callOptions ...callopt.Option) (r *favorite.FavoriteActionResponse, err error) + FavoriteList(ctx context.Context, Req *favorite.FavoriteListRequest, callOptions ...callopt.Option) (r *favorite.FavoriteListResponse, err error) +} + +// NewClient creates a client for the service defined in IDL. +func NewClient(destService string, opts ...client.Option) (Client, error) { + var options []client.Option + options = append(options, client.WithDestService(destService)) + + options = append(options, opts...) + + kc, err := client.NewClient(serviceInfo(), options...) + if err != nil { + return nil, err + } + return &kFavoriteServiceClient{ + kClient: newServiceClient(kc), + }, nil +} + +// MustNewClient creates a client for the service defined in IDL. It panics if any error occurs. +func MustNewClient(destService string, opts ...client.Option) Client { + kc, err := NewClient(destService, opts...) + if err != nil { + panic(err) + } + return kc +} + +type kFavoriteServiceClient struct { + *kClient +} + +func (p *kFavoriteServiceClient) FavoriteAction(ctx context.Context, Req *favorite.FavoriteActionRequest, callOptions ...callopt.Option) (r *favorite.FavoriteActionResponse, err error) { + ctx = client.NewCtxWithCallOptions(ctx, callOptions) + return p.kClient.FavoriteAction(ctx, Req) +} + +func (p *kFavoriteServiceClient) FavoriteList(ctx context.Context, Req *favorite.FavoriteListRequest, callOptions ...callopt.Option) (r *favorite.FavoriteListResponse, err error) { + ctx = client.NewCtxWithCallOptions(ctx, callOptions) + return p.kClient.FavoriteList(ctx, Req) +} diff --git a/kitex_gen/favorite/favoriteservice/favoriteservice.go b/kitex_gen/favorite/favoriteservice/favoriteservice.go new file mode 100644 index 0000000..de6d75a --- /dev/null +++ b/kitex_gen/favorite/favoriteservice/favoriteservice.go @@ -0,0 +1,376 @@ +// Code generated by Kitex v0.6.2. DO NOT EDIT. + +package favoriteservice + +import ( + "context" + "fmt" + favorite "github.com/Tiktok-Lite/kotkit/kitex_gen/favorite" + client "github.com/cloudwego/kitex/client" + kitex "github.com/cloudwego/kitex/pkg/serviceinfo" + streaming "github.com/cloudwego/kitex/pkg/streaming" + proto "google.golang.org/protobuf/proto" +) + +func serviceInfo() *kitex.ServiceInfo { + return favoriteServiceServiceInfo +} + +var favoriteServiceServiceInfo = NewServiceInfo() + +func NewServiceInfo() *kitex.ServiceInfo { + serviceName := "FavoriteService" + handlerType := (*favorite.FavoriteService)(nil) + methods := map[string]kitex.MethodInfo{ + "FavoriteAction": kitex.NewMethodInfo(favoriteActionHandler, newFavoriteActionArgs, newFavoriteActionResult, false), + "FavoriteList": kitex.NewMethodInfo(favoriteListHandler, newFavoriteListArgs, newFavoriteListResult, false), + } + extra := map[string]interface{}{ + "PackageName": "favorite", + } + svcInfo := &kitex.ServiceInfo{ + ServiceName: serviceName, + HandlerType: handlerType, + Methods: methods, + PayloadCodec: kitex.Protobuf, + KiteXGenVersion: "v0.6.2", + Extra: extra, + } + return svcInfo +} + +func favoriteActionHandler(ctx context.Context, handler interface{}, arg, result interface{}) error { + switch s := arg.(type) { + case *streaming.Args: + st := s.Stream + req := new(favorite.FavoriteActionRequest) + if err := st.RecvMsg(req); err != nil { + return err + } + resp, err := handler.(favorite.FavoriteService).FavoriteAction(ctx, req) + if err != nil { + return err + } + if err := st.SendMsg(resp); err != nil { + return err + } + case *FavoriteActionArgs: + success, err := handler.(favorite.FavoriteService).FavoriteAction(ctx, s.Req) + if err != nil { + return err + } + realResult := result.(*FavoriteActionResult) + realResult.Success = success + } + return nil +} +func newFavoriteActionArgs() interface{} { + return &FavoriteActionArgs{} +} + +func newFavoriteActionResult() interface{} { + return &FavoriteActionResult{} +} + +type FavoriteActionArgs struct { + Req *favorite.FavoriteActionRequest +} + +func (p *FavoriteActionArgs) FastRead(buf []byte, _type int8, number int32) (n int, err error) { + if !p.IsSetReq() { + p.Req = new(favorite.FavoriteActionRequest) + } + return p.Req.FastRead(buf, _type, number) +} + +func (p *FavoriteActionArgs) FastWrite(buf []byte) (n int) { + if !p.IsSetReq() { + return 0 + } + return p.Req.FastWrite(buf) +} + +func (p *FavoriteActionArgs) Size() (n int) { + if !p.IsSetReq() { + return 0 + } + return p.Req.Size() +} + +func (p *FavoriteActionArgs) Marshal(out []byte) ([]byte, error) { + if !p.IsSetReq() { + return out, fmt.Errorf("No req in FavoriteActionArgs") + } + return proto.Marshal(p.Req) +} + +func (p *FavoriteActionArgs) Unmarshal(in []byte) error { + msg := new(favorite.FavoriteActionRequest) + if err := proto.Unmarshal(in, msg); err != nil { + return err + } + p.Req = msg + return nil +} + +var FavoriteActionArgs_Req_DEFAULT *favorite.FavoriteActionRequest + +func (p *FavoriteActionArgs) GetReq() *favorite.FavoriteActionRequest { + if !p.IsSetReq() { + return FavoriteActionArgs_Req_DEFAULT + } + return p.Req +} + +func (p *FavoriteActionArgs) IsSetReq() bool { + return p.Req != nil +} + +func (p *FavoriteActionArgs) GetFirstArgument() interface{} { + return p.Req +} + +type FavoriteActionResult struct { + Success *favorite.FavoriteActionResponse +} + +var FavoriteActionResult_Success_DEFAULT *favorite.FavoriteActionResponse + +func (p *FavoriteActionResult) FastRead(buf []byte, _type int8, number int32) (n int, err error) { + if !p.IsSetSuccess() { + p.Success = new(favorite.FavoriteActionResponse) + } + return p.Success.FastRead(buf, _type, number) +} + +func (p *FavoriteActionResult) FastWrite(buf []byte) (n int) { + if !p.IsSetSuccess() { + return 0 + } + return p.Success.FastWrite(buf) +} + +func (p *FavoriteActionResult) Size() (n int) { + if !p.IsSetSuccess() { + return 0 + } + return p.Success.Size() +} + +func (p *FavoriteActionResult) Marshal(out []byte) ([]byte, error) { + if !p.IsSetSuccess() { + return out, fmt.Errorf("No req in FavoriteActionResult") + } + return proto.Marshal(p.Success) +} + +func (p *FavoriteActionResult) Unmarshal(in []byte) error { + msg := new(favorite.FavoriteActionResponse) + if err := proto.Unmarshal(in, msg); err != nil { + return err + } + p.Success = msg + return nil +} + +func (p *FavoriteActionResult) GetSuccess() *favorite.FavoriteActionResponse { + if !p.IsSetSuccess() { + return FavoriteActionResult_Success_DEFAULT + } + return p.Success +} + +func (p *FavoriteActionResult) SetSuccess(x interface{}) { + p.Success = x.(*favorite.FavoriteActionResponse) +} + +func (p *FavoriteActionResult) IsSetSuccess() bool { + return p.Success != nil +} + +func (p *FavoriteActionResult) GetResult() interface{} { + return p.Success +} + +func favoriteListHandler(ctx context.Context, handler interface{}, arg, result interface{}) error { + switch s := arg.(type) { + case *streaming.Args: + st := s.Stream + req := new(favorite.FavoriteListRequest) + if err := st.RecvMsg(req); err != nil { + return err + } + resp, err := handler.(favorite.FavoriteService).FavoriteList(ctx, req) + if err != nil { + return err + } + if err := st.SendMsg(resp); err != nil { + return err + } + case *FavoriteListArgs: + success, err := handler.(favorite.FavoriteService).FavoriteList(ctx, s.Req) + if err != nil { + return err + } + realResult := result.(*FavoriteListResult) + realResult.Success = success + } + return nil +} +func newFavoriteListArgs() interface{} { + return &FavoriteListArgs{} +} + +func newFavoriteListResult() interface{} { + return &FavoriteListResult{} +} + +type FavoriteListArgs struct { + Req *favorite.FavoriteListRequest +} + +func (p *FavoriteListArgs) FastRead(buf []byte, _type int8, number int32) (n int, err error) { + if !p.IsSetReq() { + p.Req = new(favorite.FavoriteListRequest) + } + return p.Req.FastRead(buf, _type, number) +} + +func (p *FavoriteListArgs) FastWrite(buf []byte) (n int) { + if !p.IsSetReq() { + return 0 + } + return p.Req.FastWrite(buf) +} + +func (p *FavoriteListArgs) Size() (n int) { + if !p.IsSetReq() { + return 0 + } + return p.Req.Size() +} + +func (p *FavoriteListArgs) Marshal(out []byte) ([]byte, error) { + if !p.IsSetReq() { + return out, fmt.Errorf("No req in FavoriteListArgs") + } + return proto.Marshal(p.Req) +} + +func (p *FavoriteListArgs) Unmarshal(in []byte) error { + msg := new(favorite.FavoriteListRequest) + if err := proto.Unmarshal(in, msg); err != nil { + return err + } + p.Req = msg + return nil +} + +var FavoriteListArgs_Req_DEFAULT *favorite.FavoriteListRequest + +func (p *FavoriteListArgs) GetReq() *favorite.FavoriteListRequest { + if !p.IsSetReq() { + return FavoriteListArgs_Req_DEFAULT + } + return p.Req +} + +func (p *FavoriteListArgs) IsSetReq() bool { + return p.Req != nil +} + +func (p *FavoriteListArgs) GetFirstArgument() interface{} { + return p.Req +} + +type FavoriteListResult struct { + Success *favorite.FavoriteListResponse +} + +var FavoriteListResult_Success_DEFAULT *favorite.FavoriteListResponse + +func (p *FavoriteListResult) FastRead(buf []byte, _type int8, number int32) (n int, err error) { + if !p.IsSetSuccess() { + p.Success = new(favorite.FavoriteListResponse) + } + return p.Success.FastRead(buf, _type, number) +} + +func (p *FavoriteListResult) FastWrite(buf []byte) (n int) { + if !p.IsSetSuccess() { + return 0 + } + return p.Success.FastWrite(buf) +} + +func (p *FavoriteListResult) Size() (n int) { + if !p.IsSetSuccess() { + return 0 + } + return p.Success.Size() +} + +func (p *FavoriteListResult) Marshal(out []byte) ([]byte, error) { + if !p.IsSetSuccess() { + return out, fmt.Errorf("No req in FavoriteListResult") + } + return proto.Marshal(p.Success) +} + +func (p *FavoriteListResult) Unmarshal(in []byte) error { + msg := new(favorite.FavoriteListResponse) + if err := proto.Unmarshal(in, msg); err != nil { + return err + } + p.Success = msg + return nil +} + +func (p *FavoriteListResult) GetSuccess() *favorite.FavoriteListResponse { + if !p.IsSetSuccess() { + return FavoriteListResult_Success_DEFAULT + } + return p.Success +} + +func (p *FavoriteListResult) SetSuccess(x interface{}) { + p.Success = x.(*favorite.FavoriteListResponse) +} + +func (p *FavoriteListResult) IsSetSuccess() bool { + return p.Success != nil +} + +func (p *FavoriteListResult) GetResult() interface{} { + return p.Success +} + +type kClient struct { + c client.Client +} + +func newServiceClient(c client.Client) *kClient { + return &kClient{ + c: c, + } +} + +func (p *kClient) FavoriteAction(ctx context.Context, Req *favorite.FavoriteActionRequest) (r *favorite.FavoriteActionResponse, err error) { + var _args FavoriteActionArgs + _args.Req = Req + var _result FavoriteActionResult + if err = p.c.Call(ctx, "FavoriteAction", &_args, &_result); err != nil { + return + } + return _result.GetSuccess(), nil +} + +func (p *kClient) FavoriteList(ctx context.Context, Req *favorite.FavoriteListRequest) (r *favorite.FavoriteListResponse, err error) { + var _args FavoriteListArgs + _args.Req = Req + var _result FavoriteListResult + if err = p.c.Call(ctx, "FavoriteList", &_args, &_result); err != nil { + return + } + return _result.GetSuccess(), nil +} diff --git a/kitex_gen/favorite/favoriteservice/invoker.go b/kitex_gen/favorite/favoriteservice/invoker.go new file mode 100644 index 0000000..8075122 --- /dev/null +++ b/kitex_gen/favorite/favoriteservice/invoker.go @@ -0,0 +1,24 @@ +// Code generated by Kitex v0.6.2. DO NOT EDIT. + +package favoriteservice + +import ( + favorite "github.com/Tiktok-Lite/kotkit/kitex_gen/favorite" + server "github.com/cloudwego/kitex/server" +) + +// NewInvoker creates a server.Invoker with the given handler and options. +func NewInvoker(handler favorite.FavoriteService, opts ...server.Option) server.Invoker { + var options []server.Option + + options = append(options, opts...) + + s := server.NewInvoker(options...) + if err := s.RegisterService(serviceInfo(), handler); err != nil { + panic(err) + } + if err := s.Init(); err != nil { + panic(err) + } + return s +} diff --git a/kitex_gen/favorite/favoriteservice/server.go b/kitex_gen/favorite/favoriteservice/server.go new file mode 100644 index 0000000..e9f51f5 --- /dev/null +++ b/kitex_gen/favorite/favoriteservice/server.go @@ -0,0 +1,20 @@ +// Code generated by Kitex v0.6.2. DO NOT EDIT. +package favoriteservice + +import ( + favorite "github.com/Tiktok-Lite/kotkit/kitex_gen/favorite" + server "github.com/cloudwego/kitex/server" +) + +// NewServer creates a server.Server with the given handler and options. +func NewServer(handler favorite.FavoriteService, opts ...server.Option) server.Server { + var options []server.Option + + options = append(options, opts...) + + svr := server.NewServer(options...) + if err := svr.RegisterService(serviceInfo(), handler); err != nil { + panic(err) + } + return svr +} diff --git a/pkg/helper/constant/const.go b/pkg/helper/constant/const.go index f547119..5a92178 100644 --- a/pkg/helper/constant/const.go +++ b/pkg/helper/constant/const.go @@ -1,16 +1,22 @@ package constant const ( - DefaultDBConfigName = "db" - DefaultLogConfigName = "log" - DefaultAPIConfigName = "api" - DefaultUserConfigName = "user" - DefaultVideoConfigName = "video" - DefaultLoginConfigName = "login" - DefaultMinioConfigName = "minio" + DefaultDBConfigName = "db" + DefaultLogConfigName = "log" + DefaultAPIConfigName = "api" + DefaultUserConfigName = "user" + DefaultVideoConfigName = "video" + DefaultLoginConfigName = "login" + DefaultMinioConfigName = "minio" + DefaultFavoriteConfigName = "favorite" ) const ( StatusOKCode = 0 // (这里默认)响应返回0即正确 StatusErrorCode = -1 // 响应返回-1即错误 ) + +const ( + FavoriteCode = 1 + UnFavoriteCode = 2 +) From 29f1b857d4e0255d7a7cd205ef74666f630857d7 Mon Sep 17 00:00:00 2001 From: Shiyuan Ji <919745273@qq.com> Date: Tue, 29 Aug 2023 10:18:09 +0800 Subject: [PATCH 2/3] fix conflicts --- cmd/api/rpc/init.go | 3 +++ pkg/helper/constant/const.go | 1 + 2 files changed, 4 insertions(+) diff --git a/cmd/api/rpc/init.go b/cmd/api/rpc/init.go index 37a3a1b..349080f 100644 --- a/cmd/api/rpc/init.go +++ b/cmd/api/rpc/init.go @@ -17,4 +17,7 @@ func InitRPC() { favoriteConfig := conf.LoadConfig(constant.DefaultFavoriteConfigName) InitFavorite(favoriteConfig) + + relationConfig := conf.LoadConfig(constant.DefaultRelationConfigName) + InitRelation(relationConfig) } diff --git a/pkg/helper/constant/const.go b/pkg/helper/constant/const.go index 5a92178..03806fa 100644 --- a/pkg/helper/constant/const.go +++ b/pkg/helper/constant/const.go @@ -8,6 +8,7 @@ const ( DefaultVideoConfigName = "video" DefaultLoginConfigName = "login" DefaultMinioConfigName = "minio" + DefaultRelationConfigName = "relation" DefaultFavoriteConfigName = "favorite" ) From 6b45a08976be8dcba604405f0e4a930d90d644b6 Mon Sep 17 00:00:00 2001 From: Shiyuan Ji <919745273@qq.com> Date: Tue, 29 Aug 2023 10:29:22 +0800 Subject: [PATCH 3/3] fix config and port conflict --- config/api.yml | 3 ++- config/db.yml | 2 +- config/favorite.yml | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/config/api.yml b/config/api.yml index a37edda..3d2c497 100644 --- a/config/api.yml +++ b/config/api.yml @@ -1,4 +1,5 @@ server: name: "test server" - host: 127.0.0.1 +# 这里使用app测试时候使用0.0.0.0 + host: 0.0.0.0 port: 8080 \ No newline at end of file diff --git a/config/db.yml b/config/db.yml index 903860c..ea81cdb 100644 --- a/config/db.yml +++ b/config/db.yml @@ -1,4 +1,4 @@ data: mysql: # 开发前本地记得更改此设置 - user: root:test@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local \ No newline at end of file + user: root:123456789@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local \ No newline at end of file diff --git a/config/favorite.yml b/config/favorite.yml index eeb1d77..db94999 100644 --- a/config/favorite.yml +++ b/config/favorite.yml @@ -1,4 +1,4 @@ server: name: "favorite service" host: 127.0.0.1 - port: 8084 \ No newline at end of file + port: 8085 \ No newline at end of file