Files
gulermak_metro/app/api/cameras/route.ts
Şahan Hasret 76c31274d5 Database
2025-11-21 17:46:30 +03:00

104 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { withAuth } from '@/lib/auth';
// GET - Tüm kameraları getir
export async function GET() {
try {
const cameras = await prisma.camera.findMany({
orderBy: { order: 'asc' },
});
return NextResponse.json(cameras);
} catch (error) {
console.error('Cameras fetch error:', error);
return NextResponse.json(
{ error: 'Kameralar alınırken hata oluştu' },
{ status: 500 }
);
}
}
// POST - Yeni kamera ekle (Auth gerekli)
export async function POST(request: NextRequest) {
return withAuth(request, async () => {
try {
const data = await request.json();
const camera = await prisma.camera.create({
data: {
name: data.name,
location: data.location,
videoUrl: data.videoUrl,
status: data.status,
viewers: data.viewers || 0,
order: data.order,
},
});
return NextResponse.json(camera, { status: 201 });
} catch (error) {
console.error('Camera create error:', error);
return NextResponse.json(
{ error: 'Kamera oluşturulurken hata oluştu' },
{ status: 500 }
);
}
});
}
// PUT - Kamera güncelle (Auth gerekli)
export async function PUT(request: NextRequest) {
return withAuth(request, async () => {
try {
const data = await request.json();
const camera = await prisma.camera.update({
where: { id: data.id },
data: {
name: data.name,
location: data.location,
videoUrl: data.videoUrl,
status: data.status,
viewers: data.viewers,
order: data.order,
},
});
return NextResponse.json(camera);
} catch (error) {
console.error('Camera update error:', error);
return NextResponse.json(
{ error: 'Kamera güncellenirken hata oluştu' },
{ status: 500 }
);
}
});
}
// DELETE - Kamera sil (Auth gerekli)
export async function DELETE(request: NextRequest) {
return withAuth(request, async () => {
try {
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');
if (!id) {
return NextResponse.json({ error: 'ID gerekli' }, { status: 400 });
}
await prisma.camera.delete({
where: { id: parseInt(id) },
});
return NextResponse.json({ message: 'Kamera silindi' });
} catch (error) {
console.error('Camera delete error:', error);
return NextResponse.json(
{ error: 'Kamera silinirken hata oluştu' },
{ status: 500 }
);
}
});
}