users/store/memory.go

62 lines
923 B
Go
Raw Normal View History

2023-10-19 21:15:55 +00:00
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
}