-
Notifications
You must be signed in to change notification settings - Fork 4
/
query.go
38 lines (31 loc) · 1.08 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
package goal
import (
"fmt"
"net/http"
)
// QuerySupporter is the interface that return filtered results
// based on request paramters
type QuerySupporter interface {
Query(http.ResponseWriter, *http.Request) (int, interface{}, error)
}
func (api *API) queryHandler(resource interface{}) http.HandlerFunc {
return func(rw http.ResponseWriter, request *http.Request) {
var handler simpleResponse
if resource, ok := resource.(QuerySupporter); ok {
handler = resource.Query
}
renderJSON(rw, request, handler)
}
}
// AddQueryResource allows model to support query based on request
// data, return filtered results back to client
func (api *API) AddQueryResource(resource interface{}, path string) {
api.Mux().Handle(path, api.queryHandler(resource))
}
// AddDefaultQueryPath allows model to support query based on request
// data, return filtered results back to client. The path is created
// base on struct name
func (api *API) AddDefaultQueryPath(resource interface{}) {
queryPath := fmt.Sprintf("/query/%s/{query}", TableName(resource))
api.AddQueryResource(resource, queryPath)
}