dumbo/examples/turso/index.php

107 lines
2.6 KiB
PHP
Raw Normal View History

2024-08-29 12:17:57 +01:00
<?php
2024-09-08 09:24:05 +01:00
require __DIR__ . "/vendor/autoload.php";
2024-08-29 12:17:57 +01:00
use Libsql\Libsql;
2024-08-29 12:17:57 +01:00
use Dumbo\Dumbo;
$libsql = new Libsql();
$db = $libsql->openLocal("./test.db");
$conn = $db->connect();
2024-08-29 12:17:57 +01:00
$conn->execute("
2024-08-29 12:17:57 +01:00
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
)
");
$app = new Dumbo();
$app->get("/users", function ($context) use ($conn) {
$result = $conn->query("SELECT * FROM users")->fetchArray();
2024-08-29 12:17:57 +01:00
2024-08-29 16:09:38 +01:00
return $context->json($result);
2024-08-29 12:17:57 +01:00
});
$app->get("/users/:id", function ($context) use ($conn) {
$id = intval($context->req->param("id"));
2024-08-29 12:17:57 +01:00
$result = $conn->query("SELECT * FROM users WHERE id = ?", [
$id
])->fetchArray();
2024-08-29 12:17:57 +01:00
if (empty($result)) {
2024-08-29 16:09:38 +01:00
return $context->json(["error" => "User not found"], 404);
2024-08-29 12:17:57 +01:00
}
2024-08-29 16:09:38 +01:00
return $context->json($result[0]);
2024-08-29 12:17:57 +01:00
});
$app->post("/users", function ($context) use ($conn) {
2024-08-29 16:09:38 +01:00
$body = $context->req->body();
2024-08-29 12:17:57 +01:00
if (!isset($body["name"]) || !isset($body["email"])) {
2024-08-29 16:09:38 +01:00
return $context->json(["error" => "Name and email are required"], 400);
2024-08-29 12:17:57 +01:00
}
$result = $conn->query(
"INSERT INTO users (name, email) VALUES (:name, :email) RETURNING id",
[
":name" => $body["name"],
":email" => $body["email"],
])
->fetchArray();
2024-08-29 12:17:57 +01:00
2024-08-29 16:09:38 +01:00
return $context->json(["id" => $result[0]["id"]], 201);
2024-08-29 12:17:57 +01:00
});
$app->put("/users/:id", function ($context) use ($conn) {
$id = intval($context->req->param("id"));
2024-08-29 16:09:38 +01:00
$body = $context->req->body();
2024-08-29 12:17:57 +01:00
if (!isset($body["name"]) && !isset($body["email"])) {
2024-08-29 16:09:38 +01:00
return $context->json(["error" => "Name or email is required"], 400);
2024-08-29 12:17:57 +01:00
}
$setClause = [];
$params = [];
if (isset($body["name"])) {
$setClause[] = "name = ?";
$params[] = $body["name"];
}
if (isset($body["email"])) {
$setClause[] = "email = ?";
$params[] = $body["email"];
}
$params[] = $id;
$result = $conn->query(
"UPDATE users SET " . implode(", ", $setClause) . " WHERE id = ? RETURNING *",
$params
)->fetchArray();
2024-08-29 12:17:57 +01:00
if (empty($result)) {
2024-08-29 16:09:38 +01:00
return $context->json(["error" => "User not found"], 404);
2024-08-29 12:17:57 +01:00
}
2024-08-29 16:09:38 +01:00
return $context->json($result[0]);
2024-08-29 12:17:57 +01:00
});
$app->delete("/users/:id", function ($context) use ($conn) {
$id = intval($context->req->param("id"));
2024-08-29 12:17:57 +01:00
$changed = $conn->execute("DELETE FROM users WHERE id = ?", [$id]);
2024-08-29 12:17:57 +01:00
if ($changed == 0) {
2024-08-29 16:09:38 +01:00
return $context->json(["error" => "User not found"], 404);
2024-08-29 12:17:57 +01:00
}
2024-08-29 16:09:38 +01:00
return $context->json(["message" => "User deleted successfully"]);
2024-08-29 12:17:57 +01:00
});
$app->run();