diff --git a/src/Sender/Frontend/RPC.php b/src/Sender/Frontend/RPC.php index 4d3c5e57..be5b82f6 100644 --- a/src/Sender/Frontend/RPC.php +++ b/src/Sender/Frontend/RPC.php @@ -32,26 +32,28 @@ public function handleMessage(mixed $message): ?Message\Rpc return null; } - return $this->callMethod($method); + $data = isset($message['data']) && \is_array($message['data']) ? $message['data'] : []; + + return $this->callMethod($method, $data); } catch (\Throwable $e) { $this->logger->exception($e); return null; } } - private function callMethod(string $initMethod): ?Message\Rpc + private function callMethod(string $initMethod, array $data): ?Message\Rpc { [$method, $path] = \explode(':', $initMethod, 2) + [1 => '']; $route = $this->router->match(Method::fromString($method), $path); if ($route === null) { - // todo: Error message? + $this->logger->error('RPC method `%s` not found.', $initMethod); return null; } /** @var mixed $result */ - $result = $route(); + $result = $route(...$data); return $result === null ? null : new Message\Rpc(data: $result); } } diff --git a/src/Sender/Frontend/Service.php b/src/Sender/Frontend/Service.php index aa481543..625a8698 100644 --- a/src/Sender/Frontend/Service.php +++ b/src/Sender/Frontend/Service.php @@ -33,9 +33,11 @@ public function version(): Version } #[RegexpRoute(Method::Delete, '#^api/event/(?[a-f0-9-]++)$#i')] - #[AssertSuccess(Method::Delete, 'api/event/0145a0e0-0b1a-4e4a-9b1a', ['uuid' => '0145a0e0-0b1a-4e4a-9b1a'])] - #[AssertFail(Method::Delete, 'api/event/foo-bar-baz')] - #[AssertFail(Method::Delete, 'api/event/')] + #[ + AssertSuccess(Method::Delete, 'api/event/0145a0e0-0b1a-4e4a-9b1a', ['uuid' => '0145a0e0-0b1a-4e4a-9b1a']), + AssertFail(Method::Delete, 'api/event/foo-bar-baz'), + AssertFail(Method::Delete, 'api/event/'), + ] public function eventDelete(string $uuid): Success { $this->debug('Delete event %s', $uuid); @@ -44,9 +46,11 @@ public function eventDelete(string $uuid): Success } #[RegexpRoute(Method::Get, '#^api/event/(?[a-f0-9-]++)$#i')] - #[AssertSuccess(Method::Get, 'api/event/0145a0e0-0b1a-4e4a-9b1a', ['uuid' => '0145a0e0-0b1a-4e4a-9b1a'])] - #[AssertFail(Method::Get, 'api/event/foo-bar-baz')] - #[AssertFail(Method::Get, 'api/event/')] + #[ + AssertSuccess(Method::Get, 'api/event/0145a0e0-0b1a-4e4a-9b1a', ['uuid' => '0145a0e0-0b1a-4e4a-9b1a']), + AssertFail(Method::Get, 'api/event/foo-bar-baz'), + AssertFail(Method::Get, 'api/event/'), + ] public function eventShow(string $uuid): Event|Success { $this->debug('Show event %s', $uuid); @@ -55,18 +59,38 @@ public function eventShow(string $uuid): Event|Success } #[StaticRoute(Method::Delete, 'api/events')] - #[AssertFail(Method::Delete, '/api/events')] - #[AssertFail(Method::Delete, 'api/events/')] - public function eventsDelete(): Success + #[ + AssertFail(Method::Delete, '/api/events'), + AssertFail(Method::Delete, 'api/events/'), + AssertFail(Method::Delete, 'api/event'), + ] + public function eventsDelete(array $uuids = []): Success { $this->debug('Delete all events'); - $this->eventsStorage->clear(); + if (empty($uuids)) { + $this->eventsStorage->clear(); + return new Success(); + } + + try { + foreach ($uuids as $uuid) { + \is_string($uuid) or throw new \InvalidArgumentException('UUID must be a string'); + $this->eventsStorage->delete($uuid); + } + } catch (\Throwable $e) { + $this->logger->exception($e); + return new Success(status: false); + } + return new Success(); } #[StaticRoute(Method::Get, 'api/events')] - #[AssertFail(Method::Post, 'api/events')] - #[AssertFail(Method::Get, '/api/events')] + #[ + AssertFail(Method::Get, 'api/event'), + AssertFail(Method::Post, 'api/events'), + AssertFail(Method::Get, '/api/events'), + ] public function eventsList(): EventCollection { $this->debug('List all events');