Initial commit: Fahrrad Verschleißteile Tracker
- 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)
This commit is contained in:
87
app/api/bikes/[id]/parts/route.ts
Normal file
87
app/api/bikes/[id]/parts/route.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { wearPartSchema } from '@/lib/validations'
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const parts = await prisma.wearPart.findMany({
|
||||
where: { bikeId: params.id },
|
||||
include: {
|
||||
maintenanceHistory: {
|
||||
orderBy: {
|
||||
date: 'desc',
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json(parts)
|
||||
} catch (error) {
|
||||
console.error('Error fetching parts:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Fehler beim Laden der Verschleißteile' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const validatedData = wearPartSchema.parse({
|
||||
...body,
|
||||
bikeId: params.id,
|
||||
})
|
||||
|
||||
const part = await prisma.wearPart.create({
|
||||
data: {
|
||||
bikeId: params.id,
|
||||
type: validatedData.type,
|
||||
brand: validatedData.brand,
|
||||
model: validatedData.model,
|
||||
installDate: new Date(validatedData.installDate),
|
||||
installMileage: validatedData.installMileage,
|
||||
serviceInterval: validatedData.serviceInterval,
|
||||
status: validatedData.status,
|
||||
cost: validatedData.cost,
|
||||
notes: validatedData.notes,
|
||||
},
|
||||
})
|
||||
|
||||
// Erstelle automatisch einen MaintenanceHistory-Eintrag für die Installation
|
||||
await prisma.maintenanceHistory.create({
|
||||
data: {
|
||||
wearPartId: part.id,
|
||||
date: new Date(validatedData.installDate),
|
||||
mileage: validatedData.installMileage,
|
||||
action: 'INSTALL',
|
||||
notes: validatedData.notes,
|
||||
cost: validatedData.cost,
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json(part, { status: 201 })
|
||||
} catch (error) {
|
||||
console.error('Error creating part:', 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 Verschleißteils' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
118
app/api/bikes/[id]/route.ts
Normal file
118
app/api/bikes/[id]/route.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { bikeSchema } from '@/lib/validations'
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const bike = await prisma.bike.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
wearParts: {
|
||||
include: {
|
||||
maintenanceHistory: {
|
||||
orderBy: {
|
||||
date: 'desc',
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!bike) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Fahrrad nicht gefunden' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json(bike)
|
||||
} catch (error) {
|
||||
console.error('Error fetching bike:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Fehler beim Laden des Fahrrads' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const validatedData = bikeSchema.parse(body)
|
||||
|
||||
const bike = await prisma.bike.update({
|
||||
where: { id: params.id },
|
||||
data: {
|
||||
name: validatedData.name,
|
||||
brand: validatedData.brand,
|
||||
model: validatedData.model,
|
||||
purchaseDate: validatedData.purchaseDate
|
||||
? new Date(validatedData.purchaseDate)
|
||||
: null,
|
||||
notes: validatedData.notes,
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json(bike)
|
||||
} catch (error) {
|
||||
console.error('Error updating bike:', error)
|
||||
if (error instanceof Error && error.name === 'ZodError') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Ungültige Daten', details: error },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.message.includes('Record to update does not exist')
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Fahrrad nicht gefunden' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: 'Fehler beim Aktualisieren des Fahrrads' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
await prisma.bike.delete({
|
||||
where: { id: params.id },
|
||||
})
|
||||
|
||||
return NextResponse.json({ message: 'Fahrrad gelöscht' })
|
||||
} catch (error) {
|
||||
console.error('Error deleting bike:', error)
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.message.includes('Record to delete does not exist')
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Fahrrad nicht gefunden' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: 'Fehler beim Löschen des Fahrrads' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user