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,60 +1,77 @@
import { NextRequest, NextResponse } from 'next/server';
import { NextResponse } from 'next/server';
import prisma from '@/lib/prisma';
import { Prisma } from '@prisma/client';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
type Params = {
params: { id: string };
};
export async function GET(_request: Request, { params }: Params) {
try {
const { id } = await params;
const project = await prisma.project.findUnique({
where: { id },
where: { id: params.id },
});
if (!project) return NextResponse.json({ error: 'Project not found' }, { status: 404 });
if (!project) {
return NextResponse.json({ error: 'Projeto não encontrado' }, { status: 404 });
}
return NextResponse.json(project);
} catch (error) {
return NextResponse.json({ error: 'Error fetching project' }, { status: 500 });
console.error('Error fetching project:', error);
return NextResponse.json({ error: 'Erro ao buscar projeto' }, { status: 500 });
}
}
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
export async function PATCH(request: Request, { params }: Params) {
try {
const { id } = await params;
const data = await request.json();
const body = await request.json();
const updateData: Record<string, unknown> = {};
if (body.title !== undefined) updateData.title = body.title;
if (body.category !== undefined) updateData.category = body.category;
if (body.client !== undefined) updateData.client = body.client;
if (body.status !== undefined) updateData.status = body.status;
if (body.description !== undefined) updateData.description = body.description;
if (body.coverImage !== undefined) updateData.coverImage = body.coverImage;
if (body.galleryImages !== undefined) {
updateData.galleryImages = Array.isArray(body.galleryImages) ? body.galleryImages : [];
}
if (body.featured !== undefined) updateData.featured = Boolean(body.featured);
if (body.completionDate !== undefined) {
updateData.completionDate = body.completionDate ? new Date(body.completionDate) : null;
}
const project = await prisma.project.update({
where: { id },
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,
},
where: { id: params.id },
data: updateData,
});
return NextResponse.json(project);
} catch (error) {
return NextResponse.json({ error: 'Error updating project' }, { status: 500 });
const err = error as Prisma.PrismaClientKnownRequestError;
if (err?.code === 'P2025') {
return NextResponse.json({ error: 'Projeto não encontrado' }, { status: 404 });
}
console.error('Error updating project:', error);
return NextResponse.json({ error: 'Erro ao atualizar projeto' }, { status: 500 });
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
export async function DELETE(_request: Request, { params }: Params) {
try {
const { id } = await params;
await prisma.project.delete({
where: { id },
where: { id: params.id },
});
return NextResponse.json({ message: 'Project deleted' });
return NextResponse.json({ success: true });
} catch (error) {
return NextResponse.json({ error: 'Error deleting project' }, { status: 500 });
const err = error as Prisma.PrismaClientKnownRequestError;
if (err?.code === 'P2025') {
return NextResponse.json({ error: 'Projeto não encontrado' }, { status: 404 });
}
console.error('Error deleting project:', error);
return NextResponse.json({ error: 'Erro ao excluir projeto' }, { status: 500 });
}
}

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 });
}
}