1
|
|
|
package google |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
|
6
|
|
|
"cloud.google.com/go/datastore" |
7
|
|
|
"google.golang.org/api/iterator" |
8
|
|
|
|
9
|
|
|
"github.com/rfinochi/golang-workshop-todo/pkg/models" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
const entityName string = "todoitem" |
13
|
|
|
|
14
|
|
|
// ItemRepository godoc |
15
|
|
|
type ItemRepository struct { |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// GetItems godoc |
19
|
|
|
func (ItemRepository) GetItems() (items []models.Item, err error) { |
20
|
|
|
ctx, client, err := connnectToDatastore() |
21
|
|
|
if err != nil { |
22
|
|
|
return |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
query := datastore.NewQuery("todoitem").Order("id") |
26
|
|
|
it := client.Run(ctx, query) |
27
|
|
|
for { |
28
|
|
|
var item models.Item |
29
|
|
|
if _, err = it.Next(&item); err == iterator.Done { |
30
|
|
|
err = nil |
31
|
|
|
break |
32
|
|
|
} else if err != nil { |
33
|
|
|
return |
34
|
|
|
} |
35
|
|
|
items = append(items, item) |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// GetItem godoc |
42
|
|
|
func (ItemRepository) GetItem(id int) (item models.Item, err error) { |
43
|
|
|
ctx, client, err := connnectToDatastore() |
44
|
|
|
if err != nil { |
45
|
|
|
return |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
key := datastore.IDKey(entityName, int64(id), nil) |
49
|
|
|
err = client.Get(ctx, key, &item) |
50
|
|
|
if err == datastore.ErrNoSuchEntity { |
51
|
|
|
err = nil |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// CreateItem godoc |
58
|
|
|
func (ItemRepository) CreateItem(newItem models.Item) (err error) { |
59
|
|
|
ctx, client, err := connnectToDatastore() |
60
|
|
|
if err != nil { |
61
|
|
|
return |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
key := datastore.IDKey(entityName, int64(newItem.ID), nil) |
65
|
|
|
_, err = client.Put(ctx, key, &newItem) |
66
|
|
|
|
67
|
|
|
return |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// UpdateItem godoc |
71
|
|
|
func (ItemRepository) UpdateItem(item models.Item) (err error) { |
72
|
|
|
ctx, client, err := connnectToDatastore() |
73
|
|
|
if err != nil { |
74
|
|
|
return |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
key := datastore.IDKey(entityName, int64(item.ID), nil) |
78
|
|
|
_, err = client.Put(ctx, key, &item) |
79
|
|
|
|
80
|
|
|
return |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// DeleteItem godoc |
84
|
|
|
func (ItemRepository) DeleteItem(id int) (err error) { |
85
|
|
|
ctx, client, err := connnectToDatastore() |
86
|
|
|
if err != nil { |
87
|
|
|
return |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
key := datastore.IDKey(entityName, int64(id), nil) |
91
|
|
|
err = client.Delete(ctx, key) |
92
|
|
|
|
93
|
|
|
return |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
func connnectToDatastore() (ctx context.Context, client *datastore.Client, err error) { |
97
|
|
|
ctx = context.Background() |
98
|
|
|
|
99
|
|
|
client, err = datastore.NewClient(ctx, "golang-workshop-todo") |
100
|
|
|
|
101
|
|
|
return |
102
|
|
|
} |
103
|
|
|
|