diff --git a/src/lib/Domain/Note.php b/src/lib/Domain/Note.php index fd5db71..ba95e68 100644 --- a/src/lib/Domain/Note.php +++ b/src/lib/Domain/Note.php @@ -25,6 +25,6 @@ class Note public static function byRequestId(string $id): array { $req = Request::byId($id); - return $req ? $req->notes : []; + return $req->isDefined() ? $req->get()->notes : []; } } diff --git a/src/lib/Domain/Request.php b/src/lib/Domain/Request.php index a595da2..84adda5 100644 --- a/src/lib/Domain/Request.php +++ b/src/lib/Domain/Request.php @@ -2,11 +2,13 @@ namespace MyPrayerJournal\Domain; -use BitBadger\PDODocument\{Custom, DocumentException, DocumentList, Find, Mapper\DocumentMapper}; +use BitBadger\PDODocument\{Custom, DocumentException, DocumentList, Find}; +use BitBadger\PDODocument\Mapper\DocumentMapper; use DateTimeImmutable; use Exception; use JsonSerializable; use MyPrayerJournal\Table; +use PhpOption\{None, Option, Some}; use Visus\Cuid2\Cuid2; /** @@ -110,13 +112,13 @@ class Request implements JsonSerializable * Find a request by its ID * * @param string $id The ID of the request - * @return Request|false The request if it is found and belongs to the current user, false if not + * @return Option The request (if it is found and belongs to the current user) * @throws DocumentException If any is encountered */ - public static function byId(string $id): Request|false + public static function byId(string $id): Option { $req = Find::byId(Table::REQUEST, $id, self::class); - return ($req && $req->userId == $_SESSION['user_id']) ? $req : false; + return ($req && $req->userId == $_SESSION['user_id']) ? Some::create($req) : None::create(); } /** diff --git a/src/public/request/edit.php b/src/public/request/edit.php index 8d1238f..8b32468 100644 --- a/src/public/request/edit.php +++ b/src/public/request/edit.php @@ -13,14 +13,13 @@ $isNew = $_GET['id'] == 'new'; $req = match ($isNew) { true => new Request('new'), - false => Request::byId($_GET['id']) + false => Request::byId($_GET['id'])->getOrCall(not_found(...)) }; -if (!$req) not_found(); $cancelLink = match (true) { - str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'active.php') => '/requests/active', - str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'snoozed.php') => '/requests/snoozed', - default => '/journal' + str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'active') => '/requests/active', + str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'snoozed') => '/requests/snoozed', + default => '/journal' }; $action = $_GET['id'] == 'new' ? 'Add' : 'Edit'; diff --git a/src/public/request/save.php b/src/public/request/save.php index 7d3bea1..0d56f86 100644 --- a/src/public/request/save.php +++ b/src/public/request/save.php @@ -26,8 +26,7 @@ switch ($_SERVER['REQUEST_METHOD']) { see_other('/journal'); case 'PATCH': - $req = Request::byId($_PATCH['requestId']); - if (!$req) not_found(); + $req = Request::byId($_PATCH['requestId'])->getOrCall(not_found(...)); $patch = []; // update recurrence if changed if ($recurrence != $req->recurrence) { diff --git a/src/start.php b/src/start.php index abe4103..1581354 100644 --- a/src/start.php +++ b/src/start.php @@ -79,8 +79,5 @@ function validate_request(string $id, array $methods, bool $redirect = true): Re Auth::requireUser($redirect); - $req = Request::byId($id); - if (!$req) not_found(); - - return $req; + return Request::byId($id)->getOrCall(not_found(...)); }