-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_MoveType.go
93 lines (76 loc) · 2.62 KB
/
gen_MoveType.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"
)
// GetMoveType retrieves a MoveType by its ID.
func (c *CloudPersister) GetMoveType(id int64) (*types.MoveType, error) {
ctx := context.Background()
k := datastore.NewKey(ctx, "MoveType", "", id, nil)
MoveType := &types.MoveType{}
if err := c.DatastoreClient().Get(ctx, k, MoveType); err != nil {
return nil, fmt.Errorf("datastoredb: could not get MoveType: %v", err)
}
MoveType.ID = id
return MoveType, nil
}
// AddMoveType saves a given MoveType, assigning it a new ID.
func (c *CloudPersister) AddMoveType(b *types.MoveType) (*types.MoveType, error) {
ctx := context.Background()
k := datastore.NewIncompleteKey(ctx, "MoveType", nil)
k, err := c.DatastoreClient().Put(ctx, k, b)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not put MoveType: %v", err)
}
b.ID = k.ID()
return b, nil
}
// DeleteMoveType removes a given MoveType by its ID.
func (c *CloudPersister) DeleteMoveType(id int64) error {
ctx := context.Background()
k := datastore.NewKey(ctx, "MoveType", "", id, nil)
if err := c.DatastoreClient().Delete(ctx, k); err != nil {
return fmt.Errorf("datastoredb: could not delete MoveType: %v", err)
}
return nil
}
// UpdateMoveType updates the entry for a given MoveType.
func (c *CloudPersister) UpdateMoveType(b *types.MoveType) error {
ctx := context.Background()
k := datastore.NewKey(ctx, "MoveType", "", b.ID, nil)
if _, err := c.DatastoreClient().Put(ctx, k, b); err != nil {
return fmt.Errorf("datastoredb: could not update MoveType: %v", err)
}
return nil
}
// ListMoveTypes returns a list of MoveTypes
func (c *CloudPersister) ListMoveTypes() ([]*types.MoveType, error) {
ctx := context.Background()
MoveTypes := make([]*types.MoveType, 0)
q := datastore.NewQuery("MoveType")
keys, err := c.DatastoreClient().GetAll(ctx, q, &MoveTypes)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not list MoveTypes: %v", err)
}
for i, k := range keys {
MoveTypes[i].ID = k.ID()
}
return MoveTypes, nil
}
// QueryMoveTypesByProp
func (c *CloudPersister) QueryMoveTypesByProp(propName, value string) (*types.MoveType, error) {
ctx := context.Background()
MoveTypes := make([]*types.MoveType, 0)
q := datastore.NewQuery("MoveType").Filter(fmt.Sprintf("%s =", propName), value)
keys, err := c.DatastoreClient().GetAll(ctx, q, &MoveTypes)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not list MoveTypes: %v", err)
}
if len(MoveTypes) == 0 {
return nil, nil
}
MoveTypes[0].ID = keys[0].ID()
return MoveTypes[0], nil
}