Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

90 linhas
3.1 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(
  20. { error: 'The card could not be saved because some fields are invalid. See the highlighted errors and fix them, then try again.', errors: validation.errors },
  21. { status: 400 },
  22. );
  23. }
  24. if (typeof incomingCard.fullContent === 'string') {
  25. incomingCard.fullContent = sanitizeCardHtml(incomingCard.fullContent);
  26. }
  27. const cards = await getCards();
  28. const existingIndex = cards.findIndex(c => c.id === incomingCard.id);
  29. if (existingIndex >= 0) {
  30. cards[existingIndex] = incomingCard;
  31. } else {
  32. cards.push(incomingCard);
  33. }
  34. await saveCards(cards);
  35. revalidatePath('/'); // Force public portal to update instantly
  36. return NextResponse.json(incomingCard, { status: 200 });
  37. } catch {
  38. return NextResponse.json(
  39. { error: 'Unexpected error while saving the card. The card was not saved. Check the server logs for details and try again.' },
  40. { status: 500 },
  41. );
  42. }
  43. }
  44. // NEW: Bulk update for saving card reordering
  45. export async function PUT(request: Request) {
  46. try {
  47. const updatedCards: Card[] = await request.json();
  48. await saveCards(updatedCards);
  49. revalidatePath('/'); // Force public portal to update instantly
  50. return NextResponse.json({ success: true }, { status: 200 });
  51. } catch {
  52. return NextResponse.json(
  53. { error: 'Unexpected error while saving the new card order. The order was not saved. Reload the page and try again.' },
  54. { status: 500 },
  55. );
  56. }
  57. }
  58. export async function DELETE(request: Request) {
  59. try {
  60. const { searchParams } = new URL(request.url);
  61. const id = searchParams.get('id');
  62. if (!id) return NextResponse.json(
  63. { error: 'Missing "id" parameter: cannot tell which card to delete. Reload the page and retry.' },
  64. { status: 400 },
  65. );
  66. const cards = await getCards();
  67. const filteredCards = cards.filter(c => c.id !== id);
  68. await saveCards(filteredCards);
  69. revalidatePath('/'); // Force public portal to update instantly
  70. return NextResponse.json({ success: true }, { status: 200 });
  71. } catch {
  72. return NextResponse.json(
  73. { error: 'Unexpected error while deleting the card. The card was not deleted. Check the server logs and try again.' },
  74. { status: 500 },
  75. );
  76. }
  77. }