Implement updating items
This commit is contained in:
parent
1fb9e5853c
commit
a84a464901
@ -90,8 +90,7 @@ func actionExecute() {
|
|||||||
log.Fatalf("error: expected action to produce exactly one item, got %d", len(res))
|
log.Fatalf("error: expected action to produce exactly one item, got %d", len(res))
|
||||||
}
|
}
|
||||||
newItem := res[0]
|
newItem := res[0]
|
||||||
newItem.Active = item.Active // These fields can't
|
core.BackfillItem(&newItem, &item)
|
||||||
newItem.Created = item.Created // be updated by actions
|
|
||||||
|
|
||||||
if actionExecuteDiff {
|
if actionExecuteDiff {
|
||||||
if item.Title != newItem.Title {
|
if item.Title != newItem.Title {
|
||||||
@ -119,5 +118,7 @@ func actionExecute() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
if err = core.UpdateItems(db, []core.Item{newItem}); err != nil {
|
||||||
|
log.Fatalf("error: failed to update item: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,6 +82,56 @@ func AddItems(db *DB, items []Item) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set fields in the new item to match the old item where the new item's fields are zero-valued.
|
||||||
|
// This allows sources to omit fields and let an action set them without a later fetch overwriting
|
||||||
|
// the value from the action, e.g. an on-create action archiving a page and setting the link to
|
||||||
|
// point to the archive.
|
||||||
|
func BackfillItem(new *Item, old *Item) {
|
||||||
|
new.Active = old.Active
|
||||||
|
new.Created = old.Created
|
||||||
|
if new.Author == "" {
|
||||||
|
new.Author = old.Author
|
||||||
|
}
|
||||||
|
if new.Body == "" {
|
||||||
|
new.Body = old.Body
|
||||||
|
}
|
||||||
|
if new.Link == "" {
|
||||||
|
new.Link = old.Link
|
||||||
|
}
|
||||||
|
if new.Time == 0 {
|
||||||
|
new.Time = old.Time
|
||||||
|
}
|
||||||
|
if new.Title == "" {
|
||||||
|
new.Title = old.Title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateItems(db *DB, items []Item) error {
|
||||||
|
return db.Transact(func(tx *sql.Tx) error {
|
||||||
|
stmt, err := tx.Prepare(`
|
||||||
|
update items
|
||||||
|
set
|
||||||
|
title = ?,
|
||||||
|
author = ?,
|
||||||
|
body = ?,
|
||||||
|
link = ?,
|
||||||
|
time = ?
|
||||||
|
where source = ?
|
||||||
|
and id = ?
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, item := range items {
|
||||||
|
_, err = stmt.Exec(item.Title, item.Author, item.Body, item.Link, item.Time, item.Source, item.Id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Deactivate an item, returning its previous active state.
|
// Deactivate an item, returning its previous active state.
|
||||||
func DeactivateItem(db *DB, source string, id string) (bool, error) {
|
func DeactivateItem(db *DB, source string, id string) (bool, error) {
|
||||||
row := db.QueryRow(`
|
row := db.QueryRow(`
|
||||||
@ -197,8 +247,10 @@ func UpdateWithFetchedItems(db *DB, source string, items []Item) (int, int, erro
|
|||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
existingIds := map[string]bool{}
|
existingIds := map[string]bool{}
|
||||||
|
existingItemsById := map[string]*Item{}
|
||||||
for _, item := range existingItems {
|
for _, item := range existingItems {
|
||||||
existingIds[item.Id] = true
|
existingIds[item.Id] = true
|
||||||
|
existingItemsById[item.Id] = &item
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split the fetch into adds and updates
|
// Split the fetch into adds and updates
|
||||||
@ -218,7 +270,12 @@ func UpdateWithFetchedItems(db *DB, source string, items []Item) (int, int, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Bulk update the existing items
|
// Bulk update the existing items
|
||||||
// TODO
|
for _, item := range updatedItems {
|
||||||
|
BackfillItem(&item, existingItemsById[item.Id])
|
||||||
|
}
|
||||||
|
if err = UpdateItems(db, updatedItems); err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
// Get the list of expired items
|
// Get the list of expired items
|
||||||
fetchedIds := map[string]bool{}
|
fetchedIds := map[string]bool{}
|
||||||
|
Loading…
Reference in New Issue
Block a user