-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_ItemType.go
93 lines (76 loc) · 2.62 KB
/
gen_ItemType.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
package cloudpersister
import (
"fmt"
"cloud.google.com/go/datastore"
"github.com/codegp/game-object-types/types"
"golang.org/x/net/context"
)
// GetItemType retrieves a ItemType by its ID.
func (c *CloudPersister) GetItemType(id int64) (*types.ItemType, error) {
ctx := context.Background()
k := datastore.NewKey(ctx, "ItemType", "", id, nil)
ItemType := &types.ItemType{}
if err := c.DatastoreClient().Get(ctx, k, ItemType); err != nil {
return nil, fmt.Errorf("datastoredb: could not get ItemType: %v", err)
}
ItemType.ID = id
return ItemType, nil
}
// AddItemType saves a given ItemType, assigning it a new ID.
func (c *CloudPersister) AddItemType(b *types.ItemType) (*types.ItemType, error) {
ctx := context.Background()
k := datastore.NewIncompleteKey(ctx, "ItemType", nil)
k, err := c.DatastoreClient().Put(ctx, k, b)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not put ItemType: %v", err)
}
b.ID = k.ID()
return b, nil
}
// DeleteItemType removes a given ItemType by its ID.
func (c *CloudPersister) DeleteItemType(id int64) error {
ctx := context.Background()
k := datastore.NewKey(ctx, "ItemType", "", id, nil)
if err := c.DatastoreClient().Delete(ctx, k); err != nil {
return fmt.Errorf("datastoredb: could not delete ItemType: %v", err)
}
return nil
}
// UpdateItemType updates the entry for a given ItemType.
func (c *CloudPersister) UpdateItemType(b *types.ItemType) error {
ctx := context.Background()
k := datastore.NewKey(ctx, "ItemType", "", b.ID, nil)
if _, err := c.DatastoreClient().Put(ctx, k, b); err != nil {
return fmt.Errorf("datastoredb: could not update ItemType: %v", err)
}
return nil
}
// ListItemTypes returns a list of ItemTypes
func (c *CloudPersister) ListItemTypes() ([]*types.ItemType, error) {
ctx := context.Background()
ItemTypes := make([]*types.ItemType, 0)
q := datastore.NewQuery("ItemType")
keys, err := c.DatastoreClient().GetAll(ctx, q, &ItemTypes)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not list ItemTypes: %v", err)
}
for i, k := range keys {
ItemTypes[i].ID = k.ID()
}
return ItemTypes, nil
}
// QueryItemTypesByProp
func (c *CloudPersister) QueryItemTypesByProp(propName, value string) (*types.ItemType, error) {
ctx := context.Background()
ItemTypes := make([]*types.ItemType, 0)
q := datastore.NewQuery("ItemType").Filter(fmt.Sprintf("%s =", propName), value)
keys, err := c.DatastoreClient().GetAll(ctx, q, &ItemTypes)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not list ItemTypes: %v", err)
}
if len(ItemTypes) == 0 {
return nil, nil
}
ItemTypes[0].ID = keys[0].ID()
return ItemTypes[0], nil
}