This commit is contained in:
+16
-5
@@ -72,12 +72,21 @@ class Daemon
|
||||
$collectionName = array_shift($paths);
|
||||
$collectionOp = array_shift($paths);
|
||||
|
||||
try {
|
||||
return $this->handleRequest($collectionName, $collectionOp, $request);
|
||||
}
|
||||
catch (\Throwable $t) {
|
||||
return Response::plaintext($t)->withStatus(500);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleRequest(?string $collectionName, ?string $collectionOp, ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
if ($collectionName == null) {
|
||||
$collections = $this->db->getCollectionNames();
|
||||
return Response::json($collections);
|
||||
}
|
||||
|
||||
|
||||
switch ($collectionOp) {
|
||||
|
||||
case null:
|
||||
@@ -89,8 +98,7 @@ class Daemon
|
||||
}
|
||||
return Response::json($result);
|
||||
} elseif ($request->getMethod() == 'POST') {
|
||||
$this->opCollectionSet($request, $collectionName);
|
||||
return Response::json(true);
|
||||
return $this->opCollectionSet($request, $collectionName);
|
||||
}
|
||||
return Response::json([ 'error' => "Invalid request method" ]);
|
||||
|
||||
@@ -182,7 +190,7 @@ class Daemon
|
||||
}
|
||||
}
|
||||
|
||||
private function opCollectionSet(ServerRequestInterface $request, string $collectionName): void
|
||||
private function opCollectionSet(ServerRequestInterface $request, string $collectionName): ResponseInterface
|
||||
{
|
||||
|
||||
$collection = $this->db->getOrCreateCollection($collectionName);
|
||||
@@ -197,7 +205,7 @@ class Daemon
|
||||
foreach ((array)$body as $change) {
|
||||
|
||||
if (!(isset($change->key) && isset($change->value))) {
|
||||
throw new \Exception("Missing key or value on update item");
|
||||
return Response::json([ 'error' => "Missing key or value on update item" ])->withStatus(500);
|
||||
}
|
||||
// echo "Applying change (collection={$collectionName}): ".json_encode($change)."\n";
|
||||
$id = $change->id??null;
|
||||
@@ -207,7 +215,10 @@ class Daemon
|
||||
$validUntil = isset($change->validity->until) ? new DateTime($change->validity->until) : null;
|
||||
|
||||
$collection->setValue($name, $value, $validFrom, $validUntil, $id);
|
||||
|
||||
}
|
||||
|
||||
return Response::json(true);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user