-
Notifications
You must be signed in to change notification settings - Fork 0
/
userrepository.go
48 lines (38 loc) · 994 Bytes
/
userrepository.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
package telejoon
import (
"github.com/aliforever/go-telegram-bot-api/structs"
"sync"
)
type UserRepository interface {
UpsertUser(user *structs.User) error
SetUserState(id int64, state string) error
GetUserState(id int64) (string, error)
}
type UserI[T any] interface {
FromTgUser(tgUser *structs.User) T
}
type defaultUserRepository struct {
users sync.Map
states sync.Map
}
// NewDefaultUserRepository Factory function for defaultUserRepository.
func NewDefaultUserRepository() UserRepository {
return &defaultUserRepository{
users: sync.Map{},
states: sync.Map{},
}
}
func (u *defaultUserRepository) UpsertUser(user *structs.User) error {
u.users.Store(user.Id, user)
return nil
}
func (u *defaultUserRepository) SetUserState(id int64, state string) error {
u.states.Store(id, state)
return nil
}
func (u *defaultUserRepository) GetUserState(id int64) (string, error) {
if state, ok := u.states.Load(id); ok {
return state.(string), nil
}
return "", nil
}