-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery.go
125 lines (102 loc) · 3.28 KB
/
query.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package ud859
import (
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
// ConferenceQueryForm wraps a list of filters.
type ConferenceQueryForm struct {
Filters []*Filter `json:"filters"`
}
// Filter describes a query restriction.
type Filter struct {
Field string `endpoints:"req"`
Op string `endpoints:"req"`
Value interface{} `endpoints:"req"`
}
// Filter adds a restriction to the ConferenceQueryForm.
func (q *ConferenceQueryForm) Filter(field string, op string, value interface{}) *ConferenceQueryForm {
q.Filters = append(q.Filters, &Filter{field, op, value})
return q
}
// QueryConferences searches for Conferences with the specified ConferenceQueryForm.
func (ConferenceAPI) QueryConferences(c context.Context, form *ConferenceQueryForm) (*Conferences, error) {
// perform search on index
if len(form.Filters) > 0 {
return searchConferences(c, form)
}
// get the conferences from cache
conferences := getCacheNoFilters(c)
if conferences != nil {
return conferences, nil
}
items := make([]*Conference, 0)
keys, err := datastore.NewQuery("Conference").Order(StartDate).GetAll(c, &items)
if err != nil {
return nil, errInternalServer(err, "unable to query conference")
}
for i := 0; i < len(items); i++ {
items[i].WebsafeKey = keys[i].Encode()
}
conferences = &Conferences{Items: items}
// cache the conferences
err = setCacheNoFilters.Call(c, conferences)
if err != nil {
log.Errorf(c, "unable to set cache: %v", err)
}
return conferences, nil
}
// ConferencesCreated returns the Conferences created by the current user.
func (ConferenceAPI) ConferencesCreated(c context.Context) (*Conferences, error) {
pid, err := profileID(c)
if err != nil {
return nil, err
}
// get the conferences whose parent is the profile key
items := make([]*Conference, 0)
query := datastore.NewQuery("Conference").Ancestor(pid.key).Order(StartDate)
keys, err := query.GetAll(c, &items)
if err != nil {
return nil, errInternalServer(err, "unable to query conference")
}
for i := 0; i < len(items); i++ {
items[i].WebsafeKey = keys[i].Encode()
}
return &Conferences{Items: items}, nil
}
// ConferencesToAttend returns the Conferences to attend by the current user.
func (ConferenceAPI) ConferencesToAttend(c context.Context) (*Conferences, error) {
pid, err := profileID(c)
if err != nil {
return nil, err
}
// get the profile
profile, err := getProfile(c, pid)
if err != nil {
return nil, err
}
if len(profile.Conferences) == 0 {
items := make([]*Conference, 0)
return &Conferences{Items: items}, nil
}
// get the conference keys
keys := make([]*datastore.Key, len(profile.Conferences))
for i, safeKey := range profile.Conferences {
keys[i], err = datastore.DecodeKey(safeKey)
if err != nil {
return nil, errInternalServer(err, "unable to query conference")
}
}
// get the conferences
items := make([]*Conference, len(profile.Conferences))
err = datastore.GetMulti(c, keys, items)
if err != nil {
return nil, errInternalServer(err, "unable to query conference")
}
// datastore.GetMulti returns the entities in the same order as the keys
for i := 0; i < len(items); i++ {
items[i].WebsafeKey = profile.Conferences[i]
}
// TODO: sort by StartDate
return &Conferences{Items: items}, nil
}