full upgrade

This commit is contained in:
2026-01-07 23:13:25 +01:00
parent 4cd3f60c98
commit c5efd28383
23 changed files with 693 additions and 226 deletions

View File

@@ -1,5 +1,7 @@
import { type NextRequest, NextResponse } from "next/server";
import { PrismaClient } from '@prisma/client';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
import { checkRateLimit, getRateLimitHeaders } from '@/lib/auth';
const prisma = new PrismaClient();
@@ -8,6 +10,21 @@ export async function PUT(
{ params }: { params: Promise<{ id: string }> }
) {
try {
// Rate limiting for PUT requests
const ip = request.headers.get('x-forwarded-for') || request.headers.get('x-real-ip') || 'unknown';
if (!checkRateLimit(ip, 5, 60000)) { // 5 requests per minute
return new NextResponse(
JSON.stringify({ error: 'Rate limit exceeded' }),
{
status: 429,
headers: {
'Content-Type': 'application/json',
...getRateLimitHeaders(ip, 5, 60000)
}
}
);
}
const resolvedParams = await params;
const id = parseInt(resolvedParams.id);
const body = await request.json();
@@ -35,7 +52,20 @@ export async function PUT(
});
} catch (error) {
console.error('Error updating contact:', error);
// Handle missing database table gracefully
if (error instanceof PrismaClientKnownRequestError && error.code === 'P2021') {
if (process.env.NODE_ENV === 'development') {
console.warn('Contact table does not exist.');
}
return NextResponse.json(
{ error: 'Database table not found. Please run migrations.' },
{ status: 503 }
);
}
if (process.env.NODE_ENV === 'development') {
console.error('Error updating contact:', error);
}
return NextResponse.json(
{ error: 'Failed to update contact' },
{ status: 500 }
@@ -48,6 +78,21 @@ export async function DELETE(
{ params }: { params: Promise<{ id: string }> }
) {
try {
// Rate limiting for DELETE requests
const ip = request.headers.get('x-forwarded-for') || request.headers.get('x-real-ip') || 'unknown';
if (!checkRateLimit(ip, 3, 60000)) { // 3 requests per minute for DELETE (more restrictive)
return new NextResponse(
JSON.stringify({ error: 'Rate limit exceeded' }),
{
status: 429,
headers: {
'Content-Type': 'application/json',
...getRateLimitHeaders(ip, 3, 60000)
}
}
);
}
const resolvedParams = await params;
const id = parseInt(resolvedParams.id);
@@ -67,7 +112,20 @@ export async function DELETE(
});
} catch (error) {
console.error('Error deleting contact:', error);
// Handle missing database table gracefully
if (error instanceof PrismaClientKnownRequestError && error.code === 'P2021') {
if (process.env.NODE_ENV === 'development') {
console.warn('Contact table does not exist.');
}
return NextResponse.json(
{ error: 'Database table not found. Please run migrations.' },
{ status: 503 }
);
}
if (process.env.NODE_ENV === 'development') {
console.error('Error deleting contact:', error);
}
return NextResponse.json(
{ error: 'Failed to delete contact' },
{ status: 500 }

View File

@@ -1,5 +1,7 @@
import { type NextRequest, NextResponse } from "next/server";
import { PrismaClient } from '@prisma/client';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
import { checkRateLimit, getRateLimitHeaders } from '@/lib/auth';
const prisma = new PrismaClient();
@@ -40,7 +42,21 @@ export async function GET(request: NextRequest) {
});
} catch (error) {
console.error('Error fetching contacts:', error);
// Handle missing database table gracefully
if (error instanceof PrismaClientKnownRequestError && error.code === 'P2021') {
if (process.env.NODE_ENV === 'development') {
console.warn('Contact table does not exist. Returning empty result.');
}
return NextResponse.json({
contacts: [],
total: 0,
hasMore: false
});
}
if (process.env.NODE_ENV === 'development') {
console.error('Error fetching contacts:', error);
}
return NextResponse.json(
{ error: 'Failed to fetch contacts' },
{ status: 500 }
@@ -50,6 +66,21 @@ export async function GET(request: NextRequest) {
export async function POST(request: NextRequest) {
try {
// Rate limiting for POST requests
const ip = request.headers.get('x-forwarded-for') || request.headers.get('x-real-ip') || 'unknown';
if (!checkRateLimit(ip, 5, 60000)) { // 5 requests per minute
return new NextResponse(
JSON.stringify({ error: 'Rate limit exceeded' }),
{
status: 429,
headers: {
'Content-Type': 'application/json',
...getRateLimitHeaders(ip, 5, 60000)
}
}
);
}
const body = await request.json();
const { name, email, subject, message } = body;
@@ -86,7 +117,20 @@ export async function POST(request: NextRequest) {
}, { status: 201 });
} catch (error) {
console.error('Error creating contact:', error);
// Handle missing database table gracefully
if (error instanceof PrismaClientKnownRequestError && error.code === 'P2021') {
if (process.env.NODE_ENV === 'development') {
console.warn('Contact table does not exist.');
}
return NextResponse.json(
{ error: 'Database table not found. Please run migrations.' },
{ status: 503 }
);
}
if (process.env.NODE_ENV === 'development') {
console.error('Error creating contact:', error);
}
return NextResponse.json(
{ error: 'Failed to create contact' },
{ status: 500 }