tasko/app/api/cards/[cardId]/route.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-03-21 21:15:27 +00:00
import { auth } from '@clerk/nextjs/server';
2024-02-16 01:49:19 +00:00
import { NextResponse } from 'next/server';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { db } from '@/lib/db';
import { unstable_cache } from 'next/cache';
2024-02-15 02:30:10 +00:00
2024-08-22 18:43:08 +00:00
export async function GET(
req: Request,
props: { params: Promise<{ cardId: string }> }
2024-08-22 18:43:08 +00:00
) {
const params = await props.params;
2024-02-15 02:30:10 +00:00
try {
const { orgId, userId } = await auth();
2024-02-15 02:30:10 +00:00
if (!orgId || !userId)
2024-02-16 01:49:19 +00:00
return new NextResponse(JSON.stringify({ error: 'Unauthorized' }), {
2024-02-15 02:30:10 +00:00
status: 401,
});
const getCard = unstable_cache(
async () => {
return await db.card.findUnique({
where: {
id: params.cardId,
list: {
board: {
orgId,
},
},
2024-02-15 02:30:10 +00:00
},
include: {
list: {
select: {
title: true,
},
},
2024-02-15 02:30:10 +00:00
},
});
},
[`card-${params.cardId}`],
{
tags: [`card-${params.cardId}`],
revalidate: false,
}
);
const card = await getCard();
2024-02-15 02:30:10 +00:00
return new NextResponse(JSON.stringify(card), {
status: 200,
});
2024-02-15 02:30:10 +00:00
} catch (error) {
return new NextResponse(JSON.stringify(error), { status: 500 });
}
}