Refactor get item functions

This commit is contained in:
Tim Van Baak 2025-01-23 11:34:44 -08:00
parent 2d7d48846d
commit 675cb64f47

View File

@ -87,13 +87,8 @@ func DeactivateItem(db *DB, source string, id string) (bool, error) {
return active, nil return active, nil
} }
func GetAllActiveItems(db *DB) ([]Item, error) { func getItems(db *DB, query string, args ...any) ([]Item, error) {
rows, err := db.Query(` rows, err := db.Query(query, args...)
select
source, id, created, active, title, author, body, link, time
from items
where active <> 0
`)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -109,8 +104,17 @@ func GetAllActiveItems(db *DB) ([]Item, error) {
return items, nil return items, nil
} }
func GetAllActiveItems(db *DB) ([]Item, error) {
return getItems(db, `
select
source, id, created, active, title, author, body, link, time
from items
where active <> 0
`)
}
func GetActiveItemsForSource(db *DB, source string) ([]Item, error) { func GetActiveItemsForSource(db *DB, source string) ([]Item, error) {
rows, err := db.Query(` return getItems(db, `
select select
source, id, created, active, title, author, body, link, time source, id, created, active, title, author, body, link, time
from items from items
@ -118,17 +122,4 @@ func GetActiveItemsForSource(db *DB, source string) ([]Item, error) {
source = ? source = ?
and active <> 0 and active <> 0
`, source) `, source)
if err != nil {
return nil, err
}
var items []Item
for rows.Next() {
var item Item
err = rows.Scan(&item.Source, &item.Id, &item.Created, &item.Active, &item.Title, &item.Author, &item.Body, &item.Link, &item.Time)
if err != nil {
return nil, err
}
items = append(items, item)
}
return items, nil
} }