Spaces:
Configuration error
Configuration error
| import { NextRequest, NextResponse } from "next/server"; | |
| import { prisma } from "@/lib/prisma"; | |
| // GET /api/characters | |
| export async function GET(request: NextRequest) { | |
| try { | |
| const { searchParams } = new URL(request.url); | |
| const category = searchParams.get("category"); | |
| const search = searchParams.get("search"); | |
| const sortBy = searchParams.get("sortBy") || "popular"; | |
| const limit = parseInt(searchParams.get("limit") || "20"); | |
| const offset = parseInt(searchParams.get("offset") || "0"); | |
| const where: Record<string, unknown> = { | |
| visibility: "public", | |
| }; | |
| if (category && category !== "all") { | |
| where.category = category; | |
| } | |
| if (search) { | |
| where.OR = [ | |
| { name: { contains: search, mode: "insensitive" } }, | |
| { description: { contains: search, mode: "insensitive" } }, | |
| { tags: { has: search.toLowerCase() } }, | |
| ]; | |
| } | |
| const orderBy: Record<string, string> = {}; | |
| switch (sortBy) { | |
| case "newest": | |
| orderBy.createdAt = "desc"; | |
| break; | |
| case "votes": | |
| orderBy.votesCount = "desc"; | |
| break; | |
| case "chats": | |
| orderBy.totalChats = "desc"; | |
| break; | |
| default: | |
| orderBy.totalChats = "desc"; | |
| } | |
| const [characters, total] = await Promise.all([ | |
| prisma.character.findMany({ | |
| where, | |
| include: { | |
| creator: { | |
| select: { | |
| id: true, | |
| name: true, | |
| username: true, | |
| image: true, | |
| }, | |
| }, | |
| }, | |
| orderBy, | |
| take: limit, | |
| skip: offset, | |
| }), | |
| prisma.character.count({ where }), | |
| ]); | |
| return NextResponse.json({ | |
| characters, | |
| total, | |
| hasMore: offset + limit < total, | |
| }); | |
| } catch (error) { | |
| console.error("Error fetching characters:", error); | |
| return NextResponse.json( | |
| { error: "Failed to fetch characters" }, | |
| { status: 500 } | |
| ); | |
| } | |
| } | |
| // POST /api/characters | |
| export async function POST(request: NextRequest) { | |
| try { | |
| const body = await request.json(); | |
| const { | |
| name, | |
| description, | |
| personality, | |
| background, | |
| systemPrompt, | |
| avatar, | |
| style, | |
| category, | |
| tags, | |
| responseStyle, | |
| temperature, | |
| creatorId, | |
| } = body; | |
| if (!name || !personality || !systemPrompt) { | |
| return NextResponse.json( | |
| { error: "name, personality, and systemPrompt are required" }, | |
| { status: 400 } | |
| ); | |
| } | |
| // Generate slug from name | |
| const slug = name | |
| .toLowerCase() | |
| .replace(/[^a-z0-9]+/g, "-") | |
| .replace(/(^-|-$)/g, "") + | |
| "-" + | |
| Date.now().toString(36); | |
| const character = await prisma.character.create({ | |
| data: { | |
| slug, | |
| name, | |
| description, | |
| personality, | |
| background, | |
| systemPrompt, | |
| avatar, | |
| style, | |
| category, | |
| tags: tags || [], | |
| responseStyle, | |
| temperature: temperature || 0.7, | |
| creatorId, | |
| }, | |
| include: { | |
| creator: { | |
| select: { | |
| id: true, | |
| name: true, | |
| username: true, | |
| }, | |
| }, | |
| }, | |
| }); | |
| return NextResponse.json(character); | |
| } catch (error) { | |
| console.error("Error creating character:", error); | |
| return NextResponse.json( | |
| { error: "Failed to create character" }, | |
| { status: 500 } | |
| ); | |
| } | |
| } | |