Adds PATCH and DELETE endpoints for brands and shops that mirror the existing bins pattern: deleting a brand or shop nullifies referencing products (and strains, for brands) inside a transaction so nothing is lost. Brand renames return 409 when the new name collides with the UNIQUE constraint, surfaced inline in the edit modal. The Brands and Shops views now show inline edit/trash icons on each card; the trash button confirms with a preview of how many products will be unparented. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,44 @@ catalogRouter.post("/brands", (req, res) => {
|
||||
res.json({ id, name: name.trim() });
|
||||
});
|
||||
|
||||
catalogRouter.patch("/brands/:id", (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { name } = req.body as { name?: string };
|
||||
if (!name?.trim()) return res.status(400).json({ error: "name required" });
|
||||
const existing = db
|
||||
.prepare<[string], { id: string }>("SELECT id FROM brands WHERE id = ?")
|
||||
.get(id);
|
||||
if (!existing) return res.status(404).json({ error: "brand not found" });
|
||||
try {
|
||||
db.prepare("UPDATE brands SET name = ? WHERE id = ?").run(name.trim(), id);
|
||||
res.json({ id, name: name.trim() });
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : "";
|
||||
if (msg.includes("UNIQUE")) {
|
||||
return res.status(409).json({ error: "another brand already uses that name" });
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
// Deleting a brand unparents any products and strains that reference it
|
||||
// (brand_id → NULL), so users never lose products when reorganizing.
|
||||
catalogRouter.delete("/brands/:id", (req, res) => {
|
||||
const { id } = req.params;
|
||||
const tx = db.transaction(() => {
|
||||
db.prepare("UPDATE products SET brand_id = NULL WHERE brand_id = ?").run(id);
|
||||
db.prepare("UPDATE strains SET brand_id = NULL WHERE brand_id = ?").run(id);
|
||||
const result = db.prepare("DELETE FROM brands WHERE id = ?").run(id);
|
||||
if (result.changes === 0) throw new Error("not found");
|
||||
});
|
||||
try {
|
||||
tx();
|
||||
res.json({ ok: true });
|
||||
} catch {
|
||||
res.status(404).json({ error: "brand not found" });
|
||||
}
|
||||
});
|
||||
|
||||
catalogRouter.post("/shops", (req, res) => {
|
||||
const { name, location } = req.body as { name: string; location?: string };
|
||||
if (!name?.trim()) return res.status(400).json({ error: "name required" });
|
||||
@@ -27,6 +65,44 @@ catalogRouter.post("/shops", (req, res) => {
|
||||
res.json({ id, name: name.trim(), location: location?.trim() ?? null });
|
||||
});
|
||||
|
||||
catalogRouter.patch("/shops/:id", (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { name, location } = req.body as { name?: string; location?: string | null };
|
||||
const existing = db
|
||||
.prepare<[string], { id: string; name: string; location: string | null }>(
|
||||
"SELECT id, name, location FROM shops WHERE id = ?",
|
||||
)
|
||||
.get(id);
|
||||
if (!existing) return res.status(404).json({ error: "shop not found" });
|
||||
|
||||
const nextName = name?.trim() ? name.trim() : existing.name;
|
||||
const nextLocation =
|
||||
location === undefined ? existing.location : location?.toString().trim() || null;
|
||||
|
||||
db.prepare("UPDATE shops SET name = ?, location = ? WHERE id = ?").run(
|
||||
nextName,
|
||||
nextLocation,
|
||||
id,
|
||||
);
|
||||
res.json({ id, name: nextName, location: nextLocation });
|
||||
});
|
||||
|
||||
// Deleting a shop unparents any products that reference it (shop_id → NULL).
|
||||
catalogRouter.delete("/shops/:id", (req, res) => {
|
||||
const { id } = req.params;
|
||||
const tx = db.transaction(() => {
|
||||
db.prepare("UPDATE products SET shop_id = NULL WHERE shop_id = ?").run(id);
|
||||
const result = db.prepare("DELETE FROM shops WHERE id = ?").run(id);
|
||||
if (result.changes === 0) throw new Error("not found");
|
||||
});
|
||||
try {
|
||||
tx();
|
||||
res.json({ ok: true });
|
||||
} catch {
|
||||
res.status(404).json({ error: "shop not found" });
|
||||
}
|
||||
});
|
||||
|
||||
catalogRouter.post("/bins", (req, res) => {
|
||||
const { name, location, capacity } = req.body as {
|
||||
name: string;
|
||||
|
||||
Reference in New Issue
Block a user