PJob-prisma/server/api/job/create.ts

51 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2024-04-12 10:23:44 +00:00
import { Prisma, PrismaClient } from "@prisma/client";
const db = new PrismaClient();
async function up(body: Prisma.JobCreateInput) {
let result;
const data: Prisma.JobCreateInput = body;
await db.data
.create({
data: body.data,
select: {
id: true,
},
})
.then(async (res) => {
return await db.job
.create({
data: {
area: data.area,
dataId: res.id,
name: data.name,
pin: data.pin,
place: data.place,
worktime: data.worktime,
},
})
.then((job) => {
result = JSON.stringify({
code: 1,
status: "success",
job: {
id: job.id,
name: job.name,
},
});
})
.catch((reason) => {
result = JSON.stringify({
code: 0,
status: "faild",
resoan: reason,
});
return result;
});
});
return result;
}
export default defineEventHandler(async (event) => {
const body = await readBody(event);
return await up(body);
});