huge update
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { apiCache } from '@/lib/cache';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
@@ -35,20 +36,41 @@ export async function PUT(
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
// Check if this is an admin request
|
||||
const isAdminRequest = request.headers.get('x-admin-request') === 'true';
|
||||
if (!isAdminRequest) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Admin access required' },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
const { id: idParam } = await params;
|
||||
const id = parseInt(idParam);
|
||||
const data = await request.json();
|
||||
|
||||
// Remove difficulty field if it exists (since we're removing it)
|
||||
const { difficulty, ...projectData } = data;
|
||||
|
||||
const project = await prisma.project.update({
|
||||
where: { id },
|
||||
data: { ...data, updatedAt: new Date() }
|
||||
data: {
|
||||
...projectData,
|
||||
updatedAt: new Date(),
|
||||
// Keep existing difficulty if not provided
|
||||
...(difficulty ? { difficulty } : {})
|
||||
}
|
||||
});
|
||||
|
||||
// Invalidate cache after successful update
|
||||
await apiCache.invalidateProject(id);
|
||||
await apiCache.invalidateAll();
|
||||
|
||||
return NextResponse.json(project);
|
||||
} catch (error) {
|
||||
console.error('Error updating project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update project' },
|
||||
{ error: 'Failed to update project', details: error instanceof Error ? error.message : 'Unknown error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
@@ -66,6 +88,10 @@ export async function DELETE(
|
||||
where: { id }
|
||||
});
|
||||
|
||||
// Invalidate cache after successful deletion
|
||||
await apiCache.invalidateProject(id);
|
||||
await apiCache.invalidateAll();
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error deleting project:', error);
|
||||
|
||||
@@ -37,8 +37,19 @@ export async function GET(request: NextRequest) {
|
||||
const difficulty = searchParams.get('difficulty');
|
||||
const search = searchParams.get('search');
|
||||
|
||||
// Create cache parameters object
|
||||
const cacheParams = {
|
||||
page: page.toString(),
|
||||
limit: limit.toString(),
|
||||
category,
|
||||
featured,
|
||||
published,
|
||||
difficulty,
|
||||
search
|
||||
};
|
||||
|
||||
// Check cache first
|
||||
const cached = await apiCache.getProjects();
|
||||
const cached = await apiCache.getProjects(cacheParams);
|
||||
if (cached && !search) { // Don't cache search results
|
||||
return NextResponse.json(cached);
|
||||
}
|
||||
@@ -80,7 +91,7 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
// Cache the result (only for non-search queries)
|
||||
if (!search) {
|
||||
await apiCache.setProjects(result);
|
||||
await apiCache.setProjects(cacheParams, result);
|
||||
}
|
||||
|
||||
return NextResponse.json(result);
|
||||
@@ -95,11 +106,26 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// Check if this is an admin request
|
||||
const isAdminRequest = request.headers.get('x-admin-request') === 'true';
|
||||
if (!isAdminRequest) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Admin access required' },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
const data = await request.json();
|
||||
|
||||
// Remove difficulty field if it exists (since we're removing it)
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { difficulty, ...projectData } = data;
|
||||
|
||||
const project = await prisma.project.create({
|
||||
data: {
|
||||
...data,
|
||||
...projectData,
|
||||
// Set default difficulty since it's required in schema
|
||||
difficulty: 'INTERMEDIATE',
|
||||
performance: data.performance || { lighthouse: 0, bundleSize: '0KB', loadTime: '0s' },
|
||||
analytics: data.analytics || { views: 0, likes: 0, shares: 0 }
|
||||
}
|
||||
@@ -112,7 +138,7 @@ export async function POST(request: NextRequest) {
|
||||
} catch (error) {
|
||||
console.error('Error creating project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create project' },
|
||||
{ error: 'Failed to create project', details: error instanceof Error ? error.message : 'Unknown error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user