Skip to content

Commit

Permalink
[] (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Centurybbx authored Aug 29, 2023
1 parent cc6256b commit ba66a9e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 57 deletions.
119 changes: 98 additions & 21 deletions cmd/relation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"context"
"github.com/Tiktok-Lite/kotkit/internal/db"
"github.com/Tiktok-Lite/kotkit/kitex_gen/relation"
"github.com/Tiktok-Lite/kotkit/kitex_gen/user"
"github.com/Tiktok-Lite/kotkit/pkg/helper/constant"
"github.com/Tiktok-Lite/kotkit/pkg/helper/converter"
"github.com/Tiktok-Lite/kotkit/pkg/log"
"github.com/pkg/errors"
"gorm.io/gorm"
)

// RelationServiceImpl implements the last service interface defined in the IDL.
type RelationServiceImpl struct{}

// RelationAction implements the RelationServiceImpl interface.
func (s *RelationServiceImpl) RelationAction(ctx context.Context, req *relation.RelationActionRequest) (resp *relation.RelationActionResponse, err error) {
// TODO: Your code here...
logger := log.Logger()
claims, err := Jwt.ParseToken(req.Token)
if err != nil {
Expand All @@ -41,15 +43,15 @@ func (s *RelationServiceImpl) RelationAction(ctx context.Context, req *relation.
return nil, err
}

if req.ActionType == 1 {
if req.ActionType == constant.FollowUserCode {
res := &relation.RelationActionResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "关注成功",
}
return res, nil
}

if req.ActionType == 2 {
if req.ActionType == constant.UnFollowUserCode {
res := &relation.RelationActionResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "取关成功",
Expand Down Expand Up @@ -94,22 +96,46 @@ func (s *RelationServiceImpl) RelationFollowList(ctx context.Context, req *relat
}
return res, nil
}
userListProto, err := converter.ConvertFollowingListModelToProto(followings)
if err != nil {
logger.Errorf(err.Error())

if followings == nil {
logger.Errorf("关注列表为空")
res := &relation.RelationFollowListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取关注列表失败",
StatusCode: constant.StatusOKCode,
StatusMsg: "关注列表为空",
UserList: nil,
}
return res, nil
}

userProtoList := make([]*user.User, 0)
for _, following := range followings {
user, err := db.QueryUserByID(int64(following.UserID))
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
logger.Errorf(err.Error())
res := &relation.RelationFollowListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "关注列表获取失败",
}
return res, nil
}
userProto, err := converter.ConvertUserModelToProto(user)
if err != nil {
logger.Errorf(err.Error())
res := &relation.RelationFollowListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取关注列表失败",
UserList: nil,
}
return res, nil
}
userProtoList = append(userProtoList, userProto)
}

// 返回结果
res := &relation.RelationFollowListResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "success",
UserList: userListProto,
UserList: userProtoList,
}
return res, nil
}
Expand Down Expand Up @@ -147,22 +173,45 @@ func (s *RelationServiceImpl) RelationFollowerList(ctx context.Context, req *rel
}
return res, nil
}
userListProto, err := converter.ConvertFollowerListModelToProto(followers)
if err != nil {
logger.Errorf(err.Error())
if followers == nil {
logger.Errorf("粉丝列表为空")
res := &relation.RelationFollowerListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取粉丝列表失败",
StatusCode: constant.StatusOKCode,
StatusMsg: "粉丝列表为空",
UserList: nil,
}
return res, nil
}

userProtoList := make([]*user.User, 0)
for _, follower := range followers {
user, err := db.QueryUserByID(int64(follower.FollowerID))
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
logger.Errorf(err.Error())
res := &relation.RelationFollowerListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "粉丝列表获取失败",
}
return res, nil
}
userProto, err := converter.ConvertUserModelToProto(user)
if err != nil {
logger.Errorf(err.Error())
res := &relation.RelationFollowerListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取粉丝列表失败",
UserList: nil,
}
return res, nil
}
userProtoList = append(userProtoList, userProto)
}

// 返回结果
res := &relation.RelationFollowerListResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "success",
UserList: userListProto,
UserList: userProtoList,
}
return res, nil
}
Expand Down Expand Up @@ -202,22 +251,50 @@ func (s *RelationServiceImpl) RelationFriendList(ctx context.Context, req *relat
}
return res, nil
}
userListProto, err := converter.ConvertFollowerListModelToProto(friends)
if err != nil {
logger.Errorf(err.Error())

if friends == nil {
logger.Errorf("好友列表为空")
res := &relation.RelationFriendListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取朋友列表失败",
StatusCode: constant.StatusOKCode,
StatusMsg: "好友列表为空",
UserList: nil,
}
return res, nil
}

userProtoList := make([]*user.User, 0)
for _, friend := range friends {
user, err := db.QueryUserByID(int64(friend.FollowerID))
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
logger.Errorf(err.Error())
res := &relation.RelationFriendListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "好友列表获取失败",
}
return res, nil
}
userProto, err := converter.ConvertUserModelToProto(user)
if err != nil {
logger.Errorf(err.Error())
res := &relation.RelationFriendListResponse{
StatusCode: constant.StatusErrorCode,
StatusMsg: "内部转换错误,获取好友列表失败",
UserList: nil,
}
return res, nil
}
userProtoList = append(userProtoList, userProto)
}

for _, friend := range userProtoList {
friend.IsFollow = true
}

// 返回结果
res := &relation.RelationFriendListResponse{
StatusCode: constant.StatusOKCode,
StatusMsg: "success",
UserList: userListProto,
UserList: userProtoList,
}
return res, nil
}
2 changes: 0 additions & 2 deletions internal/db/relation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package db

import (
"fmt"
"github.com/Tiktok-Lite/kotkit/internal/model"
"github.com/Tiktok-Lite/kotkit/kitex_gen/relation"
"github.com/pkg/errors"
Expand Down Expand Up @@ -71,7 +70,6 @@ func DelRelation(userID uint, toUserID uint) error {

func GetFollowerListByUserID(UserID uint) ([]*FollowRelation, error) {
var RelationList []*FollowRelation
fmt.Println(UserID)
if err := DB().Where("user_id = ?", UserID).Find(&RelationList).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/helper/constant/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ const (
FavoriteCode = 1
UnFavoriteCode = 2
)

const (
FollowUserCode = 1
UnFollowUserCode = 2
)
34 changes: 0 additions & 34 deletions pkg/helper/converter/converter.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package converter

import (
"github.com/Tiktok-Lite/kotkit/internal/db"
"github.com/Tiktok-Lite/kotkit/internal/model"
genUser "github.com/Tiktok-Lite/kotkit/kitex_gen/user"
genVideo "github.com/Tiktok-Lite/kotkit/kitex_gen/video"
"github.com/pkg/errors"
"gorm.io/gorm"
)

func ConvertVideoModelListToProto(videoList []*model.Video) ([]*genVideo.Video, error) {
Expand All @@ -31,38 +29,6 @@ func ConvertVideoModelListToProto(videoList []*model.Video) ([]*genVideo.Video,
return res, nil
}

func ConvertFollowingListModelToProto(userList []*db.FollowRelation) ([]*genUser.User, error) {
users := make([]*genUser.User, 0)
if userList == nil {
return nil, errors.New("user is nil")
}
for _, user := range userList {
user2, err := db.QueryUserByID(int64(user.UserID))
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
userProto, _ := ConvertUserModelToProto(user2)
users = append(users, userProto)
}
return users, nil
}

func ConvertFollowerListModelToProto(userList []*db.FollowRelation) ([]*genUser.User, error) {
users := make([]*genUser.User, 0)
if userList == nil {
return nil, errors.New("user is nil")
}
for _, user := range userList {
user2, err := db.QueryUserByID(int64(user.FollowerID))
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
userProto, _ := ConvertUserModelToProto(user2)
users = append(users, userProto)
}
return users, nil
}

func ConvertUserModelToProto(user *model.User) (*genUser.User, error) {
if user == nil {
return nil, errors.New("user is nil")
Expand Down

0 comments on commit ba66a9e

Please sign in to comment.