Initial commit: CMS completo com gerenciamento de leads e personalização de tema
This commit is contained in:
52
frontend/src/app/api/messages/[id]/route.ts
Normal file
52
frontend/src/app/api/messages/[id]/route.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import prisma from '@/lib/prisma';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const message = await prisma.message.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
if (!message) return NextResponse.json({ error: 'Message not found' }, { status: 404 });
|
||||
return NextResponse.json(message);
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Error fetching message' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const data = await request.json();
|
||||
const message = await prisma.message.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: data.status,
|
||||
},
|
||||
});
|
||||
return NextResponse.json(message);
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Error updating message' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
await prisma.message.delete({
|
||||
where: { id },
|
||||
});
|
||||
return NextResponse.json({ message: 'Message deleted' });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Error deleting message' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
69
frontend/src/app/api/messages/route.ts
Normal file
69
frontend/src/app/api/messages/route.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import prisma from '@/lib/prisma';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const search = searchParams.get('search') || '';
|
||||
const status = searchParams.get('status') || '';
|
||||
|
||||
const where: any = {};
|
||||
|
||||
// Filtro de pesquisa
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ name: { contains: search, mode: 'insensitive' } },
|
||||
{ email: { contains: search, mode: 'insensitive' } },
|
||||
{ subject: { contains: search, mode: 'insensitive' } },
|
||||
{ message: { contains: search, mode: 'insensitive' } }
|
||||
];
|
||||
}
|
||||
|
||||
// Filtro de status
|
||||
if (status) {
|
||||
where.status = status;
|
||||
}
|
||||
|
||||
const messages = await prisma.message.findMany({
|
||||
where,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
return NextResponse.json(messages);
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Error fetching messages' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { name, email, phone, subject, message } = await request.json();
|
||||
|
||||
// Validação básica
|
||||
if (!name || !email || !message) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Nome, email e mensagem são obrigatórios' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Criar mensagem incluindo telefone no assunto se fornecido
|
||||
const fullSubject = phone
|
||||
? `${subject || 'Sem assunto'} | Tel: ${phone}`
|
||||
: subject || 'Sem assunto';
|
||||
|
||||
const newMessage = await prisma.message.create({
|
||||
data: {
|
||||
name,
|
||||
email,
|
||||
subject: fullSubject,
|
||||
message,
|
||||
status: 'Nova'
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json(newMessage, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating message:', error);
|
||||
return NextResponse.json({ error: 'Error creating message' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user