gifs/api

Paddy 2014-10-17 Parent:08ec88016e2f

3:96aac3ae74e6 Go to Latest

gifs/api/memstore.go

Remove user info from memstore. Remove all the user related methods and properties from the memstore, as those are all going to be superseded by code.secondbit.org/auth anyways.

History
     1.1 --- a/memstore.go	Fri Oct 17 07:12:23 2014 -0400
     1.2 +++ b/memstore.go	Fri Oct 17 07:13:23 2014 -0400
     1.3 @@ -3,15 +3,13 @@
     1.4  import (
     1.5  	"time"
     1.6  
     1.7 -	"secondbit.org/uuid"
     1.8 +	"code.secondbit.org/uuid.hg"
     1.9  )
    1.10  
    1.11  type Memstore struct {
    1.12  	collections map[string]Collection
    1.13  	domains     map[string]Domain
    1.14  	items       map[string]Item
    1.15 -	users       map[string]User
    1.16 -	logins      map[string]Login
    1.17  }
    1.18  
    1.19  func NewMemstore() Memstore {
    1.20 @@ -19,8 +17,6 @@
    1.21  		collections: map[string]Collection{},
    1.22  		domains:     map[string]Domain{},
    1.23  		items:       map[string]Item{},
    1.24 -		users:       map[string]User{},
    1.25 -		logins:      map[string]Login{},
    1.26  	}
    1.27  }
    1.28  
    1.29 @@ -29,18 +25,20 @@
    1.30  	return nil
    1.31  }
    1.32  
    1.33 -func (m Memstore) UpdateCollection(c Collection) error {
    1.34 -	if _, ok := m.collections[c.ID.String()]; !ok {
    1.35 -		return CollectionNotFoundError
    1.36 +func (m Memstore) UpdateCollection(id uuid.ID, change CollectionChange) error {
    1.37 +	c, ok := m.collections[id.String()]
    1.38 +	if !ok {
    1.39 +		return ErrCollectionNotFound
    1.40  	}
    1.41 -	m.collections[c.ID.String()] = c
    1.42 +	c.ApplyChange(change)
    1.43 +	m.collections[id.String()] = c
    1.44  	return nil
    1.45  }
    1.46  
    1.47  func (m Memstore) GetCollectionByDomain(domain string) (Collection, error) {
    1.48  	d, ok := m.domains[domain]
    1.49  	if !ok {
    1.50 -		return Collection{}, CollectionNotFoundError
    1.51 +		return Collection{}, ErrCollectionNotFound
    1.52  	}
    1.53  	return m.GetCollectionByID(d.CollectionID)
    1.54  }
    1.55 @@ -49,7 +47,7 @@
    1.56  	if c, ok := m.collections[id.String()]; ok {
    1.57  		return c, nil
    1.58  	}
    1.59 -	return Collection{}, CollectionNotFoundError
    1.60 +	return Collection{}, ErrCollectionNotFound
    1.61  }
    1.62  
    1.63  func (m Memstore) GetCollectionsByUser(id uuid.ID) ([]Collection, error) {
    1.64 @@ -64,7 +62,7 @@
    1.65  
    1.66  func (m Memstore) AddDomainToCollection(id uuid.ID, domain string) error {
    1.67  	if _, ok := m.domains[domain]; ok {
    1.68 -		return DomainAlreadyExistsError
    1.69 +		return ErrDomainAlreadyExists
    1.70  	}
    1.71  	m.domains[domain] = Domain{
    1.72  		Domain:       domain,
    1.73 @@ -76,7 +74,7 @@
    1.74  
    1.75  func (m Memstore) RemoveDomainFromCollection(id uuid.ID, domain string) error {
    1.76  	if _, ok := m.domains[domain]; !ok {
    1.77 -		return DomainNotFoundError
    1.78 +		return ErrDomainNotFound
    1.79  	}
    1.80  	delete(m.domains, domain)
    1.81  	return nil
    1.82 @@ -94,7 +92,7 @@
    1.83  
    1.84  func (m Memstore) DeleteCollection(c Collection) error {
    1.85  	if _, ok := m.collections[c.ID.String()]; !ok {
    1.86 -		return CollectionNotFoundError
    1.87 +		return ErrCollectionNotFound
    1.88  	}
    1.89  	delete(m.collections, c.ID.String())
    1.90  	return nil
    1.91 @@ -110,7 +108,7 @@
    1.92  
    1.93  func (m Memstore) GetItemsByCollectionID(id uuid.ID, num, offset int) ([]Item, error) {
    1.94  	if _, ok := m.collections[id.String()]; !ok {
    1.95 -		return []Item{}, CollectionNotFoundError
    1.96 +		return []Item{}, ErrCollectionNotFound
    1.97  	}
    1.98  	items := []Item{}
    1.99  	for _, item := range m.items {
   1.100 @@ -130,10 +128,10 @@
   1.101  
   1.102  func (m Memstore) AddItemToCollection(id uuid.ID, item Item) error {
   1.103  	if _, ok := m.collections[id.String()]; !ok {
   1.104 -		return CollectionNotFoundError
   1.105 +		return ErrCollectionNotFound
   1.106  	}
   1.107  	if _, ok := m.items[id.String()+"/"+item.Name]; ok {
   1.108 -		return ItemAlreadyExistsError
   1.109 +		return ErrItemAlreadyExists
   1.110  	}
   1.111  	item.CollectionID = id
   1.112  	m.items[id.String()+"/"+item.Name] = item
   1.113 @@ -142,87 +140,18 @@
   1.114  
   1.115  func (m Memstore) GetItemByName(collectionID uuid.ID, name string) (Item, error) {
   1.116  	if _, ok := m.collections[collectionID.String()]; !ok {
   1.117 -		return Item{}, CollectionNotFoundError
   1.118 +		return Item{}, ErrCollectionNotFound
   1.119  	}
   1.120  	if item, ok := m.items[collectionID.String()+"/"+name]; ok {
   1.121  		return item, nil
   1.122  	}
   1.123 -	return Item{}, ItemNotFoundError
   1.124 +	return Item{}, ErrItemNotFound
   1.125  }
   1.126  
   1.127  func (m Memstore) DeleteItem(item Item) error {
   1.128  	if _, ok := m.items[item.CollectionID.String()+"/"+item.Name]; !ok {
   1.129 -		return ItemNotFoundError
   1.130 +		return ErrItemNotFound
   1.131  	}
   1.132  	delete(m.items, item.CollectionID.String()+"/"+item.Name)
   1.133  	return nil
   1.134  }
   1.135 -
   1.136 -func (m Memstore) GetUserByID(id uuid.ID) (User, error) {
   1.137 -	if u, ok := m.users[id.String()]; ok {
   1.138 -		return u, nil
   1.139 -	}
   1.140 -	return User{}, UserNotFoundError
   1.141 -}
   1.142 -
   1.143 -func (m Memstore) GetUserByLogin(loginType, value, passphrase string) (User, error) {
   1.144 -	login, ok := m.logins[loginType+":"+value]
   1.145 -	if !ok {
   1.146 -		return User{}, UserNotFoundError
   1.147 -	}
   1.148 -	user, err := m.GetUserByID(login.UserID)
   1.149 -	if err != nil {
   1.150 -		return user, err
   1.151 -	}
   1.152 -	if user.Passphrase != passphrase {
   1.153 -		return User{}, UserNotFoundError
   1.154 -	}
   1.155 -	return user, nil
   1.156 -}
   1.157 -
   1.158 -func (m Memstore) CreateUser(u User) error {
   1.159 -	m.users[u.ID.String()] = u
   1.160 -	return nil
   1.161 -}
   1.162 -
   1.163 -func (m Memstore) AddLoginToUser(login Login, user uuid.ID) error {
   1.164 -	if _, ok := m.logins[login.Type+":"+login.Value]; ok {
   1.165 -		return LoginAlreadyExistsError
   1.166 -	}
   1.167 -	m.logins[login.Type+":"+login.Value] = login
   1.168 -	return nil
   1.169 -}
   1.170 -
   1.171 -func (m Memstore) RemoveLoginFromUser(loginType, value string, user uuid.ID) error {
   1.172 -	if _, ok := m.logins[loginType+":"+value]; !ok {
   1.173 -		return LoginNotFoundError
   1.174 -	}
   1.175 -	delete(m.logins, loginType+":"+value)
   1.176 -	return nil
   1.177 -}
   1.178 -
   1.179 -func (m Memstore) GetLoginsByUser(user uuid.ID) ([]Login, error) {
   1.180 -	logins := []Login{}
   1.181 -	for _, login := range m.logins {
   1.182 -		if login.UserID.Equal(user) {
   1.183 -			logins = append(logins, login)
   1.184 -		}
   1.185 -	}
   1.186 -	return logins, nil
   1.187 -}
   1.188 -
   1.189 -func (m Memstore) UpdateUser(u User) error {
   1.190 -	if _, ok := m.users[u.ID.String()]; !ok {
   1.191 -		return UserNotFoundError
   1.192 -	}
   1.193 -	m.users[u.ID.String()] = u
   1.194 -	return nil
   1.195 -}
   1.196 -
   1.197 -func (m Memstore) DeleteUser(u User) error {
   1.198 -	if _, ok := m.users[u.ID.String()]; !ok {
   1.199 -		return UserNotFoundError
   1.200 -	}
   1.201 -	delete(m.users, u.ID.String())
   1.202 -	return nil
   1.203 -}