Add category deletion feature
This commit is contained in:
@ -1633,6 +1633,97 @@ export async function updateEpisodeCategory(
|
||||
}
|
||||
}
|
||||
|
||||
// Category cleanup functions
|
||||
export async function deleteFeedCategory(category: string): Promise<number> {
|
||||
try {
|
||||
const stmt = db.prepare("UPDATE feeds SET category = NULL WHERE category = ?");
|
||||
const result = stmt.run(category);
|
||||
return result.changes;
|
||||
} catch (error) {
|
||||
console.error("Error deleting feed category:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteEpisodeCategory(category: string): Promise<number> {
|
||||
try {
|
||||
const stmt = db.prepare("UPDATE episodes SET category = NULL WHERE category = ?");
|
||||
const result = stmt.run(category);
|
||||
return result.changes;
|
||||
} catch (error) {
|
||||
console.error("Error deleting episode category:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteCategoryFromBoth(category: string): Promise<{feedChanges: number, episodeChanges: number}> {
|
||||
try {
|
||||
db.exec("BEGIN TRANSACTION");
|
||||
|
||||
const feedChanges = await deleteFeedCategory(category);
|
||||
const episodeChanges = await deleteEpisodeCategory(category);
|
||||
|
||||
db.exec("COMMIT");
|
||||
|
||||
return { feedChanges, episodeChanges };
|
||||
} catch (error) {
|
||||
db.exec("ROLLBACK");
|
||||
console.error("Error deleting category from both tables:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllUsedCategories(): Promise<{feedCategories: string[], episodeCategories: string[], allCategories: string[]}> {
|
||||
try {
|
||||
// Get feed categories
|
||||
const feedCatStmt = db.prepare(
|
||||
"SELECT DISTINCT category FROM feeds WHERE category IS NOT NULL AND category != '' ORDER BY category"
|
||||
);
|
||||
const feedCatRows = feedCatStmt.all() as any[];
|
||||
const feedCategories = feedCatRows.map(row => row.category);
|
||||
|
||||
// Get episode categories
|
||||
const episodeCatStmt = db.prepare(
|
||||
"SELECT DISTINCT category FROM episodes WHERE category IS NOT NULL AND category != '' ORDER BY category"
|
||||
);
|
||||
const episodeCatRows = episodeCatStmt.all() as any[];
|
||||
const episodeCategories = episodeCatRows.map(row => row.category);
|
||||
|
||||
// Get all unique categories
|
||||
const allCategoriesSet = new Set([...feedCategories, ...episodeCategories]);
|
||||
const allCategories = Array.from(allCategoriesSet).sort();
|
||||
|
||||
return {
|
||||
feedCategories,
|
||||
episodeCategories,
|
||||
allCategories
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error getting all used categories:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCategoryCounts(category: string): Promise<{feedCount: number, episodeCount: number}> {
|
||||
try {
|
||||
// Count feeds with this category
|
||||
const feedCountStmt = db.prepare("SELECT COUNT(*) as count FROM feeds WHERE category = ?");
|
||||
const feedCountResult = feedCountStmt.get(category) as { count: number };
|
||||
|
||||
// Count episodes with this category
|
||||
const episodeCountStmt = db.prepare("SELECT COUNT(*) as count FROM episodes WHERE category = ?");
|
||||
const episodeCountResult = episodeCountStmt.get(category) as { count: number };
|
||||
|
||||
return {
|
||||
feedCount: feedCountResult.count,
|
||||
episodeCount: episodeCountResult.count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error getting category counts:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Migration function to classify existing episodes without categories
|
||||
export async function migrateEpisodesWithCategories(): Promise<void> {
|
||||
try {
|
||||
|
Reference in New Issue
Block a user