Add option to show inactive items in feed
This commit is contained in:
parent
675cb64f47
commit
dde799ff8e
10
cmd/feed.go
10
cmd/feed.go
@ -27,6 +27,7 @@ Available formats:
|
|||||||
var feedFormat string
|
var feedFormat string
|
||||||
var feedSource string
|
var feedSource string
|
||||||
var feedChannel string
|
var feedChannel string
|
||||||
|
var feedShowInactive bool
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(feedCmd)
|
rootCmd.AddCommand(feedCmd)
|
||||||
@ -35,6 +36,7 @@ func init() {
|
|||||||
feedCmd.Flags().StringVarP(&feedSource, "source", "s", "", "Limit to items from source")
|
feedCmd.Flags().StringVarP(&feedSource, "source", "s", "", "Limit to items from source")
|
||||||
feedCmd.Flags().StringVarP(&feedChannel, "channel", "c", "", "Limit to items from channel")
|
feedCmd.Flags().StringVarP(&feedChannel, "channel", "c", "", "Limit to items from channel")
|
||||||
feedCmd.MarkFlagsMutuallyExclusive("source", "channel")
|
feedCmd.MarkFlagsMutuallyExclusive("source", "channel")
|
||||||
|
feedCmd.Flags().BoolVar(&feedShowInactive, "all", false, "Show inactive items")
|
||||||
}
|
}
|
||||||
|
|
||||||
func feed() {
|
func feed() {
|
||||||
@ -47,14 +49,22 @@ func feed() {
|
|||||||
|
|
||||||
var items []core.Item
|
var items []core.Item
|
||||||
if feedSource != "" {
|
if feedSource != "" {
|
||||||
|
if feedShowInactive {
|
||||||
|
items, err = core.GetAllItemsForSource(db, feedSource)
|
||||||
|
} else {
|
||||||
items, err = core.GetActiveItemsForSource(db, feedSource)
|
items, err = core.GetActiveItemsForSource(db, feedSource)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error: failed to fetch items from %s", feedSource)
|
log.Fatalf("error: failed to fetch items from %s", feedSource)
|
||||||
}
|
}
|
||||||
} else if feedChannel != "" {
|
} else if feedChannel != "" {
|
||||||
log.Fatal("error: unimplemented")
|
log.Fatal("error: unimplemented")
|
||||||
|
} else {
|
||||||
|
if feedShowInactive {
|
||||||
|
items, err = core.GetAllItems(db)
|
||||||
} else {
|
} else {
|
||||||
items, err = core.GetAllActiveItems(db)
|
items, err = core.GetAllActiveItems(db)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("error: failed to fetch items")
|
log.Fatal("error: failed to fetch items")
|
||||||
}
|
}
|
||||||
|
@ -87,6 +87,18 @@ func DeactivateItem(db *DB, source string, id string) (bool, error) {
|
|||||||
return active, nil
|
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) {
|
func getItems(db *DB, query string, args ...any) ([]Item, error) {
|
||||||
rows, err := db.Query(query, args...)
|
rows, err := db.Query(query, args...)
|
||||||
if err != nil {
|
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) {
|
func GetActiveItemsForSource(db *DB, source string) ([]Item, error) {
|
||||||
return getItems(db, `
|
return getItems(db, `
|
||||||
select
|
select
|
||||||
@ -123,3 +143,13 @@ func GetActiveItemsForSource(db *DB, source string) ([]Item, error) {
|
|||||||
and active <> 0
|
and active <> 0
|
||||||
`, source)
|
`, 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)
|
||||||
|
}
|
||||||
|
@ -85,4 +85,36 @@ func TestAddItem(t *testing.T) {
|
|||||||
if len(items) != 1 {
|
if len(items) != 1 {
|
||||||
t.Fatal("should get one item")
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user