- Next.js SPA mit Bun Runtime - Prisma mit SQLite Datenbank - Vollständige CRUD-Operationen für Fahrräder, Verschleißteile und Wartungshistorie - Warnsystem für bevorstehende Wartungen - Statistik-Features (Gesamtkosten, durchschnittliche Lebensdauer) - Zod-Validierung für alle API-Requests - Umfassende Test-Suite (41 Tests)
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { maintenanceHistorySchema } from '@/lib/validations'
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const history = await prisma.maintenanceHistory.findMany({
|
|
where: { wearPartId: params.id },
|
|
orderBy: {
|
|
date: 'desc',
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(history)
|
|
} catch (error) {
|
|
console.error('Error fetching maintenance history:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Fehler beim Laden der Wartungshistorie' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const body = await request.json()
|
|
const validatedData = maintenanceHistorySchema.parse({
|
|
...body,
|
|
wearPartId: params.id,
|
|
})
|
|
|
|
const history = await prisma.maintenanceHistory.create({
|
|
data: {
|
|
wearPartId: params.id,
|
|
date: new Date(validatedData.date),
|
|
mileage: validatedData.mileage,
|
|
action: validatedData.action,
|
|
notes: validatedData.notes,
|
|
cost: validatedData.cost,
|
|
},
|
|
})
|
|
|
|
// Wenn die Aktion REPLACE ist, setze den Status des Verschleißteils auf REPLACED
|
|
if (validatedData.action === 'REPLACE') {
|
|
await prisma.wearPart.update({
|
|
where: { id: params.id },
|
|
data: { status: 'REPLACED' },
|
|
})
|
|
}
|
|
|
|
return NextResponse.json(history, { status: 201 })
|
|
} catch (error) {
|
|
console.error('Error creating maintenance history:', error)
|
|
if (error instanceof Error && error.name === 'ZodError') {
|
|
return NextResponse.json(
|
|
{ error: 'Ungültige Daten', details: error },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
return NextResponse.json(
|
|
{ error: 'Fehler beim Erstellen des Wartungseintrags' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|