Add category deletion feature
This commit is contained in:
@ -14,6 +14,11 @@ import {
|
||||
fetchEpisodesWithArticles,
|
||||
getAllCategories,
|
||||
getAllFeedsIncludingInactive,
|
||||
getAllUsedCategories,
|
||||
getCategoryCounts,
|
||||
deleteCategoryFromBoth,
|
||||
deleteFeedCategory,
|
||||
deleteEpisodeCategory,
|
||||
getFeedByUrl,
|
||||
getFeedRequests,
|
||||
getFeedsByCategory,
|
||||
@ -345,6 +350,81 @@ app.delete("/api/admin/episodes/:id", async (c) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Category management API endpoints
|
||||
app.get("/api/admin/categories/all", async (c) => {
|
||||
try {
|
||||
const categories = await getAllUsedCategories();
|
||||
return c.json(categories);
|
||||
} catch (error) {
|
||||
console.error("Error fetching all used categories:", error);
|
||||
return c.json({ error: "Failed to fetch categories" }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/api/admin/categories/:category/counts", async (c) => {
|
||||
try {
|
||||
const category = decodeURIComponent(c.req.param("category"));
|
||||
const counts = await getCategoryCounts(category);
|
||||
return c.json(counts);
|
||||
} catch (error) {
|
||||
console.error("Error fetching category counts:", error);
|
||||
return c.json({ error: "Failed to fetch category counts" }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
app.delete("/api/admin/categories/:category", async (c) => {
|
||||
try {
|
||||
const category = decodeURIComponent(c.req.param("category"));
|
||||
const { target } = await c.req.json<{ target: "feeds" | "episodes" | "both" }>();
|
||||
|
||||
if (!category || category.trim() === "") {
|
||||
return c.json({ error: "Category name is required" }, 400);
|
||||
}
|
||||
|
||||
if (!target || !["feeds", "episodes", "both"].includes(target)) {
|
||||
return c.json({ error: "Valid target (feeds, episodes, or both) is required" }, 400);
|
||||
}
|
||||
|
||||
console.log(`🗑️ Admin deleting category "${category}" from ${target}`);
|
||||
|
||||
let result;
|
||||
if (target === "both") {
|
||||
result = await deleteCategoryFromBoth(category);
|
||||
return c.json({
|
||||
result: "DELETED",
|
||||
message: `Category "${category}" deleted from feeds and episodes`,
|
||||
category,
|
||||
feedChanges: result.feedChanges,
|
||||
episodeChanges: result.episodeChanges,
|
||||
totalChanges: result.feedChanges + result.episodeChanges
|
||||
});
|
||||
} else if (target === "feeds") {
|
||||
const changes = await deleteFeedCategory(category);
|
||||
return c.json({
|
||||
result: "DELETED",
|
||||
message: `Category "${category}" deleted from feeds`,
|
||||
category,
|
||||
feedChanges: changes,
|
||||
episodeChanges: 0,
|
||||
totalChanges: changes
|
||||
});
|
||||
} else if (target === "episodes") {
|
||||
const changes = await deleteEpisodeCategory(category);
|
||||
return c.json({
|
||||
result: "DELETED",
|
||||
message: `Category "${category}" deleted from episodes`,
|
||||
category,
|
||||
feedChanges: 0,
|
||||
episodeChanges: changes,
|
||||
totalChanges: changes
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error deleting category:", error);
|
||||
return c.json({ error: "Failed to delete category" }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Database diagnostic endpoint
|
||||
app.get("/api/admin/db-diagnostic", async (c) => {
|
||||
try {
|
||||
|
Reference in New Issue
Block a user