From c6011e0cf02530378fa16402b5aa6e87cd34db2d Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sun, 17 Dec 2023 17:27:43 +0400 Subject: [PATCH 1/2] The `Delete events` action now considers id list --- src/Sender/Frontend/RPC.php | 10 ++++--- src/Sender/Frontend/Service.php | 46 ++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 16 deletions(-) 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..61759e20 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,36 @@ 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/') + ] + 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::Post, 'api/events'), + AssertFail(Method::Get, '/api/events') + ] public function eventsList(): EventCollection { $this->debug('List all events'); From 1452fd61c856af2cae2ecd10412f49679f8f03be Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sun, 17 Dec 2023 18:16:49 +0400 Subject: [PATCH 2/2] Fix CS --- src/Sender/Frontend/Service.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Sender/Frontend/Service.php b/src/Sender/Frontend/Service.php index 61759e20..625a8698 100644 --- a/src/Sender/Frontend/Service.php +++ b/src/Sender/Frontend/Service.php @@ -36,7 +36,7 @@ public function version(): Version #[ 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/') + AssertFail(Method::Delete, 'api/event/'), ] public function eventDelete(string $uuid): Success { @@ -49,7 +49,7 @@ public function eventDelete(string $uuid): Success #[ 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/') + AssertFail(Method::Get, 'api/event/'), ] public function eventShow(string $uuid): Event|Success { @@ -61,7 +61,8 @@ public function eventShow(string $uuid): Event|Success #[StaticRoute(Method::Delete, 'api/events')] #[ AssertFail(Method::Delete, '/api/events'), - AssertFail(Method::Delete, 'api/events/') + AssertFail(Method::Delete, 'api/events/'), + AssertFail(Method::Delete, 'api/event'), ] public function eventsDelete(array $uuids = []): Success { @@ -86,8 +87,9 @@ public function eventsDelete(array $uuids = []): Success #[StaticRoute(Method::Get, 'api/events')] #[ + AssertFail(Method::Get, 'api/event'), AssertFail(Method::Post, 'api/events'), - AssertFail(Method::Get, '/api/events') + AssertFail(Method::Get, '/api/events'), ] public function eventsList(): EventCollection {