Move domain items up to lib

- WIP on converting more complex queries
This commit is contained in:
2024-05-31 22:57:24 -04:00
parent f7f5dba795
commit 610ab67475
16 changed files with 189 additions and 167 deletions

View File

@@ -10,10 +10,10 @@ use BitBadger\Documents\DocumentException;
use BitBadger\Documents\SQLite\Find;
use BitBadger\Documents\SQLite\Patch;
use FeedReaderCentral\Data;
use FeedReaderCentral\Domain\Item;
use FeedReaderCentral\Domain\Table;
use FeedReaderCentral\Item;
use FeedReaderCentral\Key;
use FeedReaderCentral\Security;
use FeedReaderCentral\Table;
include '../start.php';
@@ -33,11 +33,11 @@ $exists = $existsResult ? $existsResult->fetchArray(SQLITE3_ASSOC) : false;
if (!$exists) not_found();
if (key_exists('action', $_GET)) {
if ($_GET['action'] == 'add') {
$flag = 1;
} elseif ($_GET['action'] == 'remove') {
$flag = 0;
}
$flag = match ($_GET['action']) {
'add' => 1,
'remove' => 0,
default => null
};
if (isset($flag)) {
try {
Patch::byId(Table::ITEM, $id, ['is_bookmarked' => $flag], $db);

View File

@@ -9,9 +9,9 @@ use BitBadger\Documents\DocumentException;
use BitBadger\Documents\Field;
use BitBadger\Documents\SQLite\Delete;
use FeedReaderCentral\Data;
use FeedReaderCentral\Domain\Table;
use FeedReaderCentral\Feed;
use FeedReaderCentral\Security;
use FeedReaderCentral\Table;
include '../../start.php';
@@ -57,12 +57,15 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
}
if ($feedId == 'new') {
$title = 'Add RSS Feed';
$feed = ['id' => $_GET['id'], 'url' => ''];
$title = 'Add RSS Feed';
$feed = new Feed();
$feed->id = $_GET['id'];
} else {
$title = 'Edit RSS Feed';
if ($feedId == 'error') {
$feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? ''];
$feed = new Feed();
$feed->id = $_POST['id'] ?? '';
$feed->url = $_POST['url'] ?? '';
} elseif (!($feed = Feed::retrieveById((int)$feedId))) not_found();
}
@@ -70,10 +73,10 @@ page_head($title); ?>
<h1><?=$title?></h1>
<article>
<form method=POST action=/feed/ hx-post=/feed/>
<input type=hidden name=id value=<?=$feed['id']?>>
<input type=hidden name=id value=<?=$feed->id?>>
<label>
Feed URL
<input type=url name=url required autofocus value="<?=$feed['url']?>">
<input type=url name=url required autofocus value="<?=$feed->url?>">
</label>
<span class=break></span>
<button type=submit>Save</button>

View File

@@ -5,9 +5,16 @@
* List feeds and provide links for maintenance actions
*/
use BitBadger\Documents\ArrayMapper;
use BitBadger\Documents\Field;
use BitBadger\Documents\JsonMapper;
use BitBadger\Documents\Query;
use BitBadger\Documents\SQLite\Custom;
use FeedReaderCentral\Data;
use FeedReaderCentral\Feed;
use FeedReaderCentral\Key;
use FeedReaderCentral\Security;
use FeedReaderCentral\Table;
include '../start.php';
@@ -15,42 +22,34 @@ $db = Data::getConnection();
Security::verifyUser($db);
// TODO: adapt query when document list is done
$feedQuery = $db->prepare('SELECT * FROM feed WHERE user_id = :user ORDER BY lower(title)');
$feedQuery->bindValue(':user', $_SESSION[Key::USER_ID]);
if (!($feedResult = $feedQuery->execute())) {
add_error(Data::error($db)['error']);
}
$field = Field::EQ('user_id', $_SESSION[Key::USER_ID], '@user');
$feeds = Custom::list(Query\Find::byFields(Table::FEED, [$field]) . " ORDER BY lower(data->>'title')",
$field->toParameter(), new JsonMapper(Feed::class));
page_head('Your Feeds'); ?>
<h1>Your Feeds</h1>
<article>
<p class=action_buttons><?=hx_get('/feed/?id=new', 'Add Feed')?></p><?php
if ($feedResult) {
while ($feed = $feedResult->fetchArray(SQLITE3_ASSOC)) {
$feedId = $feed['id'];
$countQuery = $db->prepare(<<<'SQL'
SELECT (SELECT COUNT(*) FROM item WHERE feed_id = :feed) AS total,
(SELECT COUNT(*) FROM item WHERE feed_id = :feed AND is_read = 0) AS unread,
(SELECT COUNT(*) FROM item WHERE feed_id = :feed AND is_bookmarked = 1) AS marked
SQL);
$countQuery->bindValue(':feed', $feed['id']);
$countResult = $countQuery->execute();
$counts = $countResult
? $countResult->fetchArray(SQLITE3_ASSOC) : ['total' => 0, 'unread' => 0, 'marked' => 0]; ?>
<p><strong><?=htmlentities($feed['title'])?></strong><br>
<span class=meta><em>Last Updated <?=date_time($feed['updated_on'])?> &bull;
As of <?=date_time($feed['checked_on'])?></em><br>
<?=hx_get("/feed/?id=$feedId", 'Edit')?> &bull; Read
<?=$counts['unread'] > 0 ? hx_get("/feed/items?id=$feedId&unread", 'Unread') : 'Unread'?>
iterator_apply($feeds->items(), function (Feed $feed) {
$item = Table::ITEM;
$counts = Custom::single(<<<SQL
SELECT (SELECT COUNT(*) FROM $item WHERE data->>'feed_id' = @feed) AS total,
(SELECT COUNT(*) FROM $item WHERE data->>'feed_id' = @feed AND data->>'is_read' = '0') AS unread,
(SELECT COUNT(*) FROM $item WHERE data->>'feed_id' = @feed AND data->>'is_bookmarked' = '1') AS marked
SQL, ['@feed' => $feed->id], new ArrayMapper()) ?? ['total' => 0, 'unread' => 0, 'marked' => 0]; ?>
<p><strong><?=htmlentities($feed->title)?></strong><br>
<span class=meta><em>Last Updated <?=date_time($feed->updated_on)?> &bull;
As of <?=date_time($feed->checked_on)?></em><br>
<?=hx_get("/feed/?id=$feed->id", 'Edit')?> &bull; Read
<?=$counts['unread'] > 0 ? hx_get("/feed/items?id=$feed->id&unread", 'Unread') : 'Unread'?>
(<?=$counts['unread']?>) |
<?=$counts['total'] > 0 ? hx_get("/feed/items?id=$feedId", 'All') : 'All'?> (<?=$counts['total']?>) |
<?=$counts['marked'] > 0 ? hx_get("/feed/items?id=$feedId&bookmarked", 'Bookmarked') : 'Bookmarked'?>
<?=$counts['total'] > 0 ? hx_get("/feed/items?id=$feed->id", 'All') : 'All'?> (<?=$counts['total']?>) |
<?=$counts['marked'] > 0 ? hx_get("/feed/items?id=$feed->id&bookmarked", 'Bookmarked') : 'Bookmarked'?>
(<?=$counts['marked']?>) &bull;
<a href=/feed/?id=<?=$feedId?> hx-delete=/feed/?id=<?=$feedId?>
hx-confirm="Are you sure you want to delete &ldquo;<?=htmlentities($feed['title'], ENT_QUOTES)?>&rdquo;? This will remove the feed and all its items, including unread and bookmarked.">Delete</a>
</span><?php
}
} ?>
<a href=/feed/?id=<?=$feed->id?> hx-delete=/feed/?id=<?=$feed->id?>
hx-confirm="Are you sure you want to delete &ldquo;<?=htmlspecialchars($feed->title)?>&rdquo;? This will remove the feed and all its items, including unread and bookmarked.">Delete</a>
</span><?php
}); ?>
</article><?php
page_foot();
$db->close();

View File

@@ -6,11 +6,14 @@
* Retrieves and displays an item from a feed belonging to the current user
*/
use BitBadger\Documents\DocumentException;
use BitBadger\Documents\SQLite\Delete;
use BitBadger\Documents\SQLite\Patch;
use FeedReaderCentral\Data;
use FeedReaderCentral\Domain\Table;
use FeedReaderCentral\Item;
use FeedReaderCentral\Key;
use FeedReaderCentral\Security;
use FeedReaderCentral\Table;
include '../start.php';
@@ -18,47 +21,33 @@ $db = Data::getConnection();
Security::verifyUser($db);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// "Keep as New" button sends a POST request to reset the is_read flag before going back to the list of unread items
$isValidQuery = $db->prepare(<<<'SQL'
SELECT COUNT(*)
FROM item INNER JOIN feed ON feed.id = item.feed_id
WHERE item.id = :id AND feed.user_id = :user
SQL);
$isValidQuery->bindValue(':id', $_POST['id']);
$isValidQuery->bindValue(':user', $_SESSION[Key::USER_ID]);
$isValidResult = $isValidQuery->execute();
if ($isValidResult && $isValidResult->fetchArray(SQLITE3_NUM)[0] == 1) {
Patch::byId(Table::ITEM, $_POST['id'], ['is_read' => 0], $db);
// $keepUnread = $db->prepare('UPDATE item SET is_read = 0 WHERE id = :id');
// $keepUnread->bindValue(':id', $_POST['id']);
// $keepUnread->execute();
try {
// "Keep as New" button sends a POST request to reset the is_read flag before going back to the item list
if (Item::retrieveByIdForUser($_POST['id'])) {
Patch::byId(Table::ITEM, $_POST['id'], ['is_read' => 0], $db);
}
$db->close();
frc_redirect($_POST['from']);
} catch (DocumentException $ex) {
add_error("$ex");
}
$db->close();
frc_redirect($_POST['from']);
}
$from = $_GET['from'] ?? '/';
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
$deleteQuery = $db->prepare(<<<'SQL'
DELETE FROM item
WHERE id IN (
SELECT item.id
FROM item INNER JOIN feed ON feed.id = item.feed_id
WHERE item.id = :id
AND feed.user_id = :user)
SQL);
$deleteQuery->bindValue(':id', $_GET['id']);
$deleteQuery->bindValue(':user', $_SESSION[Key::USER_ID]);
if ($deleteQuery->execute()) {
add_info('Item deleted');
} else {
add_error(Data::error($db)['error']);
try {
if (Item::retrieveByIdForUser($_GET['id'])) {
Delete::byId(Table::ITEM, $_GET['id'], $db);
}
} catch (DocumentException $ex) {
add_error("$ex");
}
$db->close();
frc_redirect($from);
}
// TODO: convert this query
$query = $db->prepare(<<<'SQL'
SELECT item.title AS item_title, item.item_link, item.published_on, item.updated_on, item.content,
feed.title AS feed_title
@@ -72,9 +61,11 @@ $result = $query->execute();
$item = $result ? $result->fetchArray(SQLITE3_ASSOC) : false;
if ($item) {
$markRead = $db->prepare('UPDATE item SET is_read = 1 WHERE id = :id');
$markRead->bindValue(':id', $_GET['id']);
$markRead->execute();
try {
Patch::byId(Table::ITEM, $_GET['id'], ['is_read' => 1], $db);
} catch (DocumentException $ex) {
add_error("$ex");
}
}
$published = date_time($item['published_on']);