Edit/delete bookmarks feature

This commit is contained in:
Justin Hawkins 2022-05-30 13:28:46 +09:30
parent 226fa4a143
commit 173474d23f
8 changed files with 117 additions and 4 deletions

View File

@ -39,6 +39,16 @@ func (m *BookmarkManager) AddBookmark(bm *entity.Bookmark) error {
return nil 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. // ListBookmarks returns all bookmarks.
func (m *BookmarkManager) ListBookmarks() ([]entity.Bookmark, error) { func (m *BookmarkManager) ListBookmarks() ([]entity.Bookmark, error) {
bookmarks := make([]entity.Bookmark, 0, 0) bookmarks := make([]entity.Bookmark, 0, 0)

View File

@ -53,6 +53,8 @@
{{ template "manage.html" . }} {{ template "manage.html" . }}
{{ else if eq .page "config" }} {{ else if eq .page "config" }}
{{ template "config.html" . }} {{ template "config.html" . }}
{{ else if eq .page "edit" }}
{{ template "edit.html" . }}
{{ end }} {{ end }}
{{/* template "foundation_sample.html" . */}} {{/* template "foundation_sample.html" . */}}
</div> </div>

7
web/templates/edit.html Normal file
View File

@ -0,0 +1,7 @@
<div class="grid-x grid-padding-x">
<div class="large-12 cell">
<h5>Edit</h5>
{{ template "edit_form.html" . }}
</div>
</div>

View File

@ -0,0 +1,32 @@
<form onsubmit="return false;" id="edit-form" hx-target="#edit-form">
<table>
<tr>
<th>Title</th>
<td>{{ .bookmark.Info.Title }}</td>
</tr>
<tr>
<th>URL</th>
<td>{{ .bookmark.URL }}</td>
</tr>
<tr>
<th>tags</th>
<td>
{{ template "tags_widget.html" .tw }}
</td>
</tr>
<tr>
<th>Created</th>
<td>{{ (nicetime .bookmark.TimestampCreated).HumanDuration }}</td>
</tr>
<tr>
<th>Last Scraped</th>
<td>{{ (nicetime .bookmark.TimestampLastScraped).HumanDuration }}</td>
</tr>
</table>
<p>
<button type="button" hx-confirm="Delete this bookmark permanently?" hx-delete="/edit/{{.bookmark.ID}}" class="alert button">delete</button>
<button type="button" class="button" hx-post="/edit/{{.bookmark.ID}}">save</button>
</p>
</form>

View File

@ -0,0 +1,2 @@
<p>Bookmark deleted</p>

View File

@ -5,7 +5,7 @@
<table> <table>
<tr> <tr>
<th>id</th> <th>&nbsp;</th>
<th>title/url</th> <th>title/url</th>
<th>tags</th> <th>tags</th>
<th class="show-for-large">created</th> <th class="show-for-large">created</th>
@ -13,7 +13,7 @@
</tr> </tr>
{{ range .bookmarks }} {{ range .bookmarks }}
<tr> <tr>
<th>{{ .ID }}</th> <th><a class="button" href="/edit/{{ .ID }}">edit</a></th>
<td> <td>
<a href="{{ .URL }}">{{ .Info.Title }}</a> <a href="{{ .URL }}">{{ .Info.Title }}</a>
<br> <br>

View File

@ -5,7 +5,7 @@
hx-target="#label-widget" hx-target="#label-widget"
hx-trigger="change"> hx-trigger="change">
<label for="tag-entry" <label for="tag-entry"
class="Xtext-right Xmiddle">Tags</label> class="">Tags</label>
<input id="tag-entry" type="text" name="tag" placeholder="enter tags" /> <input id="tag-entry" type="text" name="tag" placeholder="enter tags" />
</div> </div>

View File

@ -174,7 +174,7 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
r.POST("/tags", func(c *gin.Context) { r.POST("/tags", func(c *gin.Context) {
newTag := c.PostForm("tag") newTag := c.PostForm("tag") // new tag
oldTags := strings.Split(c.PostForm("tags_hidden"), "|") oldTags := strings.Split(c.PostForm("tags_hidden"), "|")
remove := c.Query("remove") 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 return server
} }