forked from shijuvar/go-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
47 lines (39 loc) · 1.13 KB
/
task.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
package cloudendpoint
import (
"time"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
)
// Task is a datastore entity
type Task struct {
Key *datastore.Key `json:"id" datastore:"-"`
Name string `json:"name" endpoints:"req"`
Description string `json:"description" datastore:",noindex" endpoints:"req"`
CreatedOn time.Time `json:"createdon,omitempty"`
}
// Tasks is a response type of TaskService.List method
type Tasks struct {
Tasks []Task `json:"tasks"`
}
// Struct is used to add API method
type TaskService struct {
}
// List returns a list of all the existing tasks.
func (ts *TaskService) List(c context.Context) (*Tasks, error) {
tasks := []Task{}
keys, err := datastore.NewQuery("tasks").Order("-CreatedOn").GetAll(c, &tasks)
if err != nil {
return nil, err
}
for i, k := range keys {
tasks[i].Key = k
}
return &Tasks{tasks}, nil
}
// Add inserts a new Task into Datastore
func (ts *TaskService) Add(c context.Context, t *Task) error {
t.CreatedOn = time.Now()
key := datastore.NewIncompleteKey(c, "tasks", nil)
_, err := datastore.Put(c, key, t)
return err
}