Init
This commit is contained in:
commit
60ca58f5ba
80 changed files with 9458 additions and 0 deletions
58
web/api/snippets.php
Normal file
58
web/api/snippets.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
header('Content-Type: application/json');
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$input = json_decode(file_get_contents('php://input'), true) ?? [];
|
||||
|
||||
if ($method === 'GET') {
|
||||
$pid = (int)($_GET['project_id'] ?? 0);
|
||||
$stmt = $db->prepare('SELECT * FROM snippets WHERE project_id = ? ORDER BY name');
|
||||
$stmt->execute([$pid]);
|
||||
echo json_encode(['snippets' => $stmt->fetchAll()]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($method === 'POST') {
|
||||
$action = $input['action'] ?? 'create';
|
||||
|
||||
if ($action === 'create') {
|
||||
$pid = (int)($input['project_id'] ?? 0);
|
||||
$name = trim($input['name'] ?? '');
|
||||
$content = $input['content'] ?? '';
|
||||
if (!$pid || !$name) { echo json_encode(['error' => 'project_id and name required']); exit; }
|
||||
$stmt = $db->prepare('INSERT INTO snippets (project_id, name, content) VALUES (?, ?, ?)');
|
||||
$stmt->execute([$pid, $name, $content]);
|
||||
Audit::log($db, 'snippet_create', $pid, $name);
|
||||
echo json_encode(['ok' => true, 'id' => (int)$db->lastInsertId()]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($action === 'update') {
|
||||
$id = (int)($input['id'] ?? 0);
|
||||
$name = trim($input['name'] ?? '');
|
||||
$content = $input['content'] ?? '';
|
||||
if (!$id || !$name) { echo json_encode(['error' => 'id and name required']); exit; }
|
||||
$row = $db->prepare('SELECT project_id FROM snippets WHERE id = ?');
|
||||
$row->execute([$id]);
|
||||
$pid = (int)$row->fetchColumn();
|
||||
$db->prepare('UPDATE snippets SET name = ?, content = ? WHERE id = ?')
|
||||
->execute([$name, $content, $id]);
|
||||
Audit::log($db, 'snippet_update', $pid ?: null, $name);
|
||||
echo json_encode(['ok' => true]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($action === 'delete') {
|
||||
$id = (int)($input['id'] ?? 0);
|
||||
if (!$id) { echo json_encode(['error' => 'id required']); exit; }
|
||||
$row = $db->prepare('SELECT project_id, name FROM snippets WHERE id = ?');
|
||||
$row->execute([$id]);
|
||||
$r = $row->fetch();
|
||||
$db->prepare('DELETE FROM snippets WHERE id = ?')->execute([$id]);
|
||||
Audit::log($db, 'snippet_delete', $r ? (int)$r['project_id'] : null, $r['name'] ?? null);
|
||||
echo json_encode(['ok' => true]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
Loading…
Add table
Add a link
Reference in a new issue