42 lines
1.4 KiB
PHP
42 lines
1.4 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$db = db();
|
|
|
|
switch ($method) {
|
|
case 'GET':
|
|
if (isset($_GET['id'])) {
|
|
$stmt = $db->prepare("SELECT * FROM notes WHERE id = ?");
|
|
$stmt->execute([$_GET['id']]);
|
|
echo json_encode($stmt->fetch());
|
|
} else {
|
|
$stmt = $db->query("SELECT * FROM notes ORDER BY created_at DESC");
|
|
echo json_encode($stmt->fetchAll());
|
|
}
|
|
break;
|
|
|
|
case 'POST':
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
if (isset($data['id']) && $data['id']) {
|
|
// Update
|
|
$stmt = $db->prepare("UPDATE notes SET title = ?, content = ? WHERE id = ?");
|
|
$stmt->execute([$data['title'], $data['content'], $data['id']]);
|
|
echo json_encode(['success' => true]);
|
|
} else {
|
|
// Create
|
|
$stmt = $db->prepare("INSERT INTO notes (title, content) VALUES (?, ?)");
|
|
$stmt->execute([$data['title'], $data['content']]);
|
|
echo json_encode(['success' => true, 'id' => $db->lastInsertId()]);
|
|
}
|
|
break;
|
|
|
|
case 'DELETE':
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $db->prepare("DELETE FROM notes WHERE id = ?");
|
|
$stmt->execute([$data['id']]);
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
}
|