fix: add /api/pages/home endpoint for badge and hero section
This commit is contained in:
155
frontend/src/app/api/pages/home/route.ts
Normal file
155
frontend/src/app/api/pages/home/route.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { cookies } from 'next/headers';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// Middleware de autenticação
|
||||
async function authenticate(request: NextRequest) {
|
||||
const cookieStore = await cookies();
|
||||
const token = cookieStore.get('auth_token')?.value;
|
||||
|
||||
if (!token) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET!) as { userId: string };
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: decoded.userId },
|
||||
select: { id: true, email: true, name: true }
|
||||
});
|
||||
return user;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/pages/home
|
||||
* Busca conteúdo da página home
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const locale = searchParams.get('locale') || 'pt';
|
||||
|
||||
const page = await prisma.pageContent.findUnique({
|
||||
where: { slug_locale: { slug: 'home', locale } }
|
||||
});
|
||||
|
||||
if (!page) {
|
||||
// Retornar página padrão vazia
|
||||
return NextResponse.json({
|
||||
slug: 'home',
|
||||
locale,
|
||||
content: {
|
||||
hero: {
|
||||
title: 'Engenharia de Excelência para Seus Projetos',
|
||||
subtitle: 'Soluções completas em engenharia veicular, mecânica e segurança do trabalho com mais de 15 anos de experiência.',
|
||||
buttonText: 'Conheça Nossos Serviços',
|
||||
badge: {
|
||||
text: 'Coca-Cola',
|
||||
show: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(page);
|
||||
} catch (error) {
|
||||
console.error('Erro ao buscar página home:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erro ao buscar página home' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/pages/home
|
||||
* Salva conteúdo da página home (admin apenas)
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const user = await authenticate(request);
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Não autorizado' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const locale = 'pt'; // Sempre salvar em PT primeiro
|
||||
|
||||
if (!body.content) {
|
||||
return NextResponse.json({ error: 'Conteúdo é obrigatório' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Garantir que é um objeto, não string
|
||||
const content = typeof body.content === 'string'
|
||||
? JSON.parse(body.content)
|
||||
: body.content;
|
||||
|
||||
const page = await prisma.pageContent.upsert({
|
||||
where: { slug_locale: { slug: 'home', locale } },
|
||||
update: { content },
|
||||
create: {
|
||||
slug: 'home',
|
||||
locale,
|
||||
content
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true, page });
|
||||
} catch (error) {
|
||||
console.error('Erro ao salvar página home:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erro ao salvar página home: ' + (error as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT /api/pages/home
|
||||
* Atualiza conteúdo da página home (admin apenas)
|
||||
*/
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const user = await authenticate(request);
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Não autorizado' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const locale = 'pt';
|
||||
|
||||
if (!body.content) {
|
||||
return NextResponse.json({ error: 'Conteúdo é obrigatório' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Garantir que é um objeto, não string
|
||||
const content = typeof body.content === 'string'
|
||||
? JSON.parse(body.content)
|
||||
: body.content;
|
||||
|
||||
const page = await prisma.pageContent.upsert({
|
||||
where: { slug_locale: { slug: 'home', locale } },
|
||||
update: { content },
|
||||
create: {
|
||||
slug: 'home',
|
||||
locale,
|
||||
content
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true, page });
|
||||
} catch (error) {
|
||||
console.error('Erro ao atualizar página home:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erro ao atualizar página home: ' + (error as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user