Skip to content

Commit

Permalink
点赞服务实现
Browse files Browse the repository at this point in the history
  • Loading branch information
Darley-Wey committed Feb 18, 2023
1 parent 5d3a04f commit 943ec56
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 8 deletions.
25 changes: 20 additions & 5 deletions biz/handler/interaction/favorite_list_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 35 additions & 3 deletions biz/handler/interaction/favorite_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions biz/handler/interaction/resp.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 8 additions & 0 deletions kitex_server/initmicro.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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))
Expand All @@ -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
}
81 changes: 81 additions & 0 deletions kitex_server/interaction.go
Original file line number Diff line number Diff line change
@@ -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
}
56 changes: 56 additions & 0 deletions tools/errno/errno.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 943ec56

Please sign in to comment.