diff --git a/cmd/feed.go b/cmd/feed.go index 35eda59..579eef4 100644 --- a/cmd/feed.go +++ b/cmd/feed.go @@ -27,6 +27,7 @@ Available formats: var feedFormat string var feedSource string var feedChannel string +var feedShowInactive bool func init() { rootCmd.AddCommand(feedCmd) @@ -35,6 +36,7 @@ func init() { feedCmd.Flags().StringVarP(&feedSource, "source", "s", "", "Limit to items from source") feedCmd.Flags().StringVarP(&feedChannel, "channel", "c", "", "Limit to items from channel") feedCmd.MarkFlagsMutuallyExclusive("source", "channel") + feedCmd.Flags().BoolVar(&feedShowInactive, "all", false, "Show inactive items") } func feed() { @@ -47,14 +49,22 @@ func feed() { var items []core.Item if feedSource != "" { - items, err = core.GetActiveItemsForSource(db, feedSource) + if feedShowInactive { + items, err = core.GetAllItemsForSource(db, feedSource) + } else { + items, err = core.GetActiveItemsForSource(db, feedSource) + } if err != nil { log.Fatalf("error: failed to fetch items from %s", feedSource) } } else if feedChannel != "" { log.Fatal("error: unimplemented") } else { - items, err = core.GetAllActiveItems(db) + if feedShowInactive { + items, err = core.GetAllItems(db) + } else { + items, err = core.GetAllActiveItems(db) + } if err != nil { log.Fatal("error: failed to fetch items") } diff --git a/core/source.go b/core/source.go index bd0cfd7..c77bf12 100644 --- a/core/source.go +++ b/core/source.go @@ -87,6 +87,18 @@ func DeactivateItem(db *DB, source string, id string) (bool, error) { return active, nil } +func DeleteItem(db *DB, source string, id string) (int64, error) { + res, err := db.Exec(` + delete from items + where source = ? + and id = ? + `, source, id) + if err != nil { + return 0, err + } + return res.RowsAffected() +} + func getItems(db *DB, query string, args ...any) ([]Item, error) { rows, err := db.Query(query, args...) if err != nil { @@ -113,6 +125,14 @@ func GetAllActiveItems(db *DB) ([]Item, error) { `) } +func GetAllItems(db *DB) ([]Item, error) { + return getItems(db, ` + select + source, id, created, active, title, author, body, link, time + from items + `) +} + func GetActiveItemsForSource(db *DB, source string) ([]Item, error) { return getItems(db, ` select @@ -123,3 +143,13 @@ func GetActiveItemsForSource(db *DB, source string) ([]Item, error) { and active <> 0 `, source) } + +func GetAllItemsForSource(db *DB, source string) ([]Item, error) { + return getItems(db, ` + select + source, id, created, active, title, author, body, link, time + from items + where + source = ? + `, source) +} diff --git a/core/source_test.go b/core/source_test.go index 5f6ec4c..d866677 100644 --- a/core/source_test.go +++ b/core/source_test.go @@ -85,4 +85,36 @@ func TestAddItem(t *testing.T) { if len(items) != 1 { t.Fatal("should get one item") } + + items, err = GetAllItemsForSource(db, "test") + if err != nil { + t.Fatal(err) + } + if len(items) != 2 { + t.Fatal("should get two items") + } + + deleted, err := DeleteItem(db, "test", "one") + if err != nil { + t.Fatal(err) + } + if deleted != 1 { + t.Fatal("expected one deletion") + } + + deleted, err = DeleteItem(db, "test", "one") + if err != nil { + t.Fatal(err) + } + if deleted != 0 { + t.Fatal("expected no deletion") + } + + items, err = GetAllItemsForSource(db, "test") + if err != nil { + t.Fatal(err) + } + if len(items) != 1 { + t.Fatal("should get one item") + } }