You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

63 lines
2.1 KiB

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