Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Considers id list in Delete events action #37

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Sender/Frontend/RPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
48 changes: 36 additions & 12 deletions src/Sender/Frontend/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public function version(): Version
}

#[RegexpRoute(Method::Delete, '#^api/event/(?<uuid>[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);
Expand All @@ -44,9 +46,11 @@ public function eventDelete(string $uuid): Success
}

#[RegexpRoute(Method::Get, '#^api/event/(?<uuid>[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);
Expand All @@ -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');
Expand Down
Loading