Files
2026-07-23 16:55:44 +03:00

129 lines
3.5 KiB
TypeScript

'use server';
import { db } from '@/lib/db';
import { revalidatePath } from 'next/cache';
const INITIAL_CATEGORIES = [
'Next.js',
'React',
'Python & AI',
'Tailwind CSS',
'Docker & DevOps',
];
export async function getCategories() {
try {
if (!db || !(db as any).category) {
console.error('db.category is not initialized yet');
return INITIAL_CATEGORIES.map((name) => ({ id: name, name, slug: name.toLowerCase() }));
}
let categories = await db.category.findMany({
orderBy: { name: 'asc' },
});
if (categories.length === 0) {
// Seed default categories if database has none
for (const name of INITIAL_CATEGORIES) {
const slug = name
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-');
await db.category.upsert({
where: { slug },
update: { name },
create: { name, slug },
}).catch(err => console.error('Failed seeding category:', name, err));
}
categories = await db.category.findMany({
orderBy: { name: 'asc' },
});
}
return categories;
} catch (error) {
console.error('Error fetching categories:', error);
return INITIAL_CATEGORIES.map((name) => ({ id: name, name, slug: name.toLowerCase() }));
}
}
export async function createCategory(name: string) {
try {
const trimmed = name.trim();
if (!trimmed) return { success: false, error: 'Category name cannot be empty' };
const slug = trimmed
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-') + '-' + Date.now().toString().slice(-4);
const newCategory = await db.category.create({
data: {
name: trimmed,
slug,
},
});
revalidatePath('/[locale]');
revalidatePath('/[locale]/lessons');
revalidatePath('/[locale]/admin');
return { success: true, category: newCategory };
} catch (error: any) {
console.error('Error creating category:', error);
return { success: false, error: error.message || 'Failed to create category' };
}
}
export async function updateCategory(id: string, name: string) {
try {
const trimmed = name.trim();
if (!trimmed) return { success: false, error: 'Category name cannot be empty' };
const slug = trimmed
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-') + '-' + Date.now().toString().slice(-4);
const updatedCategory = await db.category.updateMany({
where: {
OR: [{ id }, { name: id }, { slug: id }],
},
data: {
name: trimmed,
slug,
},
});
revalidatePath('/[locale]');
revalidatePath('/[locale]/lessons');
revalidatePath('/[locale]/admin');
return { success: true, count: updatedCategory.count };
} catch (error: any) {
console.error('Error updating category:', error);
return { success: false, error: error.message || 'Failed to update category' };
}
}
export async function deleteCategory(id: string) {
try {
if (!db || !(db as any).category) {
return { success: false, error: 'Category database table is not ready' };
}
await db.category.deleteMany({
where: {
OR: [{ id }, { name: id }, { slug: id }],
},
});
revalidatePath('/[locale]');
revalidatePath('/[locale]/dersler');
revalidatePath('/[locale]/admin');
return { success: true };
} catch (error: any) {
console.error('Error deleting category:', error);
return { success: false, error: error.message || 'Failed to delete category' };
}
}