golang-im聊天
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

66 lines
1.3 KiB

  1. package app
  2. import (
  3. "context"
  4. "gim/internal/business/domain/user/repo"
  5. "gim/pkg/pb"
  6. "time"
  7. )
  8. type userApp struct{}
  9. var UserApp = new(userApp)
  10. func (*userApp) Get(ctx context.Context, userId int64) (*pb.User, error) {
  11. user, err := repo.UserRepo.Get(userId)
  12. return user.ToProto(), err
  13. }
  14. func (*userApp) Update(ctx context.Context, userId int64, req *pb.UpdateUserReq) error {
  15. u, err := repo.UserRepo.Get(userId)
  16. if err != nil {
  17. return err
  18. }
  19. if u == nil {
  20. return nil
  21. }
  22. u.Nickname = req.Nickname
  23. u.Sex = req.Sex
  24. u.AvatarUrl = req.AvatarUrl
  25. u.Extra = req.Extra
  26. u.UpdateTime = time.Now()
  27. err = repo.UserRepo.Save(u)
  28. if err != nil {
  29. return err
  30. }
  31. return nil
  32. }
  33. func (*userApp) GetByIds(ctx context.Context, userIds []int64) (map[int64]*pb.User, error) {
  34. users, err := repo.UserRepo.GetByIds(userIds)
  35. if err != nil {
  36. return nil, err
  37. }
  38. pbUsers := make(map[int64]*pb.User, len(users))
  39. for i := range users {
  40. pbUsers[users[i].Id] = users[i].ToProto()
  41. }
  42. return pbUsers, nil
  43. }
  44. func (*userApp) Search(ctx context.Context, key string, masterId int64) ([]*pb.User, error) {
  45. users, err := repo.UserRepo.Search(key, masterId)
  46. if err != nil {
  47. return nil, err
  48. }
  49. pbUsers := make([]*pb.User, len(users))
  50. for i, v := range users {
  51. pbUsers[i] = v.ToProto()
  52. }
  53. return pbUsers, nil
  54. }