Fix inconsistent navigation

This commit is contained in:
NobeKanai 2021-09-22 14:21:01 +08:00 committed by fguillot
parent edee11931d
commit cf1939f063
7 changed files with 15 additions and 13 deletions

View File

@ -20,6 +20,7 @@ type EntryPaginationBuilder struct {
conditions []string conditions []string
args []interface{} args []interface{}
entryID int64 entryID int64
order string
direction string direction string
} }
@ -107,20 +108,20 @@ func (e *EntryPaginationBuilder) getPrevNextID(tx *sql.Tx) (prevID int64, nextID
WITH entry_pagination AS ( WITH entry_pagination AS (
SELECT SELECT
e.id, e.id,
lag(e.id) over (order by e.published_at asc, e.id desc) as prev_id, lag(e.id) over (order by e.%[1]s asc, e.id desc) as prev_id,
lead(e.id) over (order by e.published_at asc, e.id desc) as next_id lead(e.id) over (order by e.%[1]s asc, e.id desc) as next_id
FROM entries AS e FROM entries AS e
JOIN feeds AS f ON f.id=e.feed_id JOIN feeds AS f ON f.id=e.feed_id
JOIN categories c ON c.id = f.category_id JOIN categories c ON c.id = f.category_id
WHERE %s WHERE %[2]s
ORDER BY e.published_at asc, e.id desc 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 ") subCondition := strings.Join(e.conditions, " AND ")
finalCondition := fmt.Sprintf("ep.id = $%d", len(e.args)+1) 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) e.args = append(e.args, e.entryID)
var pID, nID sql.NullInt64 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. // 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{ return &EntryPaginationBuilder{
store: store, store: store,
args: []interface{}{userID, "removed"}, args: []interface{}{userID, "removed"},
conditions: []string{"e.user_id = $1", "e.status <> $2"}, conditions: []string{"e.user_id = $1", "e.status <> $2"},
entryID: entryID, entryID: entryID,
order: order,
direction: direction, direction: direction,
} }
} }

View File

@ -49,7 +49,7 @@ func (h *handler) showStarredEntryPage(w http.ResponseWriter, r *http.Request) {
entry.Status = model.EntryStatusRead 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() entryPaginationBuilder.WithStarred()
prevEntry, nextEntry, err := entryPaginationBuilder.Entries() prevEntry, nextEntry, err := entryPaginationBuilder.Entries()
if err != nil { if err != nil {

View File

@ -52,7 +52,7 @@ func (h *handler) showCategoryEntryPage(w http.ResponseWriter, r *http.Request)
entry.Status = model.EntryStatusRead 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) entryPaginationBuilder.WithCategoryID(categoryID)
prevEntry, nextEntry, err := entryPaginationBuilder.Entries() prevEntry, nextEntry, err := entryPaginationBuilder.Entries()
if err != nil { if err != nil {

View File

@ -52,7 +52,7 @@ func (h *handler) showFeedEntryPage(w http.ResponseWriter, r *http.Request) {
entry.Status = model.EntryStatusRead 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) entryPaginationBuilder.WithFeedID(feedID)
prevEntry, nextEntry, err := entryPaginationBuilder.Entries() prevEntry, nextEntry, err := entryPaginationBuilder.Entries()
if err != nil { if err != nil {

View File

@ -39,7 +39,7 @@ func (h *handler) showReadEntryPage(w http.ResponseWriter, r *http.Request) {
return 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) entryPaginationBuilder.WithStatus(model.EntryStatusRead)
prevEntry, nextEntry, err := entryPaginationBuilder.Entries() prevEntry, nextEntry, err := entryPaginationBuilder.Entries()
if err != nil { if err != nil {

View File

@ -51,7 +51,7 @@ func (h *handler) showSearchEntryPage(w http.ResponseWriter, r *http.Request) {
entry.Status = model.EntryStatusRead 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) entryPaginationBuilder.WithSearchQuery(searchQuery)
prevEntry, nextEntry, err := entryPaginationBuilder.Entries() prevEntry, nextEntry, err := entryPaginationBuilder.Entries()
if err != nil { if err != nil {

View File

@ -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.WithStatus(model.EntryStatusUnread)
entryPaginationBuilder.WithGloballyVisible() entryPaginationBuilder.WithGloballyVisible()
prevEntry, nextEntry, err := entryPaginationBuilder.Entries() prevEntry, nextEntry, err := entryPaginationBuilder.Entries()