gifs/api
2014-08-27
Child:d3ba1115bfd0
gifs/api/datastore.go
Spike out functionality and tests. Create our interfaces around storing data and retrieving it. Create an in-memory implementation of our interfaces, for testing and rapid dev purposes. Begin sketching out what our unit tests look like. Create our Google Cloud Storage datastore implementation. Sketch out an idea for a usage collection process to keep track of which users are actually using stuff.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/datastore.go Wed Aug 27 22:34:02 2014 -0400 1.3 @@ -0,0 +1,103 @@ 1.4 +package api 1.5 + 1.6 +import ( 1.7 + "errors" 1.8 + "time" 1.9 + "unicode" 1.10 + 1.11 + "code.google.com/p/go.text/unicode/norm" 1.12 + "secondbit.org/uuid" 1.13 +) 1.14 + 1.15 +var ( 1.16 + CollectionNotFoundError = errors.New("collection not found") 1.17 + DomainNotFoundError = errors.New("domain not found") 1.18 + DomainAlreadyExistsError = errors.New("domain already attached to a collection") 1.19 + ItemNotFoundError = errors.New("item not found") 1.20 + ItemAlreadyExistsError = errors.New("item already exists") 1.21 + UserNotFoundError = errors.New("user not found") 1.22 + UserAlreadyExistsError = errors.New("user already exists") 1.23 + LoginNotFoundError = errors.New("login not found") 1.24 + LoginAlreadyExistsError = errors.New("login already exists") 1.25 +) 1.26 + 1.27 +type Datastore interface { 1.28 + CreateCollection(c Collection) error 1.29 + UpdateCollection(c Collection) error 1.30 + GetCollectionByDomain(domain string) (Collection, error) 1.31 + GetCollectionByID(id uuid.ID) (Collection, error) 1.32 + GetCollectionsByUser(id uuid.ID) ([]Collection, error) 1.33 + AddDomainToCollection(id uuid.ID, domain string) error 1.34 + RemoveDomainFromCollection(id uuid.ID, domain string) error 1.35 + GetDomainsByCollection(id uuid.ID) ([]Domain, error) 1.36 + DeleteCollection(c Collection) error 1.37 + 1.38 + GetItemsByCollectionDomain(domain string, num, offset int) ([]Item, error) 1.39 + GetItemsByCollectionID(id uuid.ID, num, offset int) ([]Item, error) 1.40 + AddItemToCollection(id uuid.ID, item Item) error 1.41 + GetItemByName(collectionID uuid.ID, name string) (Item, error) 1.42 + DeleteItem(item Item) error 1.43 + 1.44 + GetUserByID(id uuid.ID) (User, error) 1.45 + GetUserByLogin(loginType, value, passphrase string) (User, error) 1.46 + AddLoginToUser(login Login, user uuid.ID) error 1.47 + RemoveLoginFromUser(loginType, value string, user uuid.ID) error 1.48 + GetLoginsByUser(user uuid.ID) ([]Login, error) 1.49 + CreateUser(u User) error 1.50 + UpdateUser(u User) error 1.51 + DeleteUser(u User) error 1.52 +} 1.53 + 1.54 +func slugify(in string) string { 1.55 + buf := make([]rune, 0, len(in)) 1.56 + needsDash := false 1.57 + for _, r := range norm.NFKD.String(in) { 1.58 + if unicode.IsSpace(r) || unicode.IsPunct(r) { 1.59 + if needsDash { 1.60 + buf = append(buf, '-') 1.61 + needsDash = false 1.62 + } 1.63 + } else { 1.64 + buf = append(buf, r) 1.65 + needsDash = true 1.66 + } 1.67 + } 1.68 + return string(buf) 1.69 +} 1.70 + 1.71 +type Collection struct { 1.72 + ID uuid.ID 1.73 + Name string 1.74 + Owner uuid.ID 1.75 + Created time.Time 1.76 +} 1.77 + 1.78 +type Domain struct { 1.79 + Domain string 1.80 + CollectionID uuid.ID 1.81 + Created time.Time 1.82 +} 1.83 + 1.84 +type Item struct { 1.85 + Blob string 1.86 + Bucket string 1.87 + CollectionID uuid.ID 1.88 + Name string 1.89 +} 1.90 + 1.91 +type User struct { 1.92 + ID uuid.ID 1.93 + Name string 1.94 + Passphrase string 1.95 + Email string 1.96 + Created time.Time 1.97 + LastSeen time.Time 1.98 +} 1.99 + 1.100 +type Login struct { 1.101 + Type string 1.102 + Value string 1.103 + UserID uuid.ID 1.104 + Created time.Time 1.105 + LastUsed time.Time 1.106 +}