mirror of
https://github.com/notrab/dumbo.git
synced 2025-01-29 03:37:38 +01:00
update example to use array of users
This commit is contained in:
parent
101c05d02b
commit
4f49187515
50
README.md
50
README.md
@ -21,9 +21,11 @@ $app = new Dumbo();
|
||||
$user = new Dumbo();
|
||||
|
||||
$userData = [
|
||||
"id" => 1,
|
||||
"name" => "Jamie Barton",
|
||||
"email" => "jamie@notrab.dev",
|
||||
[
|
||||
"id" => 1,
|
||||
"name" => "Jamie Barton",
|
||||
"email" => "jamie@notrab.dev",
|
||||
],
|
||||
];
|
||||
|
||||
$user->get("/", function ($c) use ($userData) {
|
||||
@ -33,17 +35,39 @@ $user->get("/", function ($c) use ($userData) {
|
||||
$user->get("/:id", function ($c) use ($userData) {
|
||||
$id = (int) $c->req->param("id");
|
||||
|
||||
$user =
|
||||
array_values(array_filter($userData, fn($u) => $u["id"] === $id))[0] ??
|
||||
null;
|
||||
|
||||
if (!$user) {
|
||||
return $c->json(["error" => "User not found"], 404);
|
||||
}
|
||||
|
||||
return $c->json($user);
|
||||
});
|
||||
|
||||
$user->post("/", function ($c) use ($userData) {
|
||||
$body = $c->req->body();
|
||||
|
||||
if (!isset($body["name"]) || !isset($body["email"])) {
|
||||
return $c->json(["error" => "Name and email are required"], 400);
|
||||
}
|
||||
|
||||
$newId = max(array_column($userData, "id")) + 1;
|
||||
|
||||
$newUserData = array_merge(["id" => $newId], $body);
|
||||
|
||||
return $c->json($newUserData, 201);
|
||||
});
|
||||
|
||||
$user->delete("/:id", function ($c) {
|
||||
$id = (int) $c->req->param("id");
|
||||
|
||||
if ($id !== $userData["id"]) {
|
||||
return $c->json(["error" => "User not found"], 404);
|
||||
}
|
||||
|
||||
return $c->json($userData);
|
||||
});
|
||||
|
||||
$app->get("/", function ($c) {
|
||||
return $c->html("<h1>Hello from Dumbo!</h1>", 200, [
|
||||
"X-Hello" => "World",
|
||||
]);
|
||||
return $c->json(["message" => "User deleted successfully"]);
|
||||
});
|
||||
|
||||
$app->get("/greet/:greeting", function ($c) {
|
||||
@ -63,5 +87,11 @@ $app->use(function ($c, $next) {
|
||||
return $next($c);
|
||||
});
|
||||
|
||||
$app->get("/", function ($c) {
|
||||
return $c->html("<h1>Hello from Dumbo!</h1>", 200, [
|
||||
"X-Hello" => "World",
|
||||
]);
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
|
@ -8,9 +8,11 @@ $app = new Dumbo();
|
||||
$user = new Dumbo();
|
||||
|
||||
$userData = [
|
||||
"id" => 1,
|
||||
"name" => "Jamie Barton",
|
||||
"email" => "jamie@notrab.dev",
|
||||
[
|
||||
"id" => 1,
|
||||
"name" => "Jamie Barton",
|
||||
"email" => "jamie@notrab.dev",
|
||||
],
|
||||
];
|
||||
|
||||
$user->get("/", function ($c) use ($userData) {
|
||||
@ -20,17 +22,39 @@ $user->get("/", function ($c) use ($userData) {
|
||||
$user->get("/:id", function ($c) use ($userData) {
|
||||
$id = (int) $c->req->param("id");
|
||||
|
||||
$user =
|
||||
array_values(array_filter($userData, fn($u) => $u["id"] === $id))[0] ??
|
||||
null;
|
||||
|
||||
if (!$user) {
|
||||
return $c->json(["error" => "User not found"], 404);
|
||||
}
|
||||
|
||||
return $c->json($user);
|
||||
});
|
||||
|
||||
$user->post("/", function ($c) use ($userData) {
|
||||
$body = $c->req->body();
|
||||
|
||||
if (!isset($body["name"]) || !isset($body["email"])) {
|
||||
return $c->json(["error" => "Name and email are required"], 400);
|
||||
}
|
||||
|
||||
$newId = max(array_column($userData, "id")) + 1;
|
||||
|
||||
$newUserData = array_merge(["id" => $newId], $body);
|
||||
|
||||
return $c->json($newUserData, 201);
|
||||
});
|
||||
|
||||
$user->delete("/:id", function ($c) {
|
||||
$id = (int) $c->req->param("id");
|
||||
|
||||
if ($id !== $userData["id"]) {
|
||||
return $c->json(["error" => "User not found"], 404);
|
||||
}
|
||||
|
||||
return $c->json($userData);
|
||||
});
|
||||
|
||||
$app->get("/", function ($c) {
|
||||
return $c->html("<h1>Hello from Dumbo!</h1>", 200, [
|
||||
"X-Hello" => "World",
|
||||
]);
|
||||
return $c->json(["message" => "User deleted successfully"]);
|
||||
});
|
||||
|
||||
$app->get("/greet/:greeting", function ($c) {
|
||||
@ -50,4 +74,10 @@ $app->use(function ($c, $next) {
|
||||
return $next($c);
|
||||
});
|
||||
|
||||
$app->get("/", function ($c) {
|
||||
return $c->html("<h1>Hello from Dumbo!</h1>", 200, [
|
||||
"X-Hello" => "World",
|
||||
]);
|
||||
});
|
||||
|
||||
$app->run();
|
||||
|
Loading…
x
Reference in New Issue
Block a user