From 9a60cc2386592fa9a7df544eb96a2a21ab94e673 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Sun, 20 Mar 2022 12:28:51 +0300 Subject: [PATCH] Categories: Add addition to/removal from categories * The result of operations is not saved on disk. * TODO: handle bad names --- hyphae/categories/categories.go | 7 +++++++ hyphae/categories/files.go | 18 ++++++++++++++++++ views/categories.html | 5 ++++- web/categories.go | 19 +++++++++++++++++-- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/hyphae/categories/categories.go b/hyphae/categories/categories.go index b03561d..d46cc93 100644 --- a/hyphae/categories/categories.go +++ b/hyphae/categories/categories.go @@ -60,5 +60,12 @@ func AddHyphaToCategory(hyphaName, catName string) { // RemoveHyphaFromCategory removes the hypha from the category and updates the records on the disk. If the hypha is not in the category, nothing happens. func RemoveHyphaFromCategory(hyphaName, catName string) { mutex.Lock() + if node, ok := hyphaToCategories[hyphaName]; ok { + node.removeCategory(catName) + } + + if node, ok := categoryToHyphae[catName]; ok { + node.removeHypha(hyphaName) + } mutex.Unlock() } diff --git a/hyphae/categories/files.go b/hyphae/categories/files.go index 9064cb4..0cf17a7 100644 --- a/hyphae/categories/files.go +++ b/hyphae/categories/files.go @@ -58,6 +58,15 @@ func (cn *categoryNode) storeHypha(hypname string) { cn.hyphaList = append(cn.hyphaList, hypname) } +func (cn *categoryNode) removeHypha(hypname string) { + for i, hyphaName := range cn.hyphaList { + if hyphaName == hypname { + cn.hyphaList[i] = cn.hyphaList[len(cn.hyphaList)-1] + cn.hyphaList = cn.hyphaList[:len(cn.hyphaList)-1] + } + } +} + type hyphaNode struct { // TODO: ensure this is sorted categoryList []string @@ -72,6 +81,15 @@ func (hn *hyphaNode) storeCategory(cat string) { hn.categoryList = append(hn.categoryList, cat) } +func (hn *hyphaNode) removeCategory(cat string) { + for i, category := range hn.categoryList { + if category == cat { + hn.categoryList[i] = hn.categoryList[len(hn.categoryList)-1] + hn.categoryList = hn.categoryList[:len(hn.categoryList)-1] + } + } +} + type catFileRecord struct { Categories []catRecord `json:"categories"` } diff --git a/views/categories.html b/views/categories.html index 7f7f999..67364c7 100644 --- a/views/categories.html +++ b/views/categories.html @@ -9,14 +9,16 @@
+
{{end}}
  • - + +
  • @@ -43,6 +45,7 @@
    +
    diff --git a/web/categories.go b/web/categories.go index 8202010..44381c8 100644 --- a/web/categories.go +++ b/web/categories.go @@ -1,6 +1,7 @@ package web import ( + "github.com/bouncepaw/mycorrhiza/hyphae/categories" "github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/views" "github.com/gorilla/mux" @@ -22,9 +23,23 @@ func handlerCategory(w http.ResponseWriter, rq *http.Request) { } func handlerRemoveFromCategory(w http.ResponseWriter, rq *http.Request) { - + util.PrepareRq(rq) + var ( + hyphaName = rq.PostFormValue("hypha") + catName = rq.PostFormValue("cat") + redirectTo = rq.PostFormValue("redirect-to") + ) + categories.RemoveHyphaFromCategory(hyphaName, catName) + http.Redirect(w, rq, redirectTo, http.StatusSeeOther) } func handlerAddToCategory(w http.ResponseWriter, rq *http.Request) { - + util.PrepareRq(rq) + var ( + hyphaName = rq.PostFormValue("hypha") + catName = rq.PostFormValue("cat") + redirectTo = rq.PostFormValue("redirect-to") + ) + categories.AddHyphaToCategory(hyphaName, catName) + http.Redirect(w, rq, redirectTo, http.StatusSeeOther) }