1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-04 18:19:54 +00:00

Categories: Add addition to/removal from categories

* The result of operations is not saved on disk.
* TODO: handle bad names
This commit is contained in:
Timur Ismagilov 2022-03-20 12:28:51 +03:00
parent ea0c2f35d1
commit 9a60cc2386
4 changed files with 46 additions and 3 deletions

View File

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

View File

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

View File

@ -9,14 +9,16 @@
<form method="POST" action="/remove-from-category" class="categories-card__remove-form">
<input type="hidden" name="cat" value="{{.}}">
<input type="hidden" name="hypha" value="{{$hyphaName}}">
<input type="hidden" name="redirect-to" value="/hypha/{{$hyphaName}}">
<input type="submit" value="X">
</form>
</li>
{{end}}
<li class="categories-card__entry categories-card__add-to-cat">
<form method="POST" action="/add-to-category" class="categories-card__add-form">
<input type="text" name="cat" id="_cat-name">
<input type="text" name="cat" id="_cat-name" placeholder="Category name">
<input type="hidden" name="hypha" value="{{$hyphaName}}">
<input type="hidden" name="redirect-to" value="/hypha/{{$hyphaName}}">
<input type="submit" value="Add to category">
</form>
</li>
@ -43,6 +45,7 @@
<form method="POST" action="/add-to-category" class="category__add-form">
<input type="text" name="hypha" id="_hypha-name" placeholder="Hypha name">
<input type="hidden" name="cat" value="{{$catName}}">
<input type="hidden" name="redirect-to" value="/category/{{$catName}}">
<input type="submit" value="Add hypha to category">
</form>
</li>

View File

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