Spaces:
Build error
Build error
File size: 3,277 Bytes
333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 b8b98dd 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 333c51a 5cff373 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import ZAI from "z-ai-web-dev-sdk";
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const status = searchParams.get("status");
const platform = searchParams.get("platform");
const where: Record<string, string | null> = {};
if (status) where.status = status;
if (platform) where.platformId = platform;
const posts = await db.post.findMany({ where, orderBy: { createdAt: "desc" }, take: 50 });
return NextResponse.json({ success: true, posts });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { title, type, caption, scheduledAt, platformId, autoGenerateCaption } = body;
if (!title) {
return NextResponse.json({ success: false, error: "Titulo requerido" }, { status: 400 });
}
let finalCaption: string | null = caption || null;
if (autoGenerateCaption && !caption) {
try {
const { generateSocialPost } = await import("@/lib/content-generator");
const character = await db.character.findFirst({ where: { isActive: true } });
if (character) {
const result = await generateSocialPost({
character,
topic: title,
platform: "instagram",
});
finalCaption = result.caption;
if (result.hashtags?.length) {
finalCaption += "\n\n#" + result.hashtags.join(" #");
}
}
} catch (e) {
console.error("Auto-caption error", e);
}
}
const post = await db.post.create({
data: {
title,
caption: finalCaption,
type: type || "post",
status: scheduledAt ? "scheduled" : "draft",
platformId: platformId || null,
scheduledAt: scheduledAt ? new Date(scheduledAt) : null
}
});
return NextResponse.json({ success: true, post });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}
export async function PUT(request: NextRequest) {
try {
const body = await request.json();
const { id, status, publishedAt } = body;
if (!id) return NextResponse.json({ success: false, error: "ID requerido" }, { status: 400 });
const data: { status?: string; publishedAt?: Date } = {};
if (status) data.status = status;
if (publishedAt) data.publishedAt = new Date(publishedAt);
const post = await db.post.update({ where: { id }, data });
return NextResponse.json({ success: true, post });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}
export async function DELETE(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
if (!id) return NextResponse.json({ success: false, error: "ID requerido" }, { status: 400 });
await db.post.delete({ where: { id } });
return NextResponse.json({ success: true });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
} |