Use option for req-by-ID

This commit is contained in:
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();
}
/**