diff --git a/cmd/itemAdd.go b/cmd/itemAdd.go index 10f6e25..2aeb2e5 100644 --- a/cmd/itemAdd.go +++ b/cmd/itemAdd.go @@ -59,8 +59,15 @@ func itemAdd() { db := openAndMigrateDb() - err := core.AddItem(db, addSource, addId, addTitle, addAuthor, addBody, addLink, addTime) - if err != nil { + if err := core.AddItems(db, []core.Item{{ + Source: addSource, + Id: addId, + Title: addTitle, + Author: addAuthor, + Body: addBody, + Link: addLink, + Time: addTime, + }}); err != nil { log.Fatalf("Failed to add item: %s", err) } diff --git a/core/db_test.go b/core/db_test.go index d49302a..1e73670 100644 --- a/core/db_test.go +++ b/core/db_test.go @@ -11,21 +11,21 @@ func TestDeleteSourceCascade(t *testing.T) { db := EphemeralDb(t) if err := AddSource(db, "source1"); err != nil { - t.Fatal(err) + t.Fatalf("failed to add source1: %v", err) } if err := AddSource(db, "source2"); err != nil { - t.Fatal(err) + t.Fatalf("failed to add source2: %v", err) } - if err := AddItem(db, "source1", "item1", "", "", "", "", 0); err != nil { - t.Fatal(err) - } - if err := AddItem(db, "source2", "item2", "", "", "", "", 0); err != nil { - t.Fatal(err) + if err := AddItems(db, []Item{ + {"source1", "item1", 0, true, "", "", "", "", 0}, + {"source2", "item2", 0, true, "", "", "", "", 0}, + }); err != nil { + t.Fatalf("failed to add items: %v", err) } items, err := GetAllActiveItems(db) if err != nil { - t.Fatal(err) + t.Fatalf("failed to get active items: %v", err) } if len(items) != 2 { t.Fatal("Expected 2 items") @@ -39,10 +39,10 @@ func TestDeleteSourceCascade(t *testing.T) { t.Fatal(err) } if len(items) != 1 { - t.Fatal("Expected only 1 item after source delete") + t.Fatalf("Expected only 1 item after source delete, got %d", len(items)) } - err = AddItem(db, "source1", "item3", "", "", "", "", 0) + err = AddItems(db, []Item{{"source1", "item3", 0, true, "", "", "", "", 0}}) if err == nil { t.Fatal("Unexpected success adding item for nonexistent source") } diff --git a/core/source.go b/core/source.go index 95e5a22..c677add 100644 --- a/core/source.go +++ b/core/source.go @@ -48,24 +48,6 @@ func DeleteSource(db *DB, name string) error { return err } -func AddItem( - db *DB, - source string, - id string, - title string, - author string, - body string, - link string, - time int, -) error { - _, err := db.Exec(` - insert into items (source, id, active, title, author, body, link, time) - values (?, ?, ?, ?, ?, ?, ?, ?) - `, source, id, true, title, author, body, link, time) - - return err -} - func AddItems(db *DB, items []Item) error { return db.Transact(func(tx *sql.Tx) error { stmt, err := tx.Prepare(` diff --git a/core/source_test.go b/core/source_test.go index 5f04deb..838d178 100644 --- a/core/source_test.go +++ b/core/source_test.go @@ -57,18 +57,18 @@ func AssertItemIs(t *testing.T, item Item, expected string) { func TestAddItem(t *testing.T) { db := EphemeralDb(t) if err := AddSource(db, "test"); err != nil { - t.Fatal(err) + t.Fatalf("failed to add source: %v", err) } - if err := AddItem(db, "test", "one", "", "", "", "", 0); err != nil { - t.Fatal(err) - } - if err := AddItem(db, "test", "two", "title", "author", "body", "link", 123456); err != nil { - t.Fatal(err) + if err := AddItems(db, []Item{ + {"test", "one", 0, true, "", "", "", "", 0}, + {"test", "two", 0, true, "title", "author", "body", "link", 123456}, + }); err != nil { + t.Fatalf("failed to add items: %v", err) } items, err := GetActiveItemsForSource(db, "test") if err != nil { - t.Fatal(err) + t.Fatalf("failed to get active items: %v", err) } if len(items) != 2 { t.Fatal("should get two items")