aboutsummaryrefslogtreecommitdiff
path: root/internal/db/redis/acct.go
blob: 72df741ba92b0f960c7c9bcd22f7cbae861e7a6c (plain)
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
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
}