From 943ec56e18ced163c738114d0d8b9c0d085adbbf Mon Sep 17 00:00:00 2001 From: Darley Date: Sat, 18 Feb 2023 23:04:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=82=B9=E8=B5=9E=E6=9C=8D=E5=8A=A1=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interaction/favorite_list_server.go | 25 ++++-- biz/handler/interaction/favorite_server.go | 38 ++++++++- biz/handler/interaction/resp.go | 25 ++++++ go.mod | 1 + go.sum | 2 + kitex_server/initmicro.go | 8 ++ kitex_server/interaction.go | 81 +++++++++++++++++++ tools/errno/errno.go | 56 +++++++++++++ 8 files changed, 228 insertions(+), 8 deletions(-) create mode 100644 biz/handler/interaction/resp.go create mode 100644 kitex_server/interaction.go create mode 100644 tools/errno/errno.go diff --git a/biz/handler/interaction/favorite_list_server.go b/biz/handler/interaction/favorite_list_server.go index 876d40d..407d5ce 100644 --- a/biz/handler/interaction/favorite_list_server.go +++ b/biz/handler/interaction/favorite_list_server.go @@ -4,6 +4,9 @@ package interaction import ( "context" + "github.com/ClubWeGo/douyin/kitex_server" + "github.com/ClubWeGo/douyin/tools" + "github.com/ClubWeGo/douyin/tools/errno" interaction "github.com/ClubWeGo/douyin/biz/model/interaction" "github.com/cloudwego/hertz/pkg/app" @@ -17,11 +20,23 @@ func FavoriteListMethod(ctx context.Context, c *app.RequestContext) { var req interaction.FavoriteListReq err = c.BindAndValidate(&req) if err != nil { - c.String(consts.StatusBadRequest, err.Error()) + SendResponse(c, errno.NewErrNo(consts.StatusBadRequest, err.Error()), nil) return } - - resp := new(interaction.FavoriteListResp) - - c.JSON(consts.StatusOK, resp) + valid, uid, err := tools.ValidateToken(req.Token) + if err != nil { + SendResponse(c, errno.NewErrNo(consts.StatusUnauthorized, err.Error()), nil) + return + } + if !valid { + SendResponse(c, errno.NewErrNo(consts.StatusUnauthorized, "token invalid"), nil) + return + } + res, err := kitex_server.GetFavoriteList(ctx, uid) + if err != nil { + SendResponse(c, errno.RPCErr, nil) + return + } + SendResponse(c, nil, res) + return } diff --git a/biz/handler/interaction/favorite_server.go b/biz/handler/interaction/favorite_server.go index c31028d..95405a4 100644 --- a/biz/handler/interaction/favorite_server.go +++ b/biz/handler/interaction/favorite_server.go @@ -4,6 +4,9 @@ package interaction import ( "context" + "github.com/ClubWeGo/douyin/kitex_server" + "github.com/ClubWeGo/douyin/tools" + "github.com/ClubWeGo/douyin/tools/errno" interaction "github.com/ClubWeGo/douyin/biz/model/interaction" "github.com/cloudwego/hertz/pkg/app" @@ -17,11 +20,40 @@ func FavoriteMethod(ctx context.Context, c *app.RequestContext) { var req interaction.FavoriteReq err = c.BindAndValidate(&req) if err != nil { - c.String(consts.StatusBadRequest, err.Error()) + SendResponse(c, errno.NewErrNo(consts.StatusBadRequest, err.Error()), nil) return } - resp := new(interaction.FavoriteResp) + valid, uid, err := tools.ValidateToken(req.Token) + if err != nil { + SendResponse(c, errno.NewErrNo(consts.StatusUnauthorized, err.Error()), nil) + return + } - c.JSON(consts.StatusOK, resp) + if !valid { + SendResponse(c, errno.NewErrNo(consts.StatusUnauthorized, "token invalid"), nil) + return + } + + if req.ActionType == 1 { + res, err := kitex_server.AddFavorite(ctx, uid, req.VideoID) + if err != nil { + SendResponse(c, err, nil) + return + } + SendResponse(c, nil, res) + return + } else if req.ActionType == 2 { + res, err := kitex_server.DeleteFavorite(ctx, uid, req.VideoID) + if err != nil { + SendResponse(c, errno.RPCErr, nil) + return + } else { + SendResponse(c, nil, res) + return + } + } else { + SendResponse(c, errno.NewErrNo(consts.StatusBadRequest, "action type invalid"), nil) + return + } } diff --git a/biz/handler/interaction/resp.go b/biz/handler/interaction/resp.go new file mode 100644 index 0000000..acacc04 --- /dev/null +++ b/biz/handler/interaction/resp.go @@ -0,0 +1,25 @@ +package interaction + +import ( + "github.com/ClubWeGo/douyin/tools/errno" + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/protocol/consts" +) + +type Response struct { + StatusCode int32 `json:"status_code"` + StatusMessage string `json:"status_message"` +} + +// SendResponse pack response +func SendResponse(c *app.RequestContext, err error, data interface{}) { + if err != nil { + Err := errno.ConvertErr(err) + c.JSON(consts.StatusOK, Response{ + StatusCode: Err.ErrCode, + StatusMessage: Err.ErrMsg, + }) + return + } + c.JSON(consts.StatusOK, data) +} diff --git a/go.mod b/go.mod index 78bf6ee..237f21b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 replace github.com/apache/thrift => github.com/apache/thrift v0.13.0 require ( + github.com/ClubWeGo/favoritemicro v0.0.0-20230218084417-fd4c3433fb21 github.com/ClubWeGo/relationmicro v0.0.0-20230216103453-7b53cbe6042e github.com/ClubWeGo/usermicro v0.0.0-20230214161119-afa68c10bf79 github.com/ClubWeGo/videomicro v0.0.0-20230214163648-819e4cc45d4d diff --git a/go.sum b/go.sum index fb648f2..5a3b7f8 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiy dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/ClubWeGo/favoritemicro v0.0.0-20230218084417-fd4c3433fb21 h1:KJx0Td1UIReUF8Dl6SRK8uh6y+gKg4A+7k41ibfigHU= +github.com/ClubWeGo/favoritemicro v0.0.0-20230218084417-fd4c3433fb21/go.mod h1:yq3JkyV95cRDwYVrZ3d5WeDvfIzBcAC4eRYCOSH27zM= github.com/ClubWeGo/relationmicro v0.0.0-20230216103453-7b53cbe6042e h1:DK+DBcAdrOyskhh++NHQILVuawprEMjb4NcP+i7E8P0= github.com/ClubWeGo/relationmicro v0.0.0-20230216103453-7b53cbe6042e/go.mod h1:/Tc8EIH6LqyqIC4lbmoDE7RyWJVBF8tbRSGkG76j/6w= github.com/ClubWeGo/usermicro v0.0.0-20230214161119-afa68c10bf79 h1:UDT/uWXwUF6jhv0hSTCXuavaR5VHxv06obnP5q7b/i0= diff --git a/kitex_server/initmicro.go b/kitex_server/initmicro.go index b97d807..945b3e7 100644 --- a/kitex_server/initmicro.go +++ b/kitex_server/initmicro.go @@ -1,6 +1,7 @@ package kitex_server import ( + "github.com/ClubWeGo/favoritemicro/kitex_gen/favorite/favoriteservice" "log" "github.com/ClubWeGo/relationmicro/kitex_gen/relation/combineservice" @@ -13,6 +14,7 @@ import ( var Userclient userservice.Client var Videoclient videoservice.Client var Relationclient combineservice.Client +var FavoriteClient favoriteservice.Client func Init(r discovery.Resolver) { uc, err := userservice.NewClient("userservice", client.WithResolver(r)) @@ -32,4 +34,10 @@ func Init(r discovery.Resolver) { log.Fatal(err) } Relationclient = rc + + fc, err := favoriteservice.NewClient("favoriteservice", client.WithResolver(r)) + if err != nil { + log.Fatal(err) + } + FavoriteClient = fc } diff --git a/kitex_server/interaction.go b/kitex_server/interaction.go new file mode 100644 index 0000000..f021872 --- /dev/null +++ b/kitex_server/interaction.go @@ -0,0 +1,81 @@ +package kitex_server + +import ( + "context" + "github.com/ClubWeGo/douyin/biz/model/interaction" + "github.com/ClubWeGo/douyin/tools/errno" + "github.com/ClubWeGo/favoritemicro/kitex_gen/favorite" +) + +func AddFavorite(ctx context.Context, uid int64, vid int64) (*interaction.FavoriteResp, error) { + resp, err := FavoriteClient.FavoriteMethod(ctx, &favorite.FavoriteReq{ + UserId: uid, + VideoId: vid, + ActionType: 1, + }) + if err != nil { + return nil, errno.RPCErr + } + return &interaction.FavoriteResp{ + StatusCode: resp.BaseResp.StatusCode, + StatusMsg: resp.BaseResp.StatusMsg, + }, nil +} + +func DeleteFavorite(ctx context.Context, uid int64, vid int64) (*interaction.FavoriteResp, error) { + resp, err := FavoriteClient.FavoriteMethod(ctx, &favorite.FavoriteReq{ + UserId: uid, + VideoId: vid, + ActionType: 2, + }) + if err != nil { + return nil, errno.RPCErr + } + return &interaction.FavoriteResp{ + StatusCode: resp.BaseResp.StatusCode, + StatusMsg: resp.BaseResp.StatusMsg, + }, nil +} + +func GetFavoriteList(ctx context.Context, uid int64) (favorites []*interaction.FavoriteListReq, err error) { + //res, err := FavoriteClient.FavoriteListMethod(ctx, &favorite.FavoriteListReq{ + // UserId: uid, + //}) + //Videoclient.GetVideoMethod(ctx, &videomicro.GetVideoReq{}) + //if err != nil { + // return nil, errno.RPCErr + //} + + return +} + +func GetFavoriteRelation(ctx context.Context, uid int64, vid int64) (bool, error) { + res, err := FavoriteClient.FavoriteRelationMethod(ctx, &favorite.FavoriteRelationReq{ + UserId: uid, + VideoId: vid, + }) + if err != nil { + return false, errno.RPCErr + } + return res.IsFavorite, nil +} + +func CountVideoFavorite(ctx context.Context, vid int64) (cnt int64, err error) { + res, err := FavoriteClient.VideoFavoriteCountMethod(ctx, &favorite.VideoFavoriteCountReq{ + VideoId: vid, + }) + if err != nil { + return 0, errno.RPCErr + } + return res.FavoriteCount, nil +} + +func CountUserFavorite(ctx context.Context, uid int64) (int64, int64, error) { + res, err := FavoriteClient.UserFavoriteCountMethod(ctx, &favorite.UserFavoriteCountReq{ + UserId: uid, + }) + if err != nil { + return 0, 0, errno.RPCErr + } + return res.FavoriteCount, res.FavoritedCount, nil +} diff --git a/tools/errno/errno.go b/tools/errno/errno.go new file mode 100644 index 0000000..e804723 --- /dev/null +++ b/tools/errno/errno.go @@ -0,0 +1,56 @@ +package errno + +import ( + "errors" + "fmt" +) + +const ( + SuccessCode = 0 + ServiceErrCode = 10001 + ParamErrCode = 10002 + UserAlreadyExistErrCode = 10003 + AuthorizationFailedErrCode = 10004 + DBErrCode = 10005 + RPCErrCode = 10006 +) + +type ErrNo struct { + ErrCode int32 + ErrMsg string +} + +func (e ErrNo) Error() string { + return fmt.Sprintf("err_code=%d, err_msg=%s", e.ErrCode, e.ErrMsg) +} + +func NewErrNo(code int32, msg string) ErrNo { + return ErrNo{code, msg} +} + +func (e ErrNo) WithMessage(msg string) ErrNo { + e.ErrMsg = msg + return e +} + +var ( + Success = NewErrNo(SuccessCode, "Success") + ServiceErr = NewErrNo(ServiceErrCode, "Service is unable to start successfully") + ParamErr = NewErrNo(ParamErrCode, "Wrong Parameter has been given") + UserAlreadyExistErr = NewErrNo(UserAlreadyExistErrCode, "User already exists") + AuthorizationFailedErr = NewErrNo(AuthorizationFailedErrCode, "Authorization failed") + DBErr = NewErrNo(DBErrCode, "DB error") + RPCErr = NewErrNo(RPCErrCode, "RPC error") +) + +// ConvertErr convert error to Errno +func ConvertErr(err error) ErrNo { + Err := ErrNo{} + if errors.As(err, &Err) { + return Err + } + + s := ServiceErr + s.ErrMsg = err.Error() + return s +}