-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
52 lines (42 loc) · 1.22 KB
/
user.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
package memorystorage
import (
"context"
"github.com/dantin/cubit/model"
)
// User represents an in-memory user storage.
type User struct {
*memoryStorage
}
// NewUser returns an instance of User in-memory storage.
func NewUser() *User {
return &User{memoryStorage: newStorage()}
}
// UpsertUser inserts a new user entity into storage, or updates it if previously inserted.
func (m *User) UpsertUser(_ context.Context, user *model.User) error {
return m.saveEntity(userKey(user.Username), user)
}
// DeleteUser deletes a user entity from storage.
func (m *User) DeleteUser(ctx context.Context, username string) error {
return m.deleteKey(userKey(username))
}
// FetchUser retrieves a user entity from storage.
func (m *User) FetchUser(_ context.Context, username string) (*model.User, error) {
var user model.User
ok, err := m.getEntity(userKey(username), &user)
switch err {
case nil:
if ok {
return &user, nil
}
return nil, nil
default:
return nil, err
}
}
// UserExists tells whether or not a user exists within storage.
func (m *User) UserExists(_ context.Context, username string) (bool, error) {
return m.keyExists(userKey(username))
}
func userKey(username string) string {
return "users:" + username
}