'use server'; import { db } from '@/lib/db'; import { revalidatePath } from 'next/cache'; export async function getCheatsheets() { try { const cheatsheets = await db.cheatsheet.findMany({ include: { items: true, }, orderBy: { createdAt: 'desc', }, }); return cheatsheets; } catch (error) { console.error('Error fetching cheatsheets:', error); return []; } } export async function createCheatsheet(data: { title: string; category: string; description: string; items: { command: string; description: string }[]; }) { try { const slug = data.title .toLowerCase() .trim() .replace(/[^\w\s-]/g, '') .replace(/[\s_-]+/g, '-') + '-' + Date.now().toString().slice(-4); const newCheatsheet = await db.cheatsheet.create({ data: { title: data.title, slug, category: data.category, description: data.description, tags: [data.category, 'Cheatsheet', 'Commands'], items: { create: data.items.map((item) => ({ command: item.command, description: item.description, })), }, }, }); revalidatePath('/[locale]'); revalidatePath('/[locale]/cheatsheets'); revalidatePath('/[locale]/admin'); return { success: true, cheatsheet: newCheatsheet }; } catch (error: any) { console.error('Error creating cheatsheet:', error); return { success: false, error: error.message || 'Failed to create cheatsheet' }; } } export async function updateCheatsheet(id: string, data: { title: string; category: string; description: string; items: { command: string; description: string }[]; }) { try { // Delete existing items and recreate await db.cheatsheetItem.deleteMany({ where: { cheatsheetId: id } }); const updated = await db.cheatsheet.update({ where: { id }, data: { title: data.title, category: data.category, description: data.description, lastUpdated: new Date(), items: { create: data.items.map((item) => ({ command: item.command, description: item.description, })), }, }, }); revalidatePath('/[locale]'); revalidatePath('/[locale]/cheatsheets'); revalidatePath('/[locale]/admin'); return { success: true, cheatsheet: updated }; } catch (error: any) { console.error('Error updating cheatsheet:', error); return { success: false, error: error.message || 'Failed to update cheatsheet' }; } } export async function deleteCheatsheet(id: string) { try { await db.cheatsheet.delete({ where: { id }, }); revalidatePath('/[locale]'); revalidatePath('/[locale]/rehberler'); revalidatePath('/[locale]/admin'); return { success: true }; } catch (error: any) { console.error('Error deleting cheatsheet:', error); return { success: false, error: error.message || 'Failed to delete cheatsheet' }; } }