Skip to content

Commit

Permalink
update: modify user data query logic and move codes logic from data l…
Browse files Browse the repository at this point in the history
…ayer to biz layer (#80)
  • Loading branch information
alilestera authored Sep 6, 2023
1 parent 9653bd0 commit 6e3a381
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 384 deletions.
5 changes: 3 additions & 2 deletions app/user/service/cmd/wire_gen.go

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

83 changes: 56 additions & 27 deletions app/user/service/internal/biz/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

var (
ErrUserNotFound = errors.New("user not found")
ErrInternal = errors.New("internal error")
ErrUserNotFound = errors.New("无法找到此用户")
ErrInternal = errors.New("服务内部错误")
)

// User is a user model.
Expand All @@ -34,11 +34,11 @@ type User struct {
Token string
}

// UserRepo is a user repo.
// UserRepo 定义user存储的方法集合
type UserRepo interface {
Create(context.Context, *User) (*User, error)
FindById(context.Context, uint32) (*User, error)
FindByIds(context.Context, uint32, []uint32) ([]*User, error)
FindByIds(context.Context, []uint32) ([]*User, error)
FindByUsername(context.Context, string) (*User, error)
InitUpdateFollowQueue()
InitUpdateFollowerQueue()
Expand All @@ -47,28 +47,36 @@ type UserRepo interface {
InitUpdateFavoriteQueue()
}

// UserUsecase is a user usecase.
// RelationRepo 定义向relation服务请求的方法集合
type RelationRepo interface {
IsFollow(ctx context.Context, userId uint32, toUserId []uint32) ([]bool, error)
}

// UserUsecase 是user的用例
type UserUsecase struct {
repo UserRepo
conf *conf.JWT
log *log.Helper
userRepo UserRepo
relationRepo RelationRepo
conf *conf.JWT
log *log.Helper
}

// NewUserUsecase new a user usecase.
func NewUserUsecase(repo UserRepo, conf *conf.JWT, logger log.Logger) *UserUsecase {
go repo.InitUpdateFavoredQueue()
go repo.InitUpdateFollowQueue()
go repo.InitUpdateFollowerQueue()
go repo.InitUpdatePublishQueue()
go repo.InitUpdateFavoriteQueue()
return &UserUsecase{repo: repo, conf: conf, log: log.NewHelper(logger)}
func NewUserUsecase(userRepo UserRepo, relationRepo RelationRepo, conf *conf.JWT, logger log.Logger) *UserUsecase {
logs := log.NewHelper(log.With(logger, "biz", "user_usecase"))
uc := &UserUsecase{
userRepo: userRepo,
relationRepo: relationRepo,
conf: conf,
log: logs,
}
uc.updateWorker()
return uc
}

// Register .
func (uc *UserUsecase) Register(ctx context.Context, username, password string) (*User, error) {
_, err := uc.repo.FindByUsername(ctx, username)
_, err := uc.userRepo.FindByUsername(ctx, username)
if err == nil {
return nil, errors.New("the username has been registered")
return nil, errors.New("用户名已被注册")
}
if !errors.Is(err, ErrUserNotFound) {
return nil, ErrInternal
Expand All @@ -82,7 +90,7 @@ func (uc *UserUsecase) Register(ctx context.Context, username, password string)
// Name与Username相同
Name: username,
}
user, err := uc.repo.Create(ctx, regUser)
user, err := uc.userRepo.Create(ctx, regUser)
if err != nil {
return nil, ErrInternal
}
Expand All @@ -98,17 +106,17 @@ func (uc *UserUsecase) Register(ctx context.Context, username, password string)

// Login .
func (uc *UserUsecase) Login(ctx context.Context, username, password string) (*User, error) {
user, err := uc.repo.FindByUsername(ctx, username)
user, err := uc.userRepo.FindByUsername(ctx, username)
if errors.Is(err, ErrUserNotFound) {
return nil, errors.New("can not find registered user")
return nil, ErrUserNotFound
}
if err != nil {
return nil, ErrInternal
}

password = common.GenSaltPassword(username, password)
if user.Password != password {
return nil, errors.New("incorrect password")
return nil, errors.New("密码不正确")
}

// 生成 token
Expand All @@ -122,26 +130,47 @@ func (uc *UserUsecase) Login(ctx context.Context, username, password string) (*U

// GetInfo .
func (uc *UserUsecase) GetInfo(ctx context.Context, userId uint32) (*User, error) {
user, err := uc.repo.FindById(ctx, userId)
user, err := uc.userRepo.FindById(ctx, userId)
if err != nil {
return nil, ErrInternal
}
if user.Username == "" {
return nil, errors.New("can not find the user")
return nil, ErrUserNotFound
}

return user, nil
}

// GetInfos .
// GetInfos
func (uc *UserUsecase) GetInfos(ctx context.Context, userId uint32, userIds []uint32) ([]*User, error) {
users, err := uc.repo.FindByIds(ctx, userId, userIds)
users, err := uc.userRepo.FindByIds(ctx, userIds)
if err != nil {
return nil, ErrInternal
}
if len(users) == 0 {
return []*User{}, nil
}

// 添加关注详情
follows, err := uc.relationRepo.IsFollow(ctx, userId, userIds)
if err != nil {
uc.log.Errorf("请求relation服务响应失败,原因: %s", err.Error())
return nil, ErrInternal
}
if len(follows) != len(users) {
uc.log.Error("来自relation服务的响应不匹配,原因: 响应的关注数组长度与待赋值的用户信息数组长度不一致")
return nil, ErrInternal
}
for i := range users {
users[i].IsFollow = follows[i]
}
return users, nil
}

// updateWorker 执行用户信息更新的监听器
func (uc *UserUsecase) updateWorker() {
go uc.userRepo.InitUpdateFollowQueue()
go uc.userRepo.InitUpdateFollowerQueue()
go uc.userRepo.InitUpdatePublishQueue()
go uc.userRepo.InitUpdateFavoriteQueue()
go uc.userRepo.InitUpdateFavoredQueue()
}
158 changes: 0 additions & 158 deletions app/user/service/internal/biz/user_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion app/user/service/internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

// ProviderSet is data providers.
var ProviderSet = wire.NewSet(NewData, NewKafkaReader, NewGormDb, NewRedisConn, NewUserRepo)
var ProviderSet = wire.NewSet(NewData, NewKafkaReader, NewGormDb, NewRedisConn, NewUserRepo, NewRelationRepo)

var (
maxOpenConnection = 100
Expand Down
12 changes: 8 additions & 4 deletions app/user/service/internal/data/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ package data
import (
"context"

"github.com/toomanysource/atreus/app/user/service/internal/biz"

pb "github.com/toomanysource/atreus/api/relation/service/v1"
"github.com/toomanysource/atreus/app/user/service/internal/server"
)

type favoriteRepo struct {
type relationRepo struct {
client pb.RelationServiceClient
}

func NewRelationRepo(conn server.RelationConn) RelationRepo {
return &favoriteRepo{
var _ biz.RelationRepo = (*relationRepo)(nil)

func NewRelationRepo(conn server.RelationConn) biz.RelationRepo {
return &relationRepo{
client: pb.NewRelationServiceClient(conn),
}
}

// IsFollow 接收Relation服务的回应
func (u *favoriteRepo) IsFollow(ctx context.Context, userId uint32, userIds []uint32) ([]bool, error) {
func (u *relationRepo) IsFollow(ctx context.Context, userId uint32, userIds []uint32) ([]bool, error) {
resp, err := u.client.IsFollow(ctx, &pb.IsFollowRequest{UserId: userId, ToUserId: userIds})
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 6e3a381

Please sign in to comment.