feat(api): route CRUD cards (liste, détail, création, modification, suppression)
This commit is contained in:
1 parent
e4aa5f4b3b
commit
ae3e87d916
2 files changed
+57
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user