aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2019-04-14 20:29:18 +0200
committerPřemysl Janouch <p@janouch.name>2019-04-14 20:29:18 +0200
commit4dade55387515e3ed153a5901672abdfb7f3654f (patch)
tree6c1e30193745adfcea111b791b1ae7dd884b3b9f
parent3d98454543f4494fd267e851b48d58a05aea11dc (diff)
downloadsklad-4dade55387515e3ed153a5901672abdfb7f3654f.tar.gz
sklad-4dade55387515e3ed153a5901672abdfb7f3654f.tar.xz
sklad-4dade55387515e3ed153a5901672abdfb7f3654f.zip
sklad: implement search
-rw-r--r--sklad/db.go48
-rw-r--r--sklad/main.go15
-rw-r--r--sklad/search.tmpl38
3 files changed, 92 insertions, 9 deletions
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, &params)
}
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" }}&bdquo;{{ .Query }}&ldquo; &mdash; Vyhledávání{{ end }}
+{{ define "Content" }}
+
+<h2>Vyhledávání: &bdquo;{{ .Query }}&ldquo;<h2>
+
+<h3>Řady</h3>
+
+{{ range .Series }}
+<section>
+<header>
+ <h3><a href="/series?prefix={{ .Prefix }}">{{ .Prefix }}</a></h3>
+ <p>{{ .Description }}
+</header>
+</section>
+{{ else }}
+<p>Neodpovídají žádné řady.
+{{ end }}
+
+<h3>Obaly</h3>
+
+{{ range .Containers }}
+<section>
+<header>
+ <h3><a href="/?id={{ .Id }}">{{ .Id }}</a>
+{{ range .Path }}
+ <small>&laquo; <a href="/?id={{ . }}">{{ . }}</a></small>
+{{ end }}
+ </h3>
+</header>
+{{ if .Description }}
+<p>{{ .Description }}
+{{ end }}
+</section>
+{{ else }}
+<p>Neodpovídají žádné obaly.
+{{ end }}
+
+{{ end }}