feat: Initialize Next.js project with Prisma, Tailwind CSS, and API routes for device management and testing.
This commit is contained in:
44
src/app/api/devices/register/route.ts
Normal file
44
src/app/api/devices/register/route.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const { serialNumber, macAddress } = await req.json();
|
||||
|
||||
if (!serialNumber || !macAddress) {
|
||||
return NextResponse.json(
|
||||
{ error: "Serial number and MAC address are required" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if device already exists
|
||||
let device = await prisma.device.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ serialNumber },
|
||||
{ macAddress }
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
// Register as pending
|
||||
device = await prisma.device.create({
|
||||
data: {
|
||||
serialNumber,
|
||||
macAddress,
|
||||
status: "PENDING",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(device);
|
||||
} catch (error) {
|
||||
console.error("Registration error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal Server Error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user