From 675cb64f4798e479ef6a672bb1376bc83228a05e Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 23 Jan 2025 11:34:44 -0800 Subject: [PATCH] Refactor get item functions --- core/source.go | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/core/source.go b/core/source.go index d4add29..bd0cfd7 100644 --- a/core/source.go +++ b/core/source.go @@ -87,13 +87,8 @@ func DeactivateItem(db *DB, source string, id string) (bool, error) { return active, nil } -func GetAllActiveItems(db *DB) ([]Item, error) { - rows, err := db.Query(` - select - source, id, created, active, title, author, body, link, time - from items - where active <> 0 - `) +func getItems(db *DB, query string, args ...any) ([]Item, error) { + rows, err := db.Query(query, args...) if err != nil { return nil, err } @@ -109,8 +104,17 @@ func GetAllActiveItems(db *DB) ([]Item, error) { 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) { - rows, err := db.Query(` + return getItems(db, ` select source, id, created, active, title, author, body, link, time from items @@ -118,17 +122,4 @@ func GetActiveItemsForSource(db *DB, source string) ([]Item, error) { source = ? and active <> 0 `, 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 }