beta4 changes (#26)

These changes are mostly in underlying libraries; however, this now uses the [inspired by F#](https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp) library to handle the feed parsing pipeline and optional return values

Reviewed-on: #26
This commit was merged in pull request #26.
This commit is contained in:
2024-08-06 23:20:17 +00:00
parent dfd9a873f8
commit d06249aecd
35 changed files with 867 additions and 535 deletions

View File

@@ -1,11 +1,16 @@
<?php declare(strict_types=1);
<?php
/**
* Add/Edit/Delete Feed Page
*
* Allows users to add, edit, and delete feeds
*
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\InspiredByFSharp\Result;
use BitBadger\PDODocument\{Delete, DocumentException, Field};
use FeedReaderCentral\{Feed, Security, Table};
@@ -15,39 +20,40 @@ Security::verifyUser();
$feedId = key_exists('id', $_GET) ? (int)$_GET['id'] : -1;
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
try {
if (!($feed = Feed::retrieveById($feedId))) not_found();
Delete::byFields(Table::ITEM, [Field::EQ('feed_id', $feed->id)]);
Delete::byId(Table::FEED, $feed->id);
add_info('Feed &ldquo;' . htmlentities($feed->title) . '&rdquo; deleted successfully');
frc_redirect('/feeds');
} catch (DocumentException $ex) {
add_error("$ex");
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
try {
$isNew = $_POST['id'] == '-1';
if ($isNew) {
$result = Feed::add($_POST['url']);
} else {
$feedId = (int)$_POST['id'];
$toEdit = Feed::retrieveById($feedId);
$result = $toEdit
? Feed::update($toEdit, $_POST['url'])
: ['error' => "Feed $feedId not found"];
}
if (key_exists('ok', $result)) {
add_info('Feed saved successfully');
switch ($_SERVER['REQUEST_METHOD']) {
case 'DELETE':
try {
$feed = Feed::retrieveById($feedId)->getOrCall(not_found(...));
Delete::byFields(Table::Item, [Field::EQ('feed_id', $feed->id)]);
Delete::byId(Table::Feed, $feed->id);
add_info('Feed &ldquo;' . htmlentities($feed->title) . '&rdquo; deleted successfully');
frc_redirect('/feeds');
} catch (DocumentException $ex) {
add_error("$ex");
}
add_error($result['error']);
$feedId = 'error';
} catch (DocumentException $ex) {
add_error("$ex");
}
break;
case 'POST':
try {
if ((int)$_POST['id'] === -1) {
$result = Feed::add($_POST['url']);
} else {
$feedId = (int)$_POST['id'];
$toEdit = Feed::retrieveById($feedId);
$result = $toEdit->isSome()
? Feed::update($toEdit->get(), $_POST['url'])
: Result::Error("Feed $feedId not found");
}
$result->iter(function () {
add_info('Feed saved successfully');
frc_redirect('/feeds');
});
add_error($result->getError());
$feedId = 'error';
} catch (DocumentException $ex) {
add_error("$ex");
}
break;
}
if ($feedId == -1) {
@@ -55,9 +61,9 @@ if ($feedId == -1) {
$feed = new Feed(id: -1);
} else {
$title = 'Edit RSS Feed';
if ($feedId == 'error') {
$feed = new Feed(id: (int)$_POST['id'], url: $_POST['url'] ?? '');
} elseif (!($feed = Feed::retrieveById((int)$feedId))) not_found();
$feed = $feedId == 'error'
? new Feed(id: (int)$_POST['id'], url: $_POST['url'] ?? '')
: Feed::retrieveById((int)$feedId)->getOrCall(not_found(...));
}
page_head($title); ?>