forked from Yunski/RideMerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_userDatastore.go
101 lines (85 loc) · 2.29 KB
/
db_userDatastore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ridemerge
import (
"fmt"
"log"
"google.golang.org/api/iterator"
"cloud.google.com/go/datastore"
"golang.org/x/net/context"
)
type UserDatastore struct {
client *datastore.Client
}
var _ UserDatabase = &UserDatastore{}
func NewUserDatastore(client *datastore.Client) (UserDatabase, error) {
ctx := context.Background()
t, err := client.NewTransaction(ctx)
if err != nil {
return nil, fmt.Errorf("userdatastore: could not connect: %v", err)
}
if err := t.Rollback(); err != nil {
return nil, fmt.Errorf("userdatastore: could not connect: %v", err)
}
return &UserDatastore{
client: client,
}, nil
}
// Close closes the database.
func (db *UserDatastore) Close() {
// No op.
}
func (db *UserDatastore) datastoreKey(email string) *datastore.Key {
return datastore.NameKey("User", email, nil)
}
func (db *UserDatastore) GetUser(email string) (User, error) {
ctx := context.Background()
k := datastore.NameKey("User", email, nil)
var user User
if err := db.client.Get(ctx, k, &user); err != nil {
return user, fmt.Errorf("userdatastore: could not get User: %v", err)
}
return user, nil
}
func (db *UserDatastore) PutUser(u User) error {
ctx := context.Background()
k := datastore.NameKey("User", u.Email, nil)
_, err := db.client.Put(ctx, k, &u)
if err != nil {
return fmt.Errorf("userdatastore: could not put User: %v", err)
}
return nil
}
func (db *UserDatastore) DeleteUser(email string) error {
ctx := context.Background()
k := datastore.NameKey("User", email, nil)
if err := db.client.Delete(ctx, k); err != nil {
return fmt.Errorf("userdatastore: could not delete User: %v", err)
}
return nil
}
func (db *UserDatastore) UpdateUser(u User) error {
ctx := context.Background()
k := datastore.NameKey("User", u.Email, nil)
if _, err := db.client.Put(ctx, k, &u); err != nil {
return fmt.Errorf("userdatastore: could not update User: %v", err)
}
return nil
}
func (db *UserDatastore) ListUsers() ([]User, error) {
ctx := context.Background()
users := make([]User, 0)
q := datastore.NewQuery("User").
Order("LastName")
it := db.client.Run(ctx, q)
for {
var user User
_, err := it.Next(&user)
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Error fetching next user: %v", err)
}
users = append(users, user)
}
return users, nil
}