62 lines
923 B
Go
62 lines
923 B
Go
package store
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.t-juice.club/microfilm/users"
|
|
)
|
|
|
|
var (
|
|
ErrNoSuchUser = fmt.Errorf("no such user")
|
|
ErrUserExists = fmt.Errorf("user alread exists")
|
|
)
|
|
|
|
type MemoryStore struct {
|
|
Users map[string]users.User
|
|
}
|
|
|
|
func NewMemoryStore() *MemoryStore {
|
|
return &MemoryStore{
|
|
Users: make(map[string]users.User),
|
|
}
|
|
}
|
|
|
|
func (s *MemoryStore) AddUser(u users.User) error {
|
|
if _, ok := s.Users[u.ID]; ok {
|
|
return ErrUserExists
|
|
}
|
|
|
|
s.Users[u.ID] = u
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *MemoryStore) DeleteUser(id string) error {
|
|
if _, ok := s.Users[id]; !ok {
|
|
return ErrNoSuchUser
|
|
}
|
|
|
|
delete(s.Users, id)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *MemoryStore) UpdateUser(u users.User) error {
|
|
if _, ok := s.Users[u.ID]; !ok {
|
|
return ErrNoSuchUser
|
|
}
|
|
|
|
s.Users[u.ID] = u
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *MemoryStore) GetUser(id string) (users.User, error) {
|
|
u, ok := s.Users[id]
|
|
if !ok {
|
|
return u, ErrNoSuchUser
|
|
}
|
|
|
|
return u, nil
|
|
}
|