From 4dade55387515e3ed153a5901672abdfb7f3654f Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sun, 14 Apr 2019 20:29:18 +0200 Subject: sklad: implement search --- sklad/db.go | 48 +++++++++++++++++++++++++++++++++++++++++++++--- sklad/main.go | 15 +++++++++------ sklad/search.tmpl | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 sklad/search.tmpl diff --git a/sklad/db.go b/sklad/db.go index 300c1bd..aae695f 100644 --- a/sklad/db.go +++ b/sklad/db.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "strings" "time" ) @@ -27,10 +28,19 @@ func (c *Container) Id() ContainerId { } func (c *Container) Children() []*Container { - // TODO: Sort this by Id, or maybe even return a map[string]*Container. + // TODO: Sort this by Id, or maybe even return a map[string]*Container, + // text/template would sort that automatically. return indexChildren[c.Id()] } +func (c *Container) Path() (result []ContainerId) { + for c != nil && c.Parent != "" { + c = indexContainer[c.Parent] + result = append(result, c.Id()) + } + return +} + type Database struct { Password string // password for web users Prefix string // prefix for all container IDs @@ -52,9 +62,41 @@ var ( // TODO: Some functions to add, remove and change things in the database. // Indexes must be kept valid, just like any invariants. -// TODO: A function for fulltext search in series (1. Prefix, 2. Description). +func dbSearchSeries(query string) (result []*Series) { + query = strings.ToLower(query) + added := map[string]bool{} + for _, s := range db.Series { + if query == strings.ToLower(s.Prefix) { + result = append(result, s) + added[s.Prefix] = true + } + } + for _, s := range db.Series { + if strings.Contains( + strings.ToLower(s.Description), query) && !added[s.Prefix] { + result = append(result, s) + } + } + return +} -// TODO: A function for fulltext search in containers (1. Id, 2. Description). +func dbSearchContainers(query string) (result []*Container) { + query = strings.ToLower(query) + added := map[ContainerId]bool{} + for id, c := range indexContainer { + if query == strings.ToLower(string(id)) { + result = append(result, c) + added[id] = true + } + } + for id, c := range indexContainer { + if strings.Contains( + strings.ToLower(c.Description), query) && !added[id] { + result = append(result, c) + } + } + return +} func dbCommit() error { // Write a timestamp. diff --git a/sklad/main.go b/sklad/main.go index 1a9442a..9df1fd5 100644 --- a/sklad/main.go +++ b/sklad/main.go @@ -157,12 +157,15 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { } query := r.FormValue("q") - _ = query - - // TODO: Query the database for exact matches and fulltext. - // - Will want to show the full path from the root "" container. - - params := struct{}{} + params := struct { + Query string + Series []*Series + Containers []*Container + }{ + Query: query, + Series: dbSearchSeries(query), + Containers: dbSearchContainers(query), + } executeTemplate("search.tmpl", w, ¶ms) } diff --git a/sklad/search.tmpl b/sklad/search.tmpl new file mode 100644 index 0000000..cf704cf --- /dev/null +++ b/sklad/search.tmpl @@ -0,0 +1,38 @@ +{{ define "Title" }}„{{ .Query }}“ — Vyhledávání{{ end }} +{{ define "Content" }} + +

Vyhledávání: „{{ .Query }}“

+ +

Řady

+ +{{ range .Series }} +
+
+

{{ .Prefix }}

+

{{ .Description }} +

+
+{{ else }} +

Neodpovídají žádné řady. +{{ end }} + +

Obaly

+ +{{ range .Containers }} +
+
+

{{ .Id }} +{{ range .Path }} + « {{ . }} +{{ end }} +

+
+{{ if .Description }} +

{{ .Description }} +{{ end }} +

+{{ else }} +

Neodpovídají žádné obaly. +{{ end }} + +{{ end }} -- cgit v1.2.3-70-g09d2