diff --git a/storage/entry_pagination_builder.go b/storage/entry_pagination_builder.go index e65c09ef..5b7d5ec1 100644 --- a/storage/entry_pagination_builder.go +++ b/storage/entry_pagination_builder.go @@ -20,6 +20,7 @@ type EntryPaginationBuilder struct { conditions []string args []interface{} entryID int64 + order string direction string } @@ -107,20 +108,20 @@ func (e *EntryPaginationBuilder) getPrevNextID(tx *sql.Tx) (prevID int64, nextID WITH entry_pagination AS ( SELECT e.id, - lag(e.id) over (order by e.published_at asc, e.id desc) as prev_id, - lead(e.id) over (order by e.published_at asc, e.id desc) as next_id + lag(e.id) over (order by e.%[1]s asc, e.id desc) as prev_id, + lead(e.id) over (order by e.%[1]s asc, e.id desc) as next_id FROM entries AS e JOIN feeds AS f ON f.id=e.feed_id JOIN categories c ON c.id = f.category_id - WHERE %s - ORDER BY e.published_at asc, e.id desc + WHERE %[2]s + ORDER BY e.%[1]s asc, e.id desc ) - SELECT prev_id, next_id FROM entry_pagination AS ep WHERE %s; + SELECT prev_id, next_id FROM entry_pagination AS ep WHERE %[3]s; ` subCondition := strings.Join(e.conditions, " AND ") finalCondition := fmt.Sprintf("ep.id = $%d", len(e.args)+1) - query := fmt.Sprintf(cte, subCondition, finalCondition) + query := fmt.Sprintf(cte, e.order, subCondition, finalCondition) e.args = append(e.args, e.entryID) var pID, nID sql.NullInt64 @@ -162,12 +163,13 @@ func (e *EntryPaginationBuilder) getEntry(tx *sql.Tx, entryID int64) (*model.Ent } // NewEntryPaginationBuilder returns a new EntryPaginationBuilder. -func NewEntryPaginationBuilder(store *Storage, userID, entryID int64, direction string) *EntryPaginationBuilder { +func NewEntryPaginationBuilder(store *Storage, userID, entryID int64, order, direction string) *EntryPaginationBuilder { return &EntryPaginationBuilder{ store: store, args: []interface{}{userID, "removed"}, conditions: []string{"e.user_id = $1", "e.status <> $2"}, entryID: entryID, + order: order, direction: direction, } } diff --git a/ui/entry_bookmark.go b/ui/entry_bookmark.go index 86155713..388c3643 100644 --- a/ui/entry_bookmark.go +++ b/ui/entry_bookmark.go @@ -49,7 +49,7 @@ func (h *handler) showStarredEntryPage(w http.ResponseWriter, r *http.Request) { entry.Status = model.EntryStatusRead } - entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryDirection) + entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryOrder, user.EntryDirection) entryPaginationBuilder.WithStarred() prevEntry, nextEntry, err := entryPaginationBuilder.Entries() if err != nil { diff --git a/ui/entry_category.go b/ui/entry_category.go index d42dc020..f3df2224 100644 --- a/ui/entry_category.go +++ b/ui/entry_category.go @@ -52,7 +52,7 @@ func (h *handler) showCategoryEntryPage(w http.ResponseWriter, r *http.Request) entry.Status = model.EntryStatusRead } - entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryDirection) + entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryOrder, user.EntryDirection) entryPaginationBuilder.WithCategoryID(categoryID) prevEntry, nextEntry, err := entryPaginationBuilder.Entries() if err != nil { diff --git a/ui/entry_feed.go b/ui/entry_feed.go index 6eebc905..a331e2db 100644 --- a/ui/entry_feed.go +++ b/ui/entry_feed.go @@ -52,7 +52,7 @@ func (h *handler) showFeedEntryPage(w http.ResponseWriter, r *http.Request) { entry.Status = model.EntryStatusRead } - entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryDirection) + entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryOrder, user.EntryDirection) entryPaginationBuilder.WithFeedID(feedID) prevEntry, nextEntry, err := entryPaginationBuilder.Entries() if err != nil { diff --git a/ui/entry_read.go b/ui/entry_read.go index f67c0218..a3eb8bba 100644 --- a/ui/entry_read.go +++ b/ui/entry_read.go @@ -39,7 +39,7 @@ func (h *handler) showReadEntryPage(w http.ResponseWriter, r *http.Request) { return } - entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryDirection) + entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryOrder, user.EntryDirection) entryPaginationBuilder.WithStatus(model.EntryStatusRead) prevEntry, nextEntry, err := entryPaginationBuilder.Entries() if err != nil { diff --git a/ui/entry_search.go b/ui/entry_search.go index fcdb3aef..36a13e3b 100644 --- a/ui/entry_search.go +++ b/ui/entry_search.go @@ -51,7 +51,7 @@ func (h *handler) showSearchEntryPage(w http.ResponseWriter, r *http.Request) { entry.Status = model.EntryStatusRead } - entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryDirection) + entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryOrder, user.EntryDirection) entryPaginationBuilder.WithSearchQuery(searchQuery) prevEntry, nextEntry, err := entryPaginationBuilder.Entries() if err != nil { diff --git a/ui/entry_unread.go b/ui/entry_unread.go index 457b1f62..99033310 100644 --- a/ui/entry_unread.go +++ b/ui/entry_unread.go @@ -48,7 +48,7 @@ func (h *handler) showUnreadEntryPage(w http.ResponseWriter, r *http.Request) { } } - entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryDirection) + entryPaginationBuilder := storage.NewEntryPaginationBuilder(h.store, user.ID, entry.ID, user.EntryOrder, user.EntryDirection) entryPaginationBuilder.WithStatus(model.EntryStatusUnread) entryPaginationBuilder.WithGloballyVisible() prevEntry, nextEntry, err := entryPaginationBuilder.Entries()