From 608ba10eecb1f5c02eb41b288d2af5ce2de3f4f5 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sun, 14 Apr 2019 23:19:25 +0200 Subject: sklad: show the number of members of each series --- cmd/sklad/db.go | 6 ++++++ cmd/sklad/main.go | 6 +++--- cmd/sklad/series.tmpl | 19 ++++++++++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cmd/sklad/db.go b/cmd/sklad/db.go index def18a5..0aba510 100644 --- a/cmd/sklad/db.go +++ b/cmd/sklad/db.go @@ -16,6 +16,10 @@ type Series struct { Description string // what kind of containers this is for } +func (s *Series) Containers() []*Container { + return indexMembers[s.Prefix] +} + type ContainerId string type Container struct { @@ -60,6 +64,7 @@ var ( dbLog *os.File indexSeries = map[string]*Series{} + indexMembers = map[string][]*Container{} indexContainer = map[ContainerId]*Container{} indexChildren = map[ContainerId][]*Container{} @@ -184,6 +189,7 @@ func loadDatabase() error { } } indexChildren[pv.Parent] = append(indexChildren[pv.Parent], pv) + indexMembers[pv.Series] = append(indexMembers[pv.Series], pv) } // Validate that no container is a parent of itself on any level. diff --git a/cmd/sklad/main.go b/cmd/sklad/main.go index 32dd68b..fd675b1 100644 --- a/cmd/sklad/main.go +++ b/cmd/sklad/main.go @@ -129,9 +129,9 @@ func handleSeries(w http.ResponseWriter, r *http.Request) { return } - allSeries := map[string]string{} + allSeries := map[string]*Series{} for _, s := range indexSeries { - allSeries[s.Prefix] = s.Description + allSeries[s.Prefix] = s } prefix := r.FormValue("prefix") @@ -145,7 +145,7 @@ func handleSeries(w http.ResponseWriter, r *http.Request) { params := struct { Prefix string Description string - AllSeries map[string]string + AllSeries map[string]*Series }{ Prefix: prefix, Description: description, diff --git a/cmd/sklad/series.tmpl b/cmd/sklad/series.tmpl index 4956e3a..01e2539 100644 --- a/cmd/sklad/series.tmpl +++ b/cmd/sklad/series.tmpl @@ -21,15 +21,24 @@ -{{ range $prefix, $desc := .AllSeries }} +{{ range .AllSeries }}
-

{{ $prefix }}

-
- {{ .Prefix }} +{{ with $count := len .Containers }} +{{ if eq $count 1 }} +

{{ $count }} obal +{{ else if and (ge $count 2) (le $count 4) }} +

{{ $count }} obaly +{{ else if gt $count 0 }} +

{{ $count }} obalů +{{ end }} +{{ end }} + +

-
+
-- cgit v1.2.3-70-g09d2