commit 6a8eeae8dcb39d67261bec2661fdca13cada586e
parent 8f4cd48f3799196c98d7c58d8a01867da71e1050
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Mon, 3 Apr 2023 13:37:29 -0700
Add tags to links csv file
Diffstat:
2 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/pkg/database/tableLinks.go b/pkg/database/tableLinks.go
@@ -173,6 +173,22 @@ ORDER BY t.name`, linkID).Scan(&out).Error
return
}
+// LinksTagsLinks many-to-many table
+type LinksTagsLinks struct {
+ LinkID int64
+ TagID int64
+}
+
+func (d *DkfDB) GetTags() (out []LinksTag, err error) {
+ err = d.db.Find(&out).Error
+ return
+}
+
+func (d *DkfDB) GetTagsLinks() (out []LinksTagsLinks, err error) {
+ err = d.db.Find(&out).Error
+ return
+}
+
func (d *DkfDB) DeleteLinkCategories(linkID int64) error {
return d.db.Delete(&LinksCategoriesLink{}, "link_id = ?", linkID).Error
}
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -1343,13 +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()
+ // 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)
+ }
+
links, _ := db.GetLinks()
by := make([]byte, 0)
buf := bytes.NewBuffer(by)
w := csv.NewWriter(buf)
- _ = w.Write([]string{"UUID", "URL", "Title", "Description"})
+ _ = w.Write([]string{"UUID", "URL", "Title", "Description", "Tags"})
for _, link := range links {
- _ = w.Write([]string{link.UUID, link.URL, link.Title, link.Description})
+ // Get all tags for the link
+ tagNames := make([]string, 0)
+ tagIDs := tagsLinksMap[link.ID]
+ for _, tagID := range tagIDs {
+ tagNames = append(tagNames, tagsMap[tagID])
+ }
+ _ = w.Write([]string{link.UUID, link.URL, link.Title, link.Description, strings.Join(tagNames, ",")})
}
w.Flush()
c.Response().Header().Set("Content-Disposition", `attachment; filename="`+fileName+`"`)