import { NextResponse } from 'next/server' import { prisma } from '@/lib/prisma' import { getCurrentUser } from '@/lib/auth' export async function PATCH(request: Request, { params }: { params: { id: string } }) { try { const user = await getCurrentUser() if (!user) return NextResponse.json({ error: 'Non authentifi??' }, { status: 401 }) const body = await request.json() const now = new Date() const fields: string[] = [] const values: any[] = [] let idx = 1 for (const key of ['name', 'type', 'technology', 'url', 'status', 'notes', 'processName']) { if (body[key] !== undefined) { fields.push(`"${key}" = $${idx++}`) values.push(body[key]) } } for (const key of ['port', 'pid']) { if (body[key] !== undefined) { fields.push(`"${key}" = $${idx++}`) values.push(body[key] != null ? Number(body[key]) : null) } } for (const key of ['cpuUsage', 'ramUsage', 'diskUsage']) { if (body[key] !== undefined) { fields.push(`"${key}" = $${idx++}`) values.push(body[key] != null ? Number(body[key]) : null) } } if (fields.length === 0) { return NextResponse.json({ error: 'Aucun champ ?? mettre ?? jour' }, { status: 400 }) } fields.push(`"updatedAt" = $${idx++}`) values.push(now) values.push(params.id) values.push(user.id) await prisma.$executeRawUnsafe(` UPDATE "Service" SET ${fields.join(', ')} WHERE id = $${idx} AND "serverId" IN (SELECT id FROM "Server" WHERE "userId" = $${idx + 1}) `, ...values) const updated: any[] = await prisma.$queryRawUnsafe(`SELECT * FROM "Service" WHERE id = $1`, params.id) return NextResponse.json(updated[0]) } catch (error) { console.error(error) return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 }) } } export async function DELETE(request: Request, { params }: { params: { id: string } }) { try { const user = await getCurrentUser() if (!user) return NextResponse.json({ error: 'Non authentifi??' }, { status: 401 }) await prisma.$executeRawUnsafe(` DELETE FROM "Service" WHERE id = $1 AND "serverId" IN (SELECT id FROM "Server" WHERE "userId" = $2) `, params.id, user.id) return NextResponse.json({ success: true }) } catch (error) { console.error(error) return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 }) } }