diff --git a/src/app/api/cards/[id]/route.ts b/src/app/api/cards/[id]/route.ts new file mode 100644 index 0000000..c4a9a65 --- /dev/null +++ b/src/app/api/cards/[id]/route.ts @@ -0,0 +1,34 @@ +import { NextResponse, type NextRequest } from 'next/server' +import { updateCardSchema } from '@/domain/card-schemas' +import { handleError, parseJsonBody } from '@/lib/http' +import { getCardService } from '@/services/card-service-factory' + +type RouteContext = { params: Promise<{ id: string }> } + +export async function GET(_request: NextRequest, context: RouteContext) { + try { + const card = await getCardService().get((await context.params).id) + return NextResponse.json(card) + } catch (error) { + return handleError(error) + } +} + +export async function PUT(request: NextRequest, context: RouteContext) { + try { + const body = updateCardSchema.parse(await parseJsonBody(request)) + const card = await getCardService().update((await context.params).id, body) + return NextResponse.json(card) + } catch (error) { + return handleError(error) + } +} + +export async function DELETE(_request: NextRequest, context: RouteContext) { + try { + await getCardService().remove((await context.params).id) + return new NextResponse(null, { status: 204 }) + } catch (error) { + return handleError(error) + } +} \ No newline at end of file diff --git a/src/app/api/cards/route.ts b/src/app/api/cards/route.ts new file mode 100644 index 0000000..bfcd33f --- /dev/null +++ b/src/app/api/cards/route.ts @@ -0,0 +1,23 @@ +import { NextResponse, type NextRequest } from 'next/server' +import { createCardSchema } from '@/domain/card-schemas' +import { handleError, parseJsonBody } from '@/lib/http' +import { getCardService } from '@/services/card-service-factory' + +export async function GET() { + try { + const cards = await getCardService().list() + return NextResponse.json(cards) + } catch (error) { + return handleError(error) + } +} + +export async function POST(request: NextRequest) { + try { + const body = createCardSchema.parse(await parseJsonBody(request)) + const card = await getCardService().create(body) + return NextResponse.json(card, { status: 201 }) + } catch (error) { + return handleError(error) + } +} \ No newline at end of file