From 7865f77060f083ffa9c549806b8ff4c9659266df Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Sun, 20 Mar 2022 21:24:54 +0300 Subject: [PATCH] Categories: Add category list * /category lists categories * /category/ shows the category --- hyphae/categories/categories.go | 10 ++++++++++ views/categories.go | 21 +++++++++++++++++++++ views/categories.html | 17 +++++++++++++++++ web/categories.go | 5 +++++ 4 files changed, 53 insertions(+) diff --git a/hyphae/categories/categories.go b/hyphae/categories/categories.go index 6b69ed1..a9533c6 100644 --- a/hyphae/categories/categories.go +++ b/hyphae/categories/categories.go @@ -16,6 +16,16 @@ package categories import "sync" +// List returns names of all categories. +func List() (categoryList []string) { + mutex.RLock() + for cat, _ := range categoryToHyphae { + categoryList = append(categoryList, cat) + } + mutex.RUnlock() + return categoryList +} + // WithHypha returns what categories have the given hypha. The hypha name must be canonical. func WithHypha(hyphaName string) (categoryList []string) { mutex.RLock() diff --git a/views/categories.go b/views/categories.go index 199e85e..73b1c9a 100644 --- a/views/categories.go +++ b/views/categories.go @@ -64,3 +64,24 @@ func CategoryPageHTML(meta Meta, catName string) { log.Println(err) } } + +func CategoryListHTML(meta Meta) { + var buf strings.Builder + err := categoryT.ExecuteTemplate(&buf, "category list", struct { + Categories []string + }{ + categories.List(), + }) + if err != nil { + log.Println(err) + } + _, err = io.WriteString(meta.W, BaseHTML( + "Category list", + buf.String(), + meta.Lc, + meta.U, + )) + if err != nil { + log.Println(err) + } +} diff --git a/views/categories.html b/views/categories.html index 67364c7..1614d76 100644 --- a/views/categories.html +++ b/views/categories.html @@ -51,4 +51,21 @@ +{{end}} + +{{define "category list"}} +
+

Category list

+ {{if len .Categories}} + + {{else}} +

This wiki has no categories.

+ {{end}} +
{{end}} \ No newline at end of file diff --git a/web/categories.go b/web/categories.go index cc97afc..aa792f2 100644 --- a/web/categories.go +++ b/web/categories.go @@ -12,6 +12,11 @@ func initCategories(r *mux.Router) { r.PathPrefix("/add-to-category").HandlerFunc(handlerAddToCategory).Methods("POST") r.PathPrefix("/remove-from-category").HandlerFunc(handlerRemoveFromCategory).Methods("POST") r.PathPrefix("/category/").HandlerFunc(handlerCategory).Methods("GET") + r.PathPrefix("/category").HandlerFunc(handlerListCategory).Methods("GET") +} + +func handlerListCategory(w http.ResponseWriter, rq *http.Request) { + views.CategoryListHTML(views.MetaFrom(w, rq)) } func handlerCategory(w http.ResponseWriter, rq *http.Request) {