dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit ac3823f40de1f7f1c12eb7e7f0275d665032295e
parent 6a8eeae8dcb39d67261bec2661fdca13cada586e
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon,  3 Apr 2023 13:50:52 -0700

fix links tags stuff -> categories

Diffstat:
Mpkg/database/tableLinks.go | 13+++++++++----
Mpkg/web/handlers/handlers.go | 34+++++++++++++++++-----------------
2 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/pkg/database/tableLinks.go b/pkg/database/tableLinks.go @@ -173,10 +173,10 @@ ORDER BY t.name`, linkID).Scan(&out).Error return } -// LinksTagsLinks many-to-many table -type LinksTagsLinks struct { +// LinksCategoriesLinks many-to-many table +type LinksCategoriesLinks struct { LinkID int64 - TagID int64 + CategoryID int64 } func (d *DkfDB) GetTags() (out []LinksTag, err error) { @@ -184,7 +184,12 @@ func (d *DkfDB) GetTags() (out []LinksTag, err error) { return } -func (d *DkfDB) GetTagsLinks() (out []LinksTagsLinks, err error) { +func (d *DkfDB) GetLinksCategories() (out []LinksCategory, err error) { + err = d.db.Find(&out).Error + return +} + +func (d *DkfDB) GetCategoriesLinks() (out []LinksCategoriesLinks, err error) { err = d.db.Find(&out).Error return } diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -1343,33 +1343,33 @@ func LinksDownloadHandler(c echo.Context) error { logrus.Error(err) } - // Get all tags and make a hashmap for fast access - tags, _ := db.GetTags() - tagsMap := make(map[int64]string) - for _, tag := range tags { - tagsMap[tag.ID] = tag.Name - } - // Get all "tags links" associations between links and their tags - tagsLinks, _ := db.GetTagsLinks() + // Get all categories and make a hashmap for fast access + categories, _ := db.GetLinksCategories() + categoriesMap := make(map[int64]string) + for _, category := range categories { + categoriesMap[category.ID] = category.Name + } + // Get all "categories links" associations between links and their tags + categoriesLinks, _ := db.GetCategoriesLinks() // Build a map of all tag IDs for a given link ID - tagsLinksMap := make(map[int64][]int64) - for _, tl := range tagsLinks { - tagsLinksMap[tl.LinkID] = append(tagsLinksMap[tl.LinkID], tl.TagID) + categoriesLinksMap := make(map[int64][]int64) + for _, cl := range categoriesLinks { + categoriesLinksMap[cl.LinkID] = append(categoriesLinksMap[cl.LinkID], cl.CategoryID) } links, _ := db.GetLinks() by := make([]byte, 0) buf := bytes.NewBuffer(by) w := csv.NewWriter(buf) - _ = w.Write([]string{"UUID", "URL", "Title", "Description", "Tags"}) + _ = w.Write([]string{"UUID", "URL", "Title", "Description", "Categories"}) for _, link := range links { // Get all tags for the link - tagNames := make([]string, 0) - tagIDs := tagsLinksMap[link.ID] - for _, tagID := range tagIDs { - tagNames = append(tagNames, tagsMap[tagID]) + categoryNames := make([]string, 0) + categoryIDs := categoriesLinksMap[link.ID] + for _, tagID := range categoryIDs { + categoryNames = append(categoryNames, categoriesMap[tagID]) } - _ = w.Write([]string{link.UUID, link.URL, link.Title, link.Description, strings.Join(tagNames, ",")}) + _ = w.Write([]string{link.UUID, link.URL, link.Title, link.Description, strings.Join(categoryNames, ",")}) } w.Flush() c.Response().Header().Set("Content-Disposition", `attachment; filename="`+fileName+`"`)