-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.go
More file actions
65 lines (57 loc) · 1.21 KB
/
search.go
File metadata and controls
65 lines (57 loc) · 1.21 KB
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
package main
import (
"net/http"
"github.com/fluidmediaproductions/fluidmedia_crm/model"
"database/sql"
)
type SearchContext struct {
Results []*SearchResult
Term string
}
type SearchResult struct {
Obj interface{}
Type struct {
Name string
URL string
}
}
func handleSearch(m *model.Model, page *Page, user *model.User, w http.ResponseWriter, r *http.Request) {
r.ParseForm()
term := r.Form.Get("term")
results := make([]*SearchResult, 0)
contacts, err := m.SearchContacts(term)
if err != nil && err != sql.ErrNoRows {
display500(w, err)
return
}
for _, contact := range contacts {
results = append(results, &SearchResult{
Obj: contact,
Type: struct {
Name string
URL string
}{
Name: "Contact",
URL: "contacts",
},
})
}
organisations, err := m.SearchOrganisations(term)
if err != nil && err != sql.ErrNoRows {
display500(w, err)
return
}
for _, organisation := range organisations {
results = append(results, &SearchResult{
Obj: organisation,
Type: struct {
Name string
URL string
}{
Name: "Organisation",
URL: "organisations",
},
})
}
displayWithContext(w, "search", page, user, &SearchContext{Term: term, Results: results})
}