79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import prisma from '@/lib/prisma';
|
|
import { Prisma } from '@prisma/client';
|
|
|
|
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: '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,
|
|
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, { status: 201 });
|
|
} catch (error) {
|
|
console.error('Error creating project:', error);
|
|
return NextResponse.json({ error: 'Erro ao criar projeto' }, { status: 500 });
|
|
}
|
|
}
|