Grapevine_Disease_Detection/vineye-admin/app/api/alerts/[id]/route.ts
Yanis fe70005a86 add vineye-admin dashboard (Next.js)
Admin panel for VinEye with dashboard, users, diseases, guides, alerts management.
Stack: Next.js App Router + Prisma + PostgreSQL + better-auth.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 11:22:01 +02:00

71 lines
1.9 KiB
TypeScript

import { NextRequest } from "next/server";
import { prisma } from "@/lib/prisma";
import { requireAdmin } from "@/lib/auth-guard";
import { alertSchema } from "@/lib/validations";
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const alert = await prisma.seasonAlert.findUnique({ where: { id } });
if (!alert) {
return Response.json({ error: "Alerte introuvable" }, { status: 404 });
}
return Response.json({ data: alert });
}
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const auth = await requireAdmin();
if ("error" in auth) {
return Response.json({ error: auth.error }, { status: auth.status });
}
const { id } = await params;
const existing = await prisma.seasonAlert.findUnique({ where: { id } });
if (!existing) {
return Response.json({ error: "Alerte introuvable" }, { status: 404 });
}
const body = await request.json();
const result = alertSchema.safeParse(body);
if (!result.success) {
return Response.json(
{ error: "Validation failed", details: result.error.flatten() },
{ status: 400 }
);
}
const alert = await prisma.seasonAlert.update({
where: { id },
data: result.data,
});
return Response.json({ data: alert });
}
export async function DELETE(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const auth = await requireAdmin();
if ("error" in auth) {
return Response.json({ error: auth.error }, { status: auth.status });
}
const { id } = await params;
const existing = await prisma.seasonAlert.findUnique({ where: { id } });
if (!existing) {
return Response.json({ error: "Alerte introuvable" }, { status: 404 });
}
await prisma.seasonAlert.delete({ where: { id } });
return Response.json({ message: "Alerte supprimee" });
}