-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_method.go
76 lines (70 loc) · 2.79 KB
/
handler_method.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
package search
import (
"net/http"
"reflect"
)
const internalServerError = "Internal Server Error"
func (c *SearchHandler) Search(w http.ResponseWriter, r *http.Request) {
filter, x, er0 := BuildFilter(r, c.filterType, c.ParamIndex, c.userId, c.FilterIndex)
if er0 != nil {
http.Error(w, "cannot decode filter: "+er0.Error(), http.StatusBadRequest)
return
}
limit, offset, fs, _, _, er1 := Extract(filter)
if er1 != nil {
RespondError(w, r, http.StatusInternalServerError, internalServerError, c.LogError, c.ResourceName, c.Activity, er1, c.WriteLog)
return
}
modelsType := reflect.Zero(reflect.SliceOf(c.modelType)).Type()
models := reflect.New(modelsType).Interface()
count, er2 := c.Find(r.Context(), filter, models, limit, offset)
if er2 != nil {
RespondError(w, r, http.StatusInternalServerError, internalServerError, c.LogError, c.ResourceName, c.Activity, er2, c.WriteLog)
return
}
result := BuildResultMap(models, count, c.List, c.Total)
if x == -1 {
succeed(w, r, http.StatusOK, result, c.WriteLog, c.ResourceName, c.Activity)
} else if c.CSV && x == 1 {
result1, ok := ResultToCsv(fs, models, count, c.embedField, c.JsonMap, c.SecondaryJsonMap)
if ok {
succeed(w, r, http.StatusOK, result1, c.WriteLog, c.ResourceName, c.Activity)
} else {
succeed(w, r, http.StatusOK, result, c.WriteLog, c.ResourceName, c.Activity)
}
} else {
succeed(w, r, http.StatusOK, result, c.WriteLog, c.ResourceName, c.Activity)
}
}
func (c *NextSearchHandler) Search(w http.ResponseWriter, r *http.Request) {
filter, x, er0 := BuildFilter(r, c.filterType, c.ParamIndex, c.userId, c.FilterIndex)
if er0 != nil {
http.Error(w, "cannot decode filter: "+er0.Error(), http.StatusBadRequest)
return
}
limit, _, fs, _, nextPageToken, er1 := Extract(filter)
if er1 != nil {
RespondError(w, r, http.StatusInternalServerError, internalServerError, c.LogError, c.ResourceName, c.Activity, er1, c.WriteLog)
return
}
modelsType := reflect.Zero(reflect.SliceOf(c.modelType)).Type()
models := reflect.New(modelsType).Interface()
nx, er2 := c.Find(r.Context(), filter, models, limit, nextPageToken)
if er2 != nil {
RespondError(w, r, http.StatusInternalServerError, internalServerError, c.LogError, c.ResourceName, c.Activity, er2, c.WriteLog)
return
}
result := BuildNextResultMap(models, nx, c.List, c.Next)
if x == -1 {
succeed(w, r, http.StatusOK, result, c.WriteLog, c.ResourceName, c.Activity)
} else if c.CSV && x == 1 {
result1, ok := ResultToNextCsv(fs, models, nx, c.embedField, c.JsonMap, c.SecondaryJsonMap)
if ok {
succeed(w, r, http.StatusOK, result1, c.WriteLog, c.ResourceName, c.Activity)
} else {
succeed(w, r, http.StatusOK, result, c.WriteLog, c.ResourceName, c.Activity)
}
} else {
succeed(w, r, http.StatusOK, result, c.WriteLog, c.ResourceName, c.Activity)
}
}