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

103 lines
2.7 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 medya öğelerini getir
export async function GET() {
try {
const media = await prisma.media.findMany({
orderBy: { createdAt: 'desc' },
});
return NextResponse.json(media);
} catch (error) {
console.error('Media fetch error:', error);
return NextResponse.json(
{ error: 'Medya öğeleri alınırken hata oluştu' },
{ status: 500 }
);
}
}
// POST - Yeni medya öğesi ekle (Auth gerekli)
export async function POST(request: NextRequest) {
return withAuth(request, async () => {
try {
const data = await request.json();
const media = await prisma.media.create({
data: {
title: data.title,
type: data.type,
thumbnail: data.thumbnail,
videoUrl: data.videoUrl,
description: data.description,
date: new Date().toISOString().split('T')[0],
},
});
return NextResponse.json(media, { status: 201 });
} catch (error) {
console.error('Media create error:', error);
return NextResponse.json(
{ error: 'Medya öğesi oluşturulurken hata oluştu' },
{ status: 500 }
);
}
});
}
// PUT - Medya öğesi güncelle (Auth gerekli)
export async function PUT(request: NextRequest) {
return withAuth(request, async () => {
try {
const data = await request.json();
const media = await prisma.media.update({
where: { id: data.id },
data: {
title: data.title,
type: data.type,
thumbnail: data.thumbnail,
videoUrl: data.videoUrl,
description: data.description,
},
});
return NextResponse.json(media);
} catch (error) {
console.error('Media update error:', error);
return NextResponse.json(
{ error: 'Medya öğesi güncellenirken hata oluştu' },
{ status: 500 }
);
}
});
}
// DELETE - Medya öğesi 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.media.delete({
where: { id: parseInt(id) },
});
return NextResponse.json({ message: 'Medya öğesi silindi' });
} catch (error) {
console.error('Media delete error:', error);
return NextResponse.json(
{ error: 'Medya öğesi silinirken hata oluştu' },
{ status: 500 }
);
}
});
}