-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_Project.go
93 lines (76 loc) · 2.58 KB
/
gen_Project.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/cloud-persister/models"
"golang.org/x/net/context"
)
// GetProject retrieves a Project by its ID.
func (c *CloudPersister) GetProject(id int64) (*models.Project, error) {
ctx := context.Background()
k := datastore.NewKey(ctx, "Project", "", id, nil)
Project := &models.Project{}
if err := c.DatastoreClient().Get(ctx, k, Project); err != nil {
return nil, fmt.Errorf("datastoredb: could not get Project: %v", err)
}
Project.ID = id
return Project, nil
}
// AddProject saves a given Project, assigning it a new ID.
func (c *CloudPersister) AddProject(b *models.Project) (*models.Project, error) {
ctx := context.Background()
k := datastore.NewIncompleteKey(ctx, "Project", nil)
k, err := c.DatastoreClient().Put(ctx, k, b)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not put Project: %v", err)
}
b.ID = k.ID()
return b, nil
}
// DeleteProject removes a given Project by its ID.
func (c *CloudPersister) DeleteProject(id int64) error {
ctx := context.Background()
k := datastore.NewKey(ctx, "Project", "", id, nil)
if err := c.DatastoreClient().Delete(ctx, k); err != nil {
return fmt.Errorf("datastoredb: could not delete Project: %v", err)
}
return nil
}
// UpdateProject updates the entry for a given Project.
func (c *CloudPersister) UpdateProject(b *models.Project) error {
ctx := context.Background()
k := datastore.NewKey(ctx, "Project", "", b.ID, nil)
if _, err := c.DatastoreClient().Put(ctx, k, b); err != nil {
return fmt.Errorf("datastoredb: could not update Project: %v", err)
}
return nil
}
// ListProjects returns a list of Projects
func (c *CloudPersister) ListProjects() ([]*models.Project, error) {
ctx := context.Background()
Projects := make([]*models.Project, 0)
q := datastore.NewQuery("Project")
keys, err := c.DatastoreClient().GetAll(ctx, q, &Projects)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not list Projects: %v", err)
}
for i, k := range keys {
Projects[i].ID = k.ID()
}
return Projects, nil
}
// QueryProjectsByProp
func (c *CloudPersister) QueryProjectsByProp(propName, value string) (*models.Project, error) {
ctx := context.Background()
Projects := make([]*models.Project, 0)
q := datastore.NewQuery("Project").Filter(fmt.Sprintf("%s =", propName), value)
keys, err := c.DatastoreClient().GetAll(ctx, q, &Projects)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not list Projects: %v", err)
}
if len(Projects) == 0 {
return nil, nil
}
Projects[0].ID = keys[0].ID()
return Projects[0], nil
}