Use custom Result/Option types

This commit is contained in:
2024-07-26 23:53:11 -04:00
parent 6dc264d34c
commit 8cf84f1450
15 changed files with 157 additions and 95 deletions

View File

@@ -10,6 +10,7 @@
declare(strict_types=1);
use BitBadger\InspiredByFSharp\Option;
use BitBadger\PDODocument\{DocumentException, Patch};
use FeedReaderCentral\{ItemWithFeed, Table};
@@ -18,22 +19,21 @@ include '../start.php';
FeedReaderCentral\Security::verifyUser();
$id = key_exists('id', $_GET) ? (int)$_GET['id'] : -1;
$item = ItemWithFeed::retrieveById($id)->getOrCall(not_found(...));
$item = Option::getOrCall(not_found(...), ItemWithFeed::retrieveById($id));
if (key_exists('action', $_GET)) {
$flag = match ($_GET['action']) {
'add' => 1,
'remove' => 0,
default => null
};
if (isset($flag)) {
Option::iter(function (int $flag) use ($id, &$item) {
try {
Patch::byId(Table::Item, $id, ['is_bookmarked' => $flag]);
$item->is_bookmarked = $flag;
} catch (DocumentException $ex) {
add_error("$ex");
}
}
}, match ($_GET['action']) {
'add' => Option::Some(1),
'remove' => Option::Some(0),
default => Option::None(),
});
}
$action = $item->isBookmarked() ? 'remove' : 'add';