github.com/ars0915/glossika-exercise@v0.0.0-20240904180321-a5617a00d892/usecase/user.go (about) 1 package usecase 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 8 "github.com/go-redis/redis/v8" 9 "github.com/pkg/errors" 10 "golang.org/x/crypto/bcrypt" 11 "gorm.io/gorm" 12 13 "github.com/ars0915/glossika-exercise/constant" 14 "github.com/ars0915/glossika-exercise/entity" 15 "github.com/ars0915/glossika-exercise/repo" 16 "github.com/ars0915/glossika-exercise/util" 17 "github.com/ars0915/glossika-exercise/util/cTypes" 18 ) 19 20 type RegisterParam struct { 21 Email string 22 Password string 23 } 24 25 func (h UserHandler) Register(ctx context.Context, param RegisterParam) error { 26 hashedPassword, err := bcrypt.GenerateFromPassword([]byte(param.Password), bcrypt.DefaultCost) 27 if err != nil { 28 return errors.Wrap(err, "bcrypt.GenerateFromPassword") 29 } 30 31 verificationCode, err := GenerateRandomString(6) 32 if err != nil { 33 return errors.Wrap(err, "GenerateRandomString") 34 } 35 36 return repo.WithinTransaction(ctx, h.db, func(txCtx context.Context) error { 37 tx := repo.ExtractTx(txCtx) 38 39 user, err := tx.CreateUser(entity.User{ 40 Email: cTypes.String(param.Email), 41 Password: cTypes.String(string(hashedPassword)), 42 EmailVerified: cTypes.Bool(false), 43 VerificationCode: cTypes.String(verificationCode), 44 }) 45 if err != nil { 46 if IsDuplicateError(err) { 47 return ErrorEmailRegistered 48 } 49 return errors.Wrap(err, "CreateUser") 50 } 51 52 if err = h.email.SendVerificationEmail(*user.Email, *user.VerificationCode); err != nil { 53 return errors.Wrap(err, "SendVerificationEmail") 54 } 55 56 return nil 57 }) 58 } 59 60 type VerifyUserParam struct { 61 Email string 62 VerificationCode string 63 } 64 65 func (h UserHandler) Verify(ctx context.Context, param VerifyUserParam) error { 66 return repo.WithinTransaction(ctx, h.db, func(txCtx context.Context) error { 67 tx := repo.ExtractTx(txCtx) 68 69 user, err := tx.GetUserForUpdate(param.Email) 70 if err != nil { 71 if errors.Is(err, gorm.ErrRecordNotFound) { 72 return ErrorUserNotFound 73 } 74 return errors.Wrap(err, "GetUserForUpdate") 75 } 76 77 if *user.VerificationCode != param.VerificationCode { 78 return ErrorUserVerificationFailed 79 } 80 81 if err = tx.UpdateUser(user.ID, entity.User{ 82 EmailVerified: cTypes.Bool(true), 83 }); err != nil { 84 return errors.Wrap(err, "UpdateUser") 85 } 86 87 return nil 88 }) 89 } 90 91 type LoginParam struct { 92 Email string 93 Password string 94 } 95 96 func (h UserHandler) Login(ctx context.Context, param LoginParam) (token string, err error) { 97 user, err := h.db.GetUser(param.Email) 98 if err != nil { 99 if errors.Is(err, gorm.ErrRecordNotFound) { 100 return "", ErrorUserNotFound 101 } 102 return "", errors.Wrap(err, "GetUser") 103 } 104 105 if !*user.EmailVerified { 106 return "", ErrorUserUnverified 107 } 108 109 if err = bcrypt.CompareHashAndPassword([]byte(*user.Password), []byte(param.Password)); err != nil { 110 return "", ErrorPasswordVerificationFailed 111 } 112 113 token, err = util.GenerateJWT(user.ID) 114 if err != nil { 115 return "", errors.Wrap(err, "GenerateJWT") 116 } 117 118 return token, nil 119 } 120 121 func (h UserHandler) GetRecommendProducts(ctx context.Context, userID uint) ([]entity.Product, error) { 122 cacheKey := fmt.Sprintf("%s%d", constant.CacheKeyPrefixRecommend, userID) 123 productCached, err := h.redis.Get(ctx, cacheKey) 124 if err != nil { 125 if errors.Is(err, redis.Nil) { 126 // get from DB 127 products, err := h.db.RecommendProduct() 128 if err != nil { 129 return nil, errors.Wrap(err, "db.RecommendProduct") 130 } 131 132 value, err := json.Marshal(products) 133 if err != nil { 134 return nil, errors.Wrap(err, "json.Marshal") 135 } 136 137 if err = h.redis.Set(ctx, cacheKey, value, constant.CacheRecommendProductTTL); err != nil { 138 return nil, err 139 } 140 141 return products, nil 142 } 143 } 144 145 var result []entity.Product 146 if err = json.Unmarshal([]byte(productCached), &result); err != nil { 147 return nil, errors.Wrap(err, "json.Unmarshal") 148 } 149 150 return result, nil 151 }