diff options
Diffstat (limited to 'internal/db/redis/acct.go')
| -rw-r--r-- | internal/db/redis/acct.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/db/redis/acct.go b/internal/db/redis/acct.go new file mode 100644 index 0000000..72df741 --- /dev/null +++ b/internal/db/redis/acct.go @@ -0,0 +1,44 @@ +package redis + +import ( + "fmt" + + "github.com/gomodule/redigo/redis" + "github.com/pkg/errors" + + "git.ofmax.li/iserv/internal/acct" +) + +// AcctRepo account +type AcctRepo struct { + db *redis.Pool +} + +var ( + acctProfileKey string = "acct:email:%s" +) + +// NewAcctRepo account repo +func NewAcctRepo(conn *redis.Pool) *AcctRepo { + return &AcctRepo{ + conn, + } +} + +// UpdateAcctProfile write profile to redis +func (db *AcctRepo) UpdateAcctProfile(p acct.Profile) error { + conn := db.db.Get() + defer conn.Close() + profileID, profileKeyValues := p.ProfileKeyValues() + acctProfileKey := fmt.Sprintf(acctProfileKey, profileID) + profileArgs := redis.Args{}.Add(acctProfileKey).AddFlat(profileKeyValues) + res, err := redis.String(conn.Do("HMSET", profileArgs...)) + if err != nil { + fmt.Print(err) + return err + } + if res != "OK" { + return errors.Errorf("%s acct was not saved", acctProfileKey) + } + return nil +} |