From 7e88621aa1d0b87fb369993553cedb8c6c5759c0 Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Tue, 4 Jan 2022 12:56:10 +0800 Subject: [PATCH] Refactor code --- internal/rpc/friend/firend.go | 1 - pkg/common/db/model_struct.go | 6 +++--- .../mysql_model/im_mysql_model/friend_model.go | 3 +++ pkg/utils/utils.go | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/internal/rpc/friend/firend.go b/internal/rpc/friend/firend.go index 6d2348324..14de9bb20 100644 --- a/internal/rpc/friend/firend.go +++ b/internal/rpc/friend/firend.go @@ -288,7 +288,6 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFri log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID) return &pbFriend.DeleteFriendResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil } - err := imdb.DeleteSingleFriendInfo(req.CommID.FromUserID, req.CommID.ToUserID) if err != nil { log.NewError(req.CommID.OperationID, "DeleteSingleFriendInfo failed", err.Error(), req.CommID.FromUserID, req.CommID.ToUserID) diff --git a/pkg/common/db/model_struct.go b/pkg/common/db/model_struct.go index c2ab763c5..efd0d980e 100644 --- a/pkg/common/db/model_struct.go +++ b/pkg/common/db/model_struct.go @@ -14,10 +14,10 @@ import "time" //} //open_im_sdk.FriendInfo(FriendUser) != imdb.Friend(FriendUserID) type Friend struct { - OwnerUserID string `gorm:"column:owner_user_id;primaryKey;"` + OwnerUserID string `gorm:"column:owner_user_id;primary_key;"` Remark string `gorm:"column:remark"` CreateTime time.Time `gorm:"column:create_time"` - FriendUserID string `gorm:"column:friend_user_id;primaryKey;"` + FriendUserID string `gorm:"column:friend_user_id;primary_key;"` AddSource int32 `gorm:"column:add_source"` OperatorUserID string `gorm:"column:operator_user_id"` Ex string `gorm:"column:ex"` @@ -159,7 +159,7 @@ type User struct { //} // open_im_sdk.BlackInfo(BlackUserInfo) != imdb.Black (BlockUserID) type Black struct { - OwnerUserID string `gorm:"column:owner_user_id;primaryKey;"` + OwnerUserID string `gorm:"column:owner_user_id;primaryKey;size:256"` CreateTime time.Time `gorm:"column:create_time"` BlockUserID string `gorm:"column:block_user_id;primaryKey;"` AddSource int32 `gorm:"column:add_source"` diff --git a/pkg/common/db/mysql_model/im_mysql_model/friend_model.go b/pkg/common/db/mysql_model/im_mysql_model/friend_model.go index aa527698f..b8de9b120 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/friend_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/friend_model.go @@ -39,7 +39,10 @@ func GetFriendListByUserID(OwnerUserID string) ([]db.Friend, error) { return nil, err } var friends []db.Friend + var x db.Friend + x.OwnerUserID = OwnerUserID err = dbConn.Table("friend").Where("owner_user_id=?", OwnerUserID).Find(&friends).Error + if err != nil { return nil, err } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 224e459e3..2e8274207 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -2,9 +2,25 @@ package utils import ( "github.com/jinzhu/copier" + "github.com/pkg/errors" + "runtime" + "strconv" ) // copy a by b b->a func CopyStructFields(a interface{}, b interface{}, fields ...string) (err error) { return copier.Copy(a, b) } + +func Wrap(err error, message string) error { + return errors.Wrap(err, "==> "+printCallerNameAndLine()+message) +} + +func WithMessage(err error, message string) error { + return errors.WithMessage(err, "==> "+printCallerNameAndLine()+message) +} + +func printCallerNameAndLine() string { + pc, _, line, _ := runtime.Caller(2) + return runtime.FuncForPC(pc).Name() + "()@" + strconv.Itoa(line) + ": " +}