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 package api
3 import (
4 "errors"
5 "time"
6 "unicode"
8 "code.google.com/p/go.text/unicode/norm"
9 "secondbit.org/uuid"
10 )
12 var (
13 CollectionNotFoundError = errors.New("collection not found")
14 DomainNotFoundError = errors.New("domain not found")
15 DomainAlreadyExistsError = errors.New("domain already attached to a collection")
16 ItemNotFoundError = errors.New("item not found")
17 ItemAlreadyExistsError = errors.New("item already exists")
18 UserNotFoundError = errors.New("user not found")
19 UserAlreadyExistsError = errors.New("user already exists")
20 LoginNotFoundError = errors.New("login not found")
21 LoginAlreadyExistsError = errors.New("login already exists")
22 )
24 type Datastore interface {
25 CreateCollection(c Collection) error
26 UpdateCollection(c Collection) error
27 GetCollectionByDomain(domain string) (Collection, error)
28 GetCollectionByID(id uuid.ID) (Collection, error)
29 GetCollectionsByUser(id uuid.ID) ([]Collection, error)
30 AddDomainToCollection(id uuid.ID, domain string) error
31 RemoveDomainFromCollection(id uuid.ID, domain string) error
32 GetDomainsByCollection(id uuid.ID) ([]Domain, error)
33 DeleteCollection(c Collection) error
35 GetItemsByCollectionDomain(domain string, num, offset int) ([]Item, error)
36 GetItemsByCollectionID(id uuid.ID, num, offset int) ([]Item, error)
37 AddItemToCollection(id uuid.ID, item Item) error
38 GetItemByName(collectionID uuid.ID, name string) (Item, error)
39 DeleteItem(item Item) error
41 GetUserByID(id uuid.ID) (User, error)
42 GetUserByLogin(loginType, value, passphrase string) (User, error)
43 AddLoginToUser(login Login, user uuid.ID) error
44 RemoveLoginFromUser(loginType, value string, user uuid.ID) error
45 GetLoginsByUser(user uuid.ID) ([]Login, error)
46 CreateUser(u User) error
47 UpdateUser(u User) error
48 DeleteUser(u User) error
49 }
51 func slugify(in string) string {
52 buf := make([]rune, 0, len(in))
53 needsDash := false
54 for _, r := range norm.NFKD.String(in) {
55 if unicode.IsSpace(r) || unicode.IsPunct(r) {
56 if needsDash {
57 buf = append(buf, '-')
58 needsDash = false
59 }
60 } else {
61 buf = append(buf, r)
62 needsDash = true
63 }
64 }
65 return string(buf)
66 }
68 type Collection struct {
69 ID uuid.ID
70 Name string
71 Owner uuid.ID
72 Created time.Time
73 }
75 type Domain struct {
76 Domain string
77 CollectionID uuid.ID
78 Created time.Time
79 }
81 type Item struct {
82 Blob string
83 Bucket string
84 CollectionID uuid.ID
85 Name string
86 }
88 type User struct {
89 ID uuid.ID
90 Name string
91 Passphrase string
92 Email string
93 Created time.Time
94 LastSeen time.Time
95 }
97 type Login struct {
98 Type string
99 Value string
100 UserID uuid.ID
101 Created time.Time
102 LastUsed time.Time
103 }