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

98 lines
2.4 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 SSS'leri getir
export async function GET() {
try {
const faqs = await prisma.fAQ.findMany({
orderBy: { order: 'asc' },
});
return NextResponse.json(faqs);
} catch (error) {
console.error('FAQs fetch error:', error);
return NextResponse.json(
{ error: 'SSS alınırken hata oluştu' },
{ status: 500 }
);
}
}
// POST - Yeni SSS ekle (Auth gerekli)
export async function POST(request: NextRequest) {
return withAuth(request, async () => {
try {
const data = await request.json();
const faq = await prisma.fAQ.create({
data: {
question: data.question,
answer: data.answer,
order: data.order,
},
});
return NextResponse.json(faq, { status: 201 });
} catch (error) {
console.error('FAQ create error:', error);
return NextResponse.json(
{ error: 'SSS oluşturulurken hata oluştu' },
{ status: 500 }
);
}
});
}
// PUT - SSS güncelle (Auth gerekli)
export async function PUT(request: NextRequest) {
return withAuth(request, async () => {
try {
const data = await request.json();
const faq = await prisma.fAQ.update({
where: { id: data.id },
data: {
question: data.question,
answer: data.answer,
order: data.order,
},
});
return NextResponse.json(faq);
} catch (error) {
console.error('FAQ update error:', error);
return NextResponse.json(
{ error: 'SSS güncellenirken hata oluştu' },
{ status: 500 }
);
}
});
}
// DELETE - SSS 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.fAQ.delete({
where: { id: parseInt(id) },
});
return NextResponse.json({ message: 'SSS silindi' });
} catch (error) {
console.error('FAQ delete error:', error);
return NextResponse.json(
{ error: 'SSS silinirken hata oluştu' },
{ status: 500 }
);
}
});
}