Use option for req-by-ID

This commit is contained in:
Daniel J. Summers 2024-06-23 17:09:56 -04:00
parent a2feaff4da
commit 5beeee4819
5 changed files with 13 additions and 16 deletions

View File

@ -25,6 +25,6 @@ class Note
public static function byRequestId(string $id): array public static function byRequestId(string $id): array
{ {
$req = Request::byId($id); $req = Request::byId($id);
return $req ? $req->notes : []; return $req->isDefined() ? $req->get()->notes : [];
} }
} }

View File

@ -2,11 +2,13 @@
namespace MyPrayerJournal\Domain; 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 DateTimeImmutable;
use Exception; use Exception;
use JsonSerializable; use JsonSerializable;
use MyPrayerJournal\Table; use MyPrayerJournal\Table;
use PhpOption\{None, Option, Some};
use Visus\Cuid2\Cuid2; use Visus\Cuid2\Cuid2;
/** /**
@ -110,13 +112,13 @@ class Request implements JsonSerializable
* Find a request by its ID * Find a request by its ID
* *
* @param string $id The ID of the request * @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<Request> The request (if it is found and belongs to the current user)
* @throws DocumentException If any is encountered * @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); $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();
} }
/** /**

View File

@ -13,14 +13,13 @@ $isNew = $_GET['id'] == 'new';
$req = match ($isNew) { $req = match ($isNew) {
true => new Request('new'), true => new Request('new'),
false => Request::byId($_GET['id']) false => Request::byId($_GET['id'])->getOrCall(not_found(...))
}; };
if (!$req) not_found();
$cancelLink = match (true) { $cancelLink = match (true) {
str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'active.php') => '/requests/active', str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'active') => '/requests/active',
str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'snoozed.php') => '/requests/snoozed', str_ends_with($_SERVER['HTTP_REFERER'] ?? '', 'snoozed') => '/requests/snoozed',
default => '/journal' default => '/journal'
}; };
$action = $_GET['id'] == 'new' ? 'Add' : 'Edit'; $action = $_GET['id'] == 'new' ? 'Add' : 'Edit';

View File

@ -26,8 +26,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
see_other('/journal'); see_other('/journal');
case 'PATCH': case 'PATCH':
$req = Request::byId($_PATCH['requestId']); $req = Request::byId($_PATCH['requestId'])->getOrCall(not_found(...));
if (!$req) not_found();
$patch = []; $patch = [];
// update recurrence if changed // update recurrence if changed
if ($recurrence != $req->recurrence) { if ($recurrence != $req->recurrence) {

View File

@ -79,8 +79,5 @@ function validate_request(string $id, array $methods, bool $redirect = true): Re
Auth::requireUser($redirect); Auth::requireUser($redirect);
$req = Request::byId($id); return Request::byId($id)->getOrCall(not_found(...));
if (!$req) not_found();
return $req;
} }