-
Notifications
You must be signed in to change notification settings - Fork 4
/
search.go
35 lines (31 loc) · 1.05 KB
/
search.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
package clib
import (
"fmt"
"net/http"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/app/lib/search"
"projectforge.dev/projectforge/views/vsearch"
)
const searchKey = "search"
func Search(w http.ResponseWriter, r *http.Request) {
controller.Act(searchKey, w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
q := r.URL.Query().Get("q")
params := &search.Params{Q: q, PS: ps.Params}
results, errs := search.Search(ps.Context, params, as, ps)
ps.SetTitleAndData("Search Results", results)
if q != "" {
ps.Title = fmt.Sprintf("[%s] %s", q, ps.Title)
}
if len(results) == 1 && results[0].URL != "" {
return controller.FlashAndRedir(true, "single search result found", results[0].URL, ps)
}
ps.DefaultNavIcon = searchKey
bc := []string{"Search||/search"}
if q != "" {
bc = append(bc, q)
}
return controller.Render(r, as, &vsearch.Results{Params: params, Results: results, Errors: errs}, ps, bc...)
})
}