Add basic sorting to management interface

This commit is contained in:
Justin Hawkins 2022-06-07 16:25:45 +09:30
parent 42fd1973b8
commit 6cf327226d
4 changed files with 21 additions and 7 deletions

View File

@ -21,6 +21,7 @@ type BookmarkManager struct {
type SearchOptions struct {
Query string
Tags []string
Sort string
}
func NewBookmarkManager(db *DB) *BookmarkManager {
@ -138,10 +139,22 @@ func (m *BookmarkManager) Search(opts SearchOptions) ([]entity.Bookmark, error)
bhQuery = bolthold.Query(*bhQuery.And("Tags").ContainsAll(bolthold.Slice(opts.Tags)...))
}
if opts.Sort == "title" {
bhQuery.SortBy("Info.Title")
} else if opts.Sort == "created" {
bhQuery.SortBy("TimestampCreated")
} else if opts.Sort == "scraped" {
bhQuery.SortBy("TimestampLastScraped")
} else {
bhQuery.SortBy("ID")
}
out := []entity.Bookmark{}
err := m.db.store.ForEach(&bhQuery,
func(bm *entity.Bookmark) error {
out = append(out, *bm)
return nil
})
if err != nil {

View File

@ -13,8 +13,9 @@
</div>
</div>
{{ template "tags_widget.html" . }}
{{ template "manage_results.html" . }}
</form>
{{ template "manage_results.html" . }}
</div>
</div>

View File

@ -2,10 +2,10 @@
<table id="manage-results">
<tr>
<th>&nbsp;</th>
<th>title/url</th>
<th hx-post="/manage/results?sort=title" hx-target="#manage-results">title/url</th>
<th>tags</th>
<th class="show-for-large">created</th>
<th class="show-for-large">scraped</th>
<th hx-post="/manage/results?sort=created" hx-target="#manage-results" class="show-for-large">created</th>
<th hx-post="/manage/results?sort=scraped" hx-target="#manage-results" class="show-for-large">scraped</th>
</tr>
{{ range .bookmarks }}
<tr>

View File

@ -92,14 +92,14 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
r.POST("/manage/results", func(c *gin.Context) {
query := c.PostForm("query")
tags := []string{}
sort := c.Query("sort")
if c.PostForm("tags_hidden") != "" {
tags = strings.Split(c.PostForm("tags_hidden"), "|")
}
allBookmarks, _ := bmm.Search(db.SearchOptions{Query: query, Tags: tags})
allBookmarks, _ := bmm.Search(db.SearchOptions{Query: query, Tags: tags, Sort: sort})
meta := gin.H{"config": config, "bookmarks": allBookmarks}
log.Printf("query is %s, tags %v", query, tags)
c.HTML(http.StatusOK,
"manage_results.html", meta,
)