-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkout.go
66 lines (53 loc) · 1.46 KB
/
workout.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
package main
import (
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"time"
)
type Workout struct {
Key *datastore.Key `datastore:"-"`
UserID string `json:"-"`
Type string `json:"type"`
Duration int64 `json:"duration"`
Distance int `json:"distance"`
DistanceUOM string `json:"distance_uom"`
Details string `json:"details"`
Date time.Time `json:"date"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func FindRecentWorkoutsForUser(c context.Context, u *User) ([]*Workout, error) {
workouts := []*Workout{}
keys, err := datastore.NewQuery("Workout").Ancestor(u.Key).Order("-Date").Limit(20).GetAll(c, &workouts)
if err != nil {
return nil, err
}
for idx, _ := range workouts {
workouts[idx].Key = keys[idx]
}
return workouts, nil
}
func CreateWorkoutForUser(c context.Context, w *Workout, u *User) error {
w.CreatedAt = time.Now()
w.UpdatedAt = time.Now()
p := u.Key
k := datastore.NewIncompleteKey(c, "Workout", p)
k2, err := datastore.Put(c, k, w)
if err != nil {
return err
}
w.Key = k2
return nil
}
func FindWorkoutForUser(c context.Context, u *User, encK string) (*Workout, error) {
k, err := datastore.DecodeKey(encK)
if err != nil {
return nil, err
}
wo := new(Workout)
if err := datastore.Get(c, k, wo); err != nil {
return nil, err
}
wo.Key = k
return wo, nil
}