Order items by .time or .created

This commit is contained in:
Tim Van Baak 2025-01-29 22:41:50 -08:00
parent c4d53eb993
commit 647584e55b
2 changed files with 8 additions and 2 deletions

View File

@ -185,6 +185,7 @@ func GetItem(db *DB, source string, id string) (Item, error) {
from items from items
where source = ? where source = ?
and id = ? and id = ?
order by case when time = 0 then created else time end, id
`, source, id) `, source, id)
if err != nil { if err != nil {
return Item{}, err return Item{}, err
@ -201,6 +202,7 @@ func GetAllActiveItems(db *DB) ([]Item, error) {
source, id, created, active, title, author, body, link, time, json(action) source, id, created, active, title, author, body, link, time, json(action)
from items from items
where active <> 0 where active <> 0
order by case when time = 0 then created else time end, id
`) `)
} }
@ -209,6 +211,7 @@ func GetAllItems(db *DB) ([]Item, error) {
select select
source, id, created, active, title, author, body, link, time, json(action) source, id, created, active, title, author, body, link, time, json(action)
from items from items
order by case when time = 0 then created else time end, id
`) `)
} }
@ -220,6 +223,7 @@ func GetActiveItemsForSource(db *DB, source string) ([]Item, error) {
where where
source = ? source = ?
and active <> 0 and active <> 0
order by case when time = 0 then created else time end, id
`, source) `, source)
} }
@ -230,6 +234,7 @@ func GetAllItemsForSource(db *DB, source string) ([]Item, error) {
from items from items
where where
source = ? source = ?
order by case when time = 0 then created else time end, id
`, source) `, source)
} }

View File

@ -73,8 +73,9 @@ func TestAddItem(t *testing.T) {
if len(items) != 2 { if len(items) != 2 {
t.Fatal("should get two items") t.Fatal("should get two items")
} }
AssertItemIs(t, items[0], "test/one/true/////0") // order is by (time ?? created) so this ordering is correct as long as you don't run it in early 1970
AssertItemIs(t, items[1], "test/two/true/title/author/body/link/123456") AssertItemIs(t, items[0], "test/two/true/title/author/body/link/123456")
AssertItemIs(t, items[1], "test/one/true/////0")
if _, err = DeactivateItem(db, "test", "one"); err != nil { if _, err = DeactivateItem(db, "test", "one"); err != nil {
t.Fatal(err) t.Fatal(err)