WorkshopTasker/server/api/orders/[id].patch.ts

20 lines
626 B
TypeScript
Raw Normal View History

import { defineEventHandler, readBody } from "h3";
2023-05-24 09:40:45 +02:00
import { checkIsOrder } from "../orders.post";
import { database as db } from "~/server/utils/database";
import { createError } from "#imports";
2023-05-24 09:40:45 +02:00
export default defineEventHandler(async (e) => {
const body = await readBody(e);
const id = e.context.params?.id;
if (!checkIsOrder(e, true)) throw createError({ message: "Invalid body", statusCode: 400 });
const database = await db.new();
await database.beginTransaction();
for (const [k, v] of Object.entries(body))
database.query(`UPDATE TABLE \`orders\` SET \`${k}\` = ? WHERE \`id\` = ?`, [v, id]);
});