forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_api_handler.go
51 lines (44 loc) · 1.13 KB
/
search_api_handler.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
package uadmin
import (
"encoding/json"
"fmt"
"html"
"html/template"
"net/http"
)
func searchApiHandler(w http.ResponseWriter, r *http.Request, session *Session) {
modelName := r.FormValue("m")
model, ok := NewModel(modelName, false)
if !ok {
pageErrorHandler(w, r, session)
return
}
s, _ := getSchema(modelName)
query := ""
args := []interface{}{}
if s.ListModifier != nil {
query, args = s.ListModifier(&s, &session.User)
}
ld := getListData(model.Interface(), PageLength, r, session, query, &s, args...)
type Context struct {
List [][]string `json:"list"`
PageCount int `json:"page_count"`
}
context := Context{
List: [][]string{},
}
for i := range ld.Rows {
context.List = append(context.List, []string{})
for j := range ld.Rows[i] {
switch ld.Rows[i][j].(type) {
case template.HTML:
context.List[i] = append(context.List[i], fmt.Sprint(ld.Rows[i][j]))
default:
context.List[i] = append(context.List[i], html.EscapeString(fmt.Sprint(ld.Rows[i][j])))
}
}
}
context.PageCount = paginationHandler(ld.Count, PageLength)
bytes, _ := json.Marshal(context)
w.Write(bytes)
}