Fix insert statements; other minor tweaks

This commit is contained in:
2024-06-09 18:01:04 -04:00
parent edc9a218b7
commit 3dc314b2e3
9 changed files with 117 additions and 102 deletions

View File

@@ -2,6 +2,8 @@
namespace FeedReaderCentral;
use BitBadger\PDODocument\{Custom, DocumentException, Parameters, Patch};
/**
* An item from a feed
*/
@@ -47,21 +49,42 @@ class Item
}
/**
* Create an item document from a parsed feed item
* Add an item
*
* @param int $feedId The ID of the feed to which this item belongs
* @param ParsedItem $item The parsed feed item
* @return static The item document
* @param int $feedId The ID of the feed to which the item belongs
* @param ParsedItem $parsed The parsed item from the feed XML
* @throws DocumentException If any is encountered
*/
public static function fromFeedItem(int $feedId, ParsedItem $item): static
public static function add(int $feedId, ParsedItem $parsed): void
{
return new static(
feed_id: $feedId,
title: $item->title,
item_guid: $item->guid,
item_link: $item->link,
published_on: $item->publishedOn,
updated_on: $item->updatedOn,
content: $item->content);
Custom::nonQuery(<<<'SQL'
INSERT INTO item (data)
VALUES (json_set(:data, '$.id', (SELECT coalesce(max(data->>'id'), 0) + 1 FROM item)))
SQL, Parameters::json(':data', new static(
feed_id: $feedId,
title: $parsed->title,
item_guid: $parsed->guid,
item_link: $parsed->link,
published_on: $parsed->publishedOn,
updated_on: $parsed->updatedOn,
content: $parsed->content)));
}
/**
* Update an item
*
* @param int $id The ID of the item to be updated
* @param ParsedItem $parsed The parsed item from the feed XML
* @throws DocumentException If any is encountered
*/
public static function update(int $id, ParsedItem $parsed): void
{
Patch::byId(Table::ITEM, $id, [
'title' => $parsed->title,
'published_on' => $parsed->publishedOn,
'updated_on' => $parsed->updatedOn,
'content' => $parsed->content,
'is_read' => 0
]);
}
}