release: 2026-08-01
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getDb } from "@/lib/db";
|
||||
|
||||
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const db = getDb();
|
||||
const id = req.query.id as string;
|
||||
|
||||
if (req.method === "GET") {
|
||||
const t = db.prepare("SELECT * FROM templates WHERE id = ?").get(id);
|
||||
if (!t) return res.status(404).json({ error: "Not found" });
|
||||
return res.json(t);
|
||||
}
|
||||
|
||||
if (req.method === "PUT") {
|
||||
const { name, body } = req.body;
|
||||
db.prepare(
|
||||
"UPDATE templates SET name = COALESCE(?, name), body = COALESCE(?, body) WHERE id = ?"
|
||||
).run(name, body, id);
|
||||
return res.json(db.prepare("SELECT * FROM templates WHERE id = ?").get(id));
|
||||
}
|
||||
|
||||
if (req.method === "DELETE") {
|
||||
db.prepare("DELETE FROM templates WHERE id = ?").run(id);
|
||||
return res.status(204).end();
|
||||
}
|
||||
|
||||
res.setHeader("Allow", ["GET", "PUT", "DELETE"]);
|
||||
res.status(405).end();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getDb } from "@/lib/db";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const db = getDb();
|
||||
|
||||
if (req.method === "GET") {
|
||||
const templates = db.prepare("SELECT * FROM templates ORDER BY created_at DESC").all();
|
||||
return res.json(templates);
|
||||
}
|
||||
|
||||
if (req.method === "POST") {
|
||||
const { name, body } = req.body;
|
||||
if (!name || !body) return res.status(400).json({ error: "name and body required" });
|
||||
const id = randomUUID();
|
||||
db.prepare("INSERT INTO templates (id, name, body) VALUES (?, ?, ?)").run(id, name, body);
|
||||
return res.status(201).json(db.prepare("SELECT * FROM templates WHERE id = ?").get(id));
|
||||
}
|
||||
|
||||
res.setHeader("Allow", ["GET", "POST"]);
|
||||
res.status(405).end();
|
||||
}
|
||||
Reference in New Issue
Block a user