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
{
$req = Request::byId($id);
return $req ? $req->notes : [];
return $req->isDefined() ? $req->get()->notes : [];
}
}

View File

@ -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<Request> 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();
}
/**

View File

@ -13,13 +13,12 @@ $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',
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';

View File

@ -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) {

View File

@ -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(...));
}