From 278b9ade2807ce205663233c2b4b4469479af92b Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 29 Nov 2025 13:03:11 -0300 Subject: [PATCH] fix: add /api/pages/home endpoint for badge and hero section --- frontend/src/app/api/pages/home/route.ts | 155 +++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 frontend/src/app/api/pages/home/route.ts diff --git a/frontend/src/app/api/pages/home/route.ts b/frontend/src/app/api/pages/home/route.ts new file mode 100644 index 0000000..0668183 --- /dev/null +++ b/frontend/src/app/api/pages/home/route.ts @@ -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 } + ); + } +}