feat: enable project catalog management

This commit is contained in:
Erik
2025-11-27 16:22:14 -03:00
parent f077569bc1
commit 1138747565
6 changed files with 729 additions and 222 deletions

View File

@@ -1,37 +1,78 @@
import { NextResponse } from 'next/server';
import prisma from '@/lib/prisma';
import { Prisma } from '@prisma/client';
export async function GET() {
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const takeParam = searchParams.get('take');
const statusFilter = searchParams.get('status');
const featuredFilter = searchParams.get('featured');
const take = takeParam ? Number.parseInt(takeParam, 10) : undefined;
const where: Prisma.ProjectWhereInput = {};
if (statusFilter === 'published') {
where.status = { not: 'Rascunho' };
} else if (statusFilter === 'draft') {
where.status = 'Rascunho';
} else if (statusFilter) {
where.status = statusFilter;
}
if (featuredFilter === 'true') {
where.featured = true;
}
const projects = await prisma.project.findMany({
where,
orderBy: { createdAt: 'desc' },
take: Number.isInteger(take) && take! > 0 ? take : undefined,
});
return NextResponse.json(projects);
} catch (error) {
console.error('Error fetching projects:', error);
return NextResponse.json({ error: 'Error fetching projects' }, { status: 500 });
return NextResponse.json({ error: 'Erro ao buscar projetos' }, { status: 500 });
}
}
export async function POST(request: Request) {
try {
const data = await request.json();
const {
title,
category,
client,
status,
description,
completionDate,
coverImage,
galleryImages,
featured,
} = data;
if (!title || !category) {
return NextResponse.json({ error: 'Título e categoria são obrigatórios.' }, { status: 400 });
}
const project = await prisma.project.create({
data: {
title: data.title,
category: data.category,
client: data.client,
status: data.status,
completionDate: data.completionDate ? new Date(data.completionDate) : null,
description: data.description,
coverImage: data.coverImage,
galleryImages: data.galleryImages,
featured: data.featured,
title,
category,
client,
status: status || 'Em andamento',
description,
completionDate: completionDate ? new Date(completionDate) : null,
coverImage,
galleryImages: Array.isArray(galleryImages) ? galleryImages : [],
featured: Boolean(featured),
},
});
return NextResponse.json(project);
return NextResponse.json(project, { status: 201 });
} catch (error) {
console.error('Error creating project:', error);
return NextResponse.json({ error: 'Error creating project' }, { status: 500 });
return NextResponse.json({ error: 'Erro ao criar projeto' }, { status: 500 });
}
}