answer/internal/repo/captcha/captcha.go

99 lines
2.7 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package captcha
import (
"context"
2023-07-17 17:44:08 +08:00
"encoding/json"
2022-09-27 17:59:05 +08:00
"fmt"
"time"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/reason"
2023-07-17 17:44:08 +08:00
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/action"
2022-09-27 17:59:05 +08:00
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
2022-09-27 17:59:05 +08:00
)
// captchaRepo captcha repository
type captchaRepo struct {
data *data.Data
}
// NewCaptchaRepo new repository
func NewCaptchaRepo(data *data.Data) action.CaptchaRepo {
return &captchaRepo{
data: data,
}
}
2023-07-17 18:46:09 +08:00
func (cr *captchaRepo) SetActionType(ctx context.Context, unit, actionType, config string, amount int) (err error) {
2023-07-17 17:44:08 +08:00
now := time.Now()
cacheKey := fmt.Sprintf("ActionRecord:%s@%s@%s", unit, actionType, now.Format("2006-1-02"))
value := &entity.ActionRecordInfo{}
2023-07-18 18:11:18 +08:00
value.LastTime = now.Unix()
2023-07-17 17:44:08 +08:00
value.Num = amount
valueStr, err := json.Marshal(value)
if err != nil {
return nil
}
err = cr.data.Cache.SetString(ctx, cacheKey, string(valueStr), 6*time.Minute)
2022-09-27 17:59:05 +08:00
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
2023-08-22 16:10:41 +08:00
func (cr *captchaRepo) GetActionType(ctx context.Context, unit, actionType string) (actionInfo *entity.ActionRecordInfo, err error) {
2023-07-17 17:44:08 +08:00
now := time.Now()
cacheKey := fmt.Sprintf("ActionRecord:%s@%s@%s", unit, actionType, now.Format("2006-1-02"))
2023-08-22 16:10:41 +08:00
res, exist, err := cr.data.Cache.GetString(ctx, cacheKey)
2022-09-27 17:59:05 +08:00
if err != nil {
2023-08-22 16:10:41 +08:00
return nil, err
2022-09-27 17:59:05 +08:00
}
if !exist {
return nil, nil
2023-07-17 17:44:08 +08:00
}
actionInfo = &entity.ActionRecordInfo{}
_ = json.Unmarshal([]byte(res), actionInfo)
2023-08-22 16:10:41 +08:00
return actionInfo, nil
2022-09-27 17:59:05 +08:00
}
2023-07-17 17:44:08 +08:00
func (cr *captchaRepo) DelActionType(ctx context.Context, unit, actionType string) (err error) {
now := time.Now()
cacheKey := fmt.Sprintf("ActionRecord:%s@%s@%s", unit, actionType, now.Format("2006-1-02"))
2022-09-27 17:59:05 +08:00
err = cr.data.Cache.Del(ctx, cacheKey)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
// SetCaptcha set captcha to cache
func (cr *captchaRepo) SetCaptcha(ctx context.Context, key, captcha string) (err error) {
err = cr.data.Cache.SetString(ctx, key, captcha, 6*time.Minute)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
// GetCaptcha get captcha from cache
func (cr *captchaRepo) GetCaptcha(ctx context.Context, key string) (captcha string, err error) {
2023-08-22 16:10:41 +08:00
captcha, exist, err := cr.data.Cache.GetString(ctx, key)
2022-09-27 17:59:05 +08:00
if err != nil {
2023-08-22 16:10:41 +08:00
return "", err
}
if !exist {
return "", fmt.Errorf("captcha not exist")
2022-09-27 17:59:05 +08:00
}
return captcha, nil
}
2023-02-24 16:30:21 +08:00
func (cr *captchaRepo) DelCaptcha(ctx context.Context, key string) (err error) {
err = cr.data.Cache.Del(ctx, key)
if err != nil {
log.Debug(err)
}
return nil
}