Use option for req-by-ID
This commit is contained in:
parent
a2feaff4da
commit
5beeee4819
|
@ -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 : [];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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(...));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user