diff --git a/db/bookmarks.go b/db/bookmarks.go index b9650ab..759b22c 100644 --- a/db/bookmarks.go +++ b/db/bookmarks.go @@ -39,6 +39,16 @@ func (m *BookmarkManager) AddBookmark(bm *entity.Bookmark) error { return nil } +func (m *BookmarkManager) DeleteBookmark(bm *entity.Bookmark) error { + err := m.db.store.FindOne(bm, bolthold.Where("URL").Eq(bm.URL)) + if err == bolthold.ErrNotFound { + return fmt.Errorf("bookmark does not exist") + } + + m.db.store.DeleteMatching(bm, bolthold.Where("ID").Eq(bm.ID)) + return nil +} + // ListBookmarks returns all bookmarks. func (m *BookmarkManager) ListBookmarks() ([]entity.Bookmark, error) { bookmarks := make([]entity.Bookmark, 0, 0) diff --git a/web/templates/_layout.html b/web/templates/_layout.html index a0da40c..e8a3337 100644 --- a/web/templates/_layout.html +++ b/web/templates/_layout.html @@ -53,6 +53,8 @@ {{ template "manage.html" . }} {{ else if eq .page "config" }} {{ template "config.html" . }} + {{ else if eq .page "edit" }} + {{ template "edit.html" . }} {{ end }} {{/* template "foundation_sample.html" . */}} diff --git a/web/templates/edit.html b/web/templates/edit.html new file mode 100644 index 0000000..796a5b0 --- /dev/null +++ b/web/templates/edit.html @@ -0,0 +1,7 @@ +
Bookmark deleted
\ No newline at end of file diff --git a/web/templates/manage.html b/web/templates/manage.html index 2aa11b8..a051e27 100644 --- a/web/templates/manage.html +++ b/web/templates/manage.html @@ -5,7 +5,7 @@id | +title/url | tags | created | @@ -13,7 +13,7 @@|
---|---|---|---|---|
{{ .ID }} | +edit |
{{ .Info.Title }}
diff --git a/web/templates/tags_widget.html b/web/templates/tags_widget.html index 4788eed..da7fb23 100644 --- a/web/templates/tags_widget.html +++ b/web/templates/tags_widget.html @@ -5,7 +5,7 @@ hx-target="#label-widget" hx-trigger="change"> + class="">Tags diff --git a/web/web.go b/web/web.go index 0618ed8..22ccac4 100644 --- a/web/web.go +++ b/web/web.go @@ -174,7 +174,7 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server { r.POST("/tags", func(c *gin.Context) { - newTag := c.PostForm("tag") + newTag := c.PostForm("tag") // new tag oldTags := strings.Split(c.PostForm("tags_hidden"), "|") remove := c.Query("remove") @@ -235,6 +235,66 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server { ) }) + r.GET("/edit/:id", func(c *gin.Context) { + bookmarkIDstring := c.Param("id") + bookmarkID, ok := strconv.ParseUint(bookmarkIDstring, 10, 64) + if ok != nil { + c.String(http.StatusBadRequest, "bad id") + return + } + + bookmark := bmm.LoadBookmarkByID(bookmarkID) + meta := gin.H{"page": "edit", "bookmark": bookmark, "tw": gin.H{"tags": bookmark.Tags, "tags_hidden": strings.Join(bookmark.Tags, "|")}} + + c.HTML(http.StatusOK, + "_layout.html", meta, + ) + }) + + r.POST("/edit/:id", func(c *gin.Context) { + bookmarkIDstring := c.Param("id") + bookmarkID, ok := strconv.ParseUint(bookmarkIDstring, 10, 64) + if ok != nil { + c.String(http.StatusBadRequest, "bad id") + return + } + + bookmark := bmm.LoadBookmarkByID(bookmarkID) + + // freshen tags + if c.PostForm("tags_hidden") == "" { + // empty + bookmark.Tags = []string{} + } else { + bookmark.Tags = strings.Split(c.PostForm("tags_hidden"), "|") + } + bmm.SaveBookmark(&bookmark) + + meta := gin.H{"page": "edit", "bookmark": bookmark, "tw": gin.H{"tags": bookmark.Tags, "tags_hidden": strings.Join(bookmark.Tags, "|")}} + + c.HTML(http.StatusOK, + "edit_form.html", meta, + ) + }) + + r.DELETE("/edit/:id", func(c *gin.Context) { + bookmarkIDstring := c.Param("id") + bookmarkID, ok := strconv.ParseUint(bookmarkIDstring, 10, 64) + if ok != nil { + c.String(http.StatusBadRequest, "bad id") + return + } + + bookmark := bmm.LoadBookmarkByID(bookmarkID) + err := bmm.DeleteBookmark(&bookmark) + if err != nil { + panic(err) + } + c.HTML(http.StatusOK, + "edit_form_deleted.html", nil, + ) + }) + return server } |