user.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package service
  2. import (
  3. "context"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  7. "golang.org/x/crypto/bcrypt"
  8. "time"
  9. )
  10. type UserService interface {
  11. Register(ctx context.Context, req *v1.RegisterRequest) error
  12. Login(ctx context.Context, req *v1.LoginRequest) (string, error)
  13. GetProfile(ctx context.Context, userId string) (*v1.GetProfileResponseData, error)
  14. UpdateProfile(ctx context.Context, userId string, req *v1.UpdateProfileRequest) error
  15. }
  16. func NewUserService(service *Service, userRepo repository.UserRepository) UserService {
  17. return &userService{
  18. userRepo: userRepo,
  19. Service: service,
  20. }
  21. }
  22. type userService struct {
  23. userRepo repository.UserRepository
  24. *Service
  25. }
  26. func (s *userService) Register(ctx context.Context, req *v1.RegisterRequest) error {
  27. // check username
  28. if user, err := s.userRepo.GetByEmail(ctx, req.Email); err == nil && user != nil {
  29. return v1.ErrEmailAlreadyUse
  30. }
  31. hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
  32. if err != nil {
  33. return err
  34. }
  35. // Generate user ID
  36. userId, err := s.sid.GenString()
  37. if err != nil {
  38. return err
  39. }
  40. user := &model.User{
  41. UserId: userId,
  42. Email: req.Email,
  43. Password: string(hashedPassword),
  44. }
  45. // Transaction demo
  46. err = s.tm.Transaction(ctx, func(ctx context.Context) error {
  47. // Create a user
  48. if err = s.userRepo.Create(ctx, user); err != nil {
  49. return err
  50. }
  51. // TODO: other repo
  52. return nil
  53. })
  54. return err
  55. }
  56. func (s *userService) Login(ctx context.Context, req *v1.LoginRequest) (string, error) {
  57. user, err := s.userRepo.GetByEmail(ctx, req.Email)
  58. if err != nil || user == nil {
  59. return "", v1.ErrUnauthorized
  60. }
  61. err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password))
  62. if err != nil {
  63. return "", err
  64. }
  65. token, err := s.jwt.GenToken(user.UserId, time.Now().Add(time.Hour*24*90))
  66. if err != nil {
  67. return "", err
  68. }
  69. return token, nil
  70. }
  71. func (s *userService) GetProfile(ctx context.Context, userId string) (*v1.GetProfileResponseData, error) {
  72. user, err := s.userRepo.GetByID(ctx, userId)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return &v1.GetProfileResponseData{
  77. UserId: user.UserId,
  78. Nickname: user.Nickname,
  79. }, nil
  80. }
  81. func (s *userService) UpdateProfile(ctx context.Context, userId string, req *v1.UpdateProfileRequest) error {
  82. user, err := s.userRepo.GetByID(ctx, userId)
  83. if err != nil {
  84. return err
  85. }
  86. user.Email = req.Email
  87. user.Nickname = req.Nickname
  88. if err = s.userRepo.Update(ctx, user); err != nil {
  89. return err
  90. }
  91. return nil
  92. }