95 lines
2.2 KiB
TypeScript
Raw Permalink 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 { NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '../auth/[...nextauth]/route'
import { prisma } from '@/lib/prisma'
// Geçmiş kayıtlarını getir
export async function GET() {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.email) {
return NextResponse.json(
{ error: 'Oturum bulunamadı' },
{ status: 401 }
)
}
const user = await prisma.user.findUnique({
where: { email: session.user.email }
})
if (!user) {
return NextResponse.json(
{ error: 'Kullanıcı bulunamadı' },
{ status: 401 }
)
}
const history = await prisma.history.findMany({
where: { userId: user.id },
orderBy: { createdAt: 'desc' },
include: {
route: true,
address: true
}
})
return NextResponse.json(history)
} catch (error) {
console.error('Geçmiş kayıtları alınırken hata oluştu:', error)
return NextResponse.json(
{ error: 'Geçmiş kayıtları alınırken bir hata oluştu' },
{ status: 500 }
)
}
}
// Yeni geçmiş kaydı oluştur
export async function POST(req: Request) {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.email) {
return NextResponse.json(
{ error: 'Oturum bulunamadı' },
{ status: 401 }
)
}
const user = await prisma.user.findUnique({
where: { email: session.user.email }
})
if (!user) {
return NextResponse.json(
{ error: 'Kullanıcı bulunamadı' },
{ status: 401 }
)
}
const { routeId, addressId, action } = await req.json()
const history = await prisma.history.create({
data: {
userId: user.id,
routeId,
addressId,
action,
createdAt: new Date()
},
include: {
route: true,
address: true
}
})
return NextResponse.json(history, { status: 201 })
} catch (error) {
console.error('Geçmiş kaydı oluşturulurken hata oluştu:', error)
return NextResponse.json(
{ error: 'Geçmiş kaydı oluşturulurken bir hata oluştu' },
{ status: 500 }
)
}
}