-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_operations.go
48 lines (42 loc) · 1.2 KB
/
db_operations.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
package main
import (
"context"
"fmt"
"time"
"cloud.google.com/go/datastore"
"google.golang.org/api/iterator"
)
func getLatestConvention(ctx context.Context) (convention, error) {
var theConvention convention
client, err := datastore.NewClient(ctx, config.ProjectID)
if err != nil {
return convention{}, fmt.Errorf("could not create datastore client: %v", err)
}
query := datastore.NewQuery("Convention").
Order("-Creation_Date")
it := client.Run(ctx, query)
for {
_, err := it.Next(&theConvention)
if err == iterator.Done {
break
}
if err != nil {
return convention{}, fmt.Errorf("convention not in DB: %v", err)
}
return theConvention, nil
}
return convention{}, fmt.Errorf("no conventions not in DB: %v", err)
}
// addUser : adds user to User table
func addUser(ctx context.Context, u *user) (*datastore.Key, error) {
u.Creation_Date = time.Now()
client, err := datastore.NewClient(ctx, config.ProjectID)
if err != nil {
return nil, fmt.Errorf("could not create datastore client: %v", err)
}
key := datastore.IncompleteKey("User", nil)
if _, err := client.Put(ctx, key, u); err != nil {
return nil, fmt.Errorf("could not add user to user table: %v", err)
}
return key, nil
}