provider에도 Query func 추가

This commit is contained in:
2023-08-30 16:35:22 +09:00
parent 66a191f494
commit 021f183157
2 changed files with 30 additions and 4 deletions

View File

@ -11,8 +11,9 @@ import (
)
type Provider interface {
Update(key string, input *Authorization) error
Delete(key string) error
Update(string, *Authorization) error
Delete(string) error
Query(string) (*Authorization, error)
}
type provider_redis struct {
@ -91,6 +92,22 @@ func (p *provider_redis) Delete(key string) error {
return err
}
func (p *provider_redis) Query(key string) (*Authorization, error) {
payload, err := p.redisClient.Get(p.ctx, key).Result()
if err == redis.Nil {
return nil, nil
} else if err != nil {
return nil, err
}
var auth Authorization
if err := bson.Unmarshal([]byte(payload), &auth); err != nil {
return nil, err
}
return &auth, nil
}
func (p *provider_mongo) Update(key string, input *Authorization) error {
_, _, err := p.mongoClient.Update(session_collection_name, bson.M{
"key": key,
@ -110,3 +127,12 @@ func (p *provider_mongo) Delete(key string) error {
})
return err
}
func (p *provider_mongo) Query(key string) (*Authorization, error) {
var auth Authorization
err := p.mongoClient.FindOneAs(session_collection_name, bson.M{
"key": key,
}, &auth)
return &auth, err
}