sklad: implement search
This commit is contained in:
parent
3d98454543
commit
4dade55387
48
sklad/db.go
48
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.
|
||||
|
@ -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)
|
||||
}
|
||||
|
38
sklad/search.tmpl
Normal file
38
sklad/search.tmpl
Normal file
@ -0,0 +1,38 @@
|
||||
{{ define "Title" }}„{{ .Query }}“ — Vyhledávání{{ end }}
|
||||
{{ define "Content" }}
|
||||
|
||||
<h2>Vyhledávání: „{{ .Query }}“<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>« <a href="/?id={{ . }}">{{ . }}</a></small>
|
||||
{{ end }}
|
||||
</h3>
|
||||
</header>
|
||||
{{ if .Description }}
|
||||
<p>{{ .Description }}
|
||||
{{ end }}
|
||||
</section>
|
||||
{{ else }}
|
||||
<p>Neodpovídají žádné obaly.
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
Loading…
Reference in New Issue
Block a user