forked from hwholiday/learning_tools
/
atuh_code.go
62 lines (55 loc) · 2.15 KB
/
atuh_code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package repository
import (
"context"
"fmt"
"github.com/go-redis/redis"
"github.com/hwholiday/learning_tools/ddd-auth2-example/domain/obj"
"github.com/hwholiday/learning_tools/ddd-auth2-example/domain/repo"
consts "github.com/hwholiday/learning_tools/ddd-auth2-example/infrastructure/conf"
"github.com/hwholiday/learning_tools/ddd-auth2-example/infrastructure/pkg/hcode"
"github.com/hwholiday/learning_tools/ddd-auth2-example/infrastructure/pkg/log"
"go.uber.org/zap"
"time"
)
var _ repo.AuthCodeRepo = (*AuthCode)(nil)
type AuthCode struct {
repository
}
func (a *AuthCode) getCacheKey(data string) string {
return fmt.Sprintf("%s%s", consts.AuthCodeCacheKey, data)
}
func (a *AuthCode) CreateCode(ctx context.Context, data obj.CodeOpenId) error {
saveData, err := Marshal(&data)
if err != nil {
log.GetLogger().Error("[AuthToken] CreateAuthToken Marshal", zap.Any("req", data), zap.Error(err))
return hcode.RedisExecErr
}
if err := a.rds.Set(a.getCacheKey(data.Code), saveData, time.Second*60*2).Err(); err != nil {
log.GetLogger().Error("[AuthCode] CreateCode Set", zap.Any("req", data), zap.Error(err))
return hcode.RedisExecErr
}
return nil
}
func (a *AuthCode) DelCode(ctx context.Context, repo repo.AuthCodeSpecificationRepo) error {
if err := a.rds.Del(a.getCacheKey(fmt.Sprint(repo.ToSql(ctx)))).Err(); err != nil {
log.GetLogger().Error("[AuthCode] DelCode Del", zap.Any("req", repo.ToSql(ctx)), zap.Error(err))
return hcode.RedisExecErr
}
return nil
}
func (a *AuthCode) QueryCode(ctx context.Context, repo repo.AuthCodeSpecificationRepo) (obj.CodeOpenId, error) {
saveData, err := a.rds.Get(a.getCacheKey(fmt.Sprint(repo.ToSql(ctx)))).Bytes()
if err != nil {
if err == redis.Nil {
return obj.CodeOpenId{}, hcode.RedisExecErr
}
log.GetLogger().Error("[AuthCode] QueryCode Get", zap.Any("req", repo.ToSql(ctx)), zap.Error(err))
return obj.CodeOpenId{}, hcode.RedisExecErr
}
var codeOpenId obj.CodeOpenId
if err := Unmarshal(saveData, &codeOpenId); err != nil {
log.GetLogger().Error("[QueryAuthToken] Unmarshal", zap.Any("req", repo.ToSql(ctx)), zap.Error(err))
return obj.CodeOpenId{}, hcode.RedisExecErr
}
return codeOpenId, nil
}