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(identifier string) (users.User, error) { u, ok := s.Users[identifier] if !ok { // Check if identifier is username for _, u := range s.Users { if u.Username == identifier { return u, nil } } return u, ErrNoSuchUser } return u, nil }