Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

75 řádky
2.6 KiB

  1. import { NextResponse } from 'next/server';
  2. import { revalidatePath } from 'next/cache';
  3. import { getCards, saveCards } from '@/lib/db';
  4. import { validateCard } from '@/lib/validation';
  5. import { sanitizeCardHtml } from '@/lib/sanitize';
  6. import { Card } from '@/types';
  7. export const dynamic = 'force-dynamic';
  8. export async function GET(request: Request) {
  9. const { searchParams } = new URL(request.url);
  10. const portalId = searchParams.get('portalId');
  11. const cards = await getCards(portalId || undefined);
  12. return NextResponse.json(cards);
  13. }
  14. export async function POST(request: Request) {
  15. try {
  16. const incomingCard: Card = await request.json();
  17. const validation = validateCard(incomingCard);
  18. if (!validation.valid) {
  19. return NextResponse.json({ error: 'Validation failed', errors: validation.errors }, { status: 400 });
  20. }
  21. if (typeof incomingCard.fullContent === 'string') {
  22. incomingCard.fullContent = sanitizeCardHtml(incomingCard.fullContent);
  23. }
  24. const cards = await getCards();
  25. const existingIndex = cards.findIndex(c => c.id === incomingCard.id);
  26. if (existingIndex >= 0) {
  27. cards[existingIndex] = incomingCard;
  28. } else {
  29. cards.push(incomingCard);
  30. }
  31. await saveCards(cards);
  32. revalidatePath('/'); // Force public portal to update instantly
  33. return NextResponse.json(incomingCard, { status: 200 });
  34. } catch {
  35. return NextResponse.json({ error: 'Failed to save card' }, { status: 500 });
  36. }
  37. }
  38. // NEW: Bulk update for saving card reordering
  39. export async function PUT(request: Request) {
  40. try {
  41. const updatedCards: Card[] = await request.json();
  42. await saveCards(updatedCards);
  43. revalidatePath('/'); // Force public portal to update instantly
  44. return NextResponse.json({ success: true }, { status: 200 });
  45. } catch (error) {
  46. return NextResponse.json({ error: 'Failed to reorder cards' }, { status: 500 });
  47. }
  48. }
  49. export async function DELETE(request: Request) {
  50. try {
  51. const { searchParams } = new URL(request.url);
  52. const id = searchParams.get('id');
  53. if (!id) return NextResponse.json({ error: 'Card ID required' }, { status: 400 });
  54. const cards = await getCards();
  55. const filteredCards = cards.filter(c => c.id !== id);
  56. await saveCards(filteredCards);
  57. revalidatePath('/'); // Force public portal to update instantly
  58. return NextResponse.json({ success: true }, { status: 200 });
  59. } catch (error) {
  60. return NextResponse.json({ error: 'Failed to delete card' }, { status: 500 });
  61. }
  62. }