Browse Source

sklad: implement search

master
Přemysl Janouch 9 months ago
parent
commit
4dade55387
Signed by: Přemysl Janouch <p@janouch.name> GPG Key ID: A0420B94F92B9493
3 changed files with 92 additions and 9 deletions
  1. +45
    -3
      sklad/db.go
  2. +9
    -6
      sklad/main.go
  3. +38
    -0
      sklad/search.tmpl

+ 45
- 3
sklad/db.go View File

@@ -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.

+ 9
- 6
sklad/main.go View File

@@ -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)
}

+ 38
- 0
sklad/search.tmpl View File

@@ -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 }}

Loading…
Cancel
Save