forked from ebhomengo/niki
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/adapter/redis"
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
|
)
|
|
|
|
type RepositoryOtp struct {
|
|
conn *redis.Adapter
|
|
}
|
|
|
|
func NewRepositoryOtp(conn *redis.Adapter) RepositoryOtp {
|
|
return RepositoryOtp{conn: conn}
|
|
}
|
|
|
|
func (r RepositoryOtp) IsExistPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) {
|
|
const op = "RepositoryOtp.IsExistPhoneNumber"
|
|
|
|
result, err := r.conn.Client().Exists(ctx, phoneNumber).Result()
|
|
if err != nil {
|
|
return false, richerror.New(op).WithKind(richerror.KindUnexpected).WithErr(err)
|
|
}
|
|
|
|
if result == 0 {
|
|
return false, nil
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func (r RepositoryOtp) SaveCodeWithPhoneNumber(ctx context.Context, phoneNumber string, code string, expireTime time.Duration) error {
|
|
const op = "RepositoryOtp.SaveCodeWithPhoneNumber"
|
|
|
|
_, err := r.conn.Client().Set(ctx, phoneNumber, code, expireTime).Result()
|
|
if err != nil {
|
|
return richerror.New(op).WithKind(richerror.KindUnexpected).WithErr(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r RepositoryOtp) GetCodeByPhoneNumber(ctx context.Context, phoneNumber string) (string, error) {
|
|
const op = "RepositoryOtp.GetCodeByPhoneNumber"
|
|
|
|
result, err := r.conn.Client().Get(ctx, phoneNumber).Result()
|
|
if err != nil {
|
|
return "", richerror.New(op).WithKind(richerror.KindUnexpected).WithErr(err)
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (r RepositoryOtp) DeleteCodeByPhoneNumber(ctx context.Context, PhoneNumber string) (bool, error) {
|
|
const op = "RepositoryOtp.DeleteCodeByPhoneNumber"
|
|
success, err := r.conn.Client().Del(ctx, PhoneNumber).Result()
|
|
if err != nil {
|
|
return false, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
|
}
|
|
if success != 1 {
|
|
return false, nil
|
|
}
|
|
|
|
return true, nil
|
|
}
|