Replace mysql2 with prisma

also I updated packages,
and properly typed api input
a lot of time was spent, I don't remeber what really I did x3
but everything was related to replacing mysql2 with prisma
This commit is contained in:
Wroclaw 2023-11-08 05:35:48 +01:00
parent be1e3909b6
commit eebf25198d
39 changed files with 1081 additions and 1292 deletions

View file

@ -1,14 +1,27 @@
import { defineEventHandler } from "h3";
import { orderExists, getImportedProducts } from "../[id].get";
import { orderExists } from "../[id].get";
import { database } from "~/server/utils/database";
import { prismaToWeb } from "~/server/utils/prismaToWeb";
import { createError } from "#imports";
export default defineEventHandler(async (e) => {
const id = e.context.params?.id as string;
const orderId = e.context.params?.id as string;
if (!orderExists(id)) throw createError({ statusCode: 404 });
if (!(await orderExists(BigInt(orderId)))) throw createError({ statusCode: 404 });
const importedProducts = await getImportedProducts(id);
return importedProducts;
return database.importedProduct.findMany({
where: {
orderId: BigInt(orderId),
},
select: {
id: true,
link: true,
name: true,
orderId: true,
price: true,
price_imported: true,
},
}).then(prismaToWeb);
});

View file

@ -1,29 +1,33 @@
import { defineEventHandler, readBody, setResponseStatus } from "h3";
import { checkIsImportedProduct } from "../../orders.post";
import { getImportedProducts, orderExists } from "../[id].get";
import { orderExists } from "../[id].get";
import Snowflake from "~/utils/snowflake";
import { database } from "~/server/utils/database";
import { prismaToWeb } from "~/server/utils/prismaToWeb";
import { createError } from "#imports";
export default defineEventHandler(async (e) => {
const body = await readBody(e);
const idOrder = e.context.params?.id as string;
const idImportedProducts = new Snowflake().toString();
const idImportedProduct = new Snowflake().state;
if (!orderExists(idOrder)) throw createError({ statusCode: 404 });
if (!checkIsImportedProduct(body, false)) throw createError({ message: "Invalid body", statusCode: 400 });
if (!await orderExists(BigInt(idOrder))) throw createError({ statusCode: 404 });
if (!checkIsImportedProduct(body, false, false)) throw createError({ message: "Invalid body", statusCode: 400 });
await database.query(
["INSERT INTO",
"`imported_products`",
"VALUES",
"(?, ?, ?, ?, ?, ?)",
].join(" "),
[idImportedProducts, idOrder, body.name, body.link, body.price_imported, body.price],
);
const rvalue = await database.importedProduct.create({
data: {
id: idImportedProduct,
link: body.link,
name: body.name,
orderId: BigInt(idOrder),
price: body.price,
price_imported: body.price_imported,
},
});
setResponseStatus(e, 201);
return getImportedProducts(idOrder);
return prismaToWeb(rvalue);
});

View file

@ -1,14 +1,31 @@
import { defineEventHandler } from "h3";
import { orderExists, getWork } from "../[id].get";
import { orderExists } from "../[id].get";
import { database } from "~/server/utils/database";
import { prismaToWeb } from "~/server/utils/prismaToWeb";
import { createError } from "#imports";
export default defineEventHandler(async (e) => {
const id = e.context.params?.id as string;
const orderId = e.context.params?.id as string;
if (!orderExists(id)) throw createError({ statusCode: 404 });
if (!await orderExists(BigInt(orderId))) throw createError({ statusCode: 404 });
const work = await getWork(id);
return work;
const data = await database.work.findMany({
where: {
orderId: BigInt(orderId),
},
select: {
id: true,
fulfilled: true,
notes: true,
offer: true,
orderId: true,
price: true,
},
});
if (!data) throw createError({ statusCode: 404 });
return prismaToWeb(data);
});

View file

@ -1,30 +1,34 @@
import { defineEventHandler, readBody, setResponseStatus } from "h3";
import { Decimal } from "@prisma/client/runtime/library";
import { checkIsWork } from "../../orders.post";
import { getWork, orderExists } from "../[id].get";
import { orderExists } from "../[id].get";
import Snowflake from "~/utils/snowflake";
import { database } from "~/server/utils/database";
import { prismaToWeb } from "~/server/utils/prismaToWeb";
import { createError } from "#imports";
export default defineEventHandler(async (e) => {
const body = await readBody(e);
const idOrder = e.context.params?.id as string;
const idWork = new Snowflake().toString();
const idWork = new Snowflake().state;
if (!orderExists(idOrder)) throw createError({ statusCode: 404 });
if (!checkIsWork(body, false)) throw createError({ message: "Invalid body", statusCode: 400 });
if (!orderExists(BigInt(idOrder))) throw createError({ statusCode: 404 });
if (!checkIsWork(body, false, false)) throw createError({ message: "Invalid body", statusCode: 400 });
await database.query(
["INSERT INTO",
"`work`",
"VALUES",
"(?, ?, ?, ?, ?, ?)",
].join(" "),
[idWork, idOrder, body.offer, body.price, body.notes, body.is_fulfilled],
);
const rvalue = await database.work.create({
data: {
id: BigInt(idWork),
fulfilled: body.fulfilled,
notes: body.notes,
offerId: BigInt(body.offerId),
orderId: BigInt(body.orderId),
price: new Decimal(body.price),
},
});
setResponseStatus(e, 201);
return getWork(idWork);
return prismaToWeb(rvalue);
});

View file

@ -1,7 +1,5 @@
import { defineEventHandler } from "h3";
import { type ResultSetHeader } from "mysql2";
import { orderExists } from "../../[id].get";
import { database } from "~/server/utils/database";
import { createError } from "#imports";
@ -10,13 +8,17 @@ export default defineEventHandler(async (e) => {
const idOrder = e.context.params?.id as string;
const idWork = e.context.params?.idWork as string;
if (!orderExists(idOrder)) throw createError({ statusCode: 404 });
try {
await database.work.delete({
where: {
id: BigInt(idWork),
orderId: BigInt(idOrder),
},
});
} catch (e) {
// FIXME: should be 500 on errors other than "RecordNotFound"
throw createError({ statusCode: 404 });
}
const [response] = await database.query(
"DELETE FROM `work` WHERE `id` = ?",
[idWork],
) as unknown as [ResultSetHeader];
if (response.affectedRows === 0) throw createError({ statusCode: 404 });
return null;
});

View file

@ -1,13 +1,32 @@
import { defineEventHandler } from "h3";
import { orderExists, getWork } from "../../[id].get";
import { orderExists } from "../../[id].get";
import { database } from "~/server/utils/database";
import { prismaToWeb } from "~/server/utils/prismaToWeb";
import { createError } from "#imports";
export default defineEventHandler((e) => {
export default defineEventHandler(async (e) => {
const idOrder = e.context.params?.id as string;
const idWork = e.context.params?.idWork as string;
if (!orderExists(idOrder)) throw createError({ statusCode: 404 });
return getWork(idWork);
if (!await orderExists(BigInt(idOrder))) throw createError({ statusCode: 404 });
const data = await database.work.findUnique({
where: {
orderId: BigInt(idOrder),
id: BigInt(idWork),
},
select: {
id: true,
fulfilled: true,
notes: true,
offer: true,
orderId: true,
price: true,
},
});
if (!data) throw createError({ statusCode: 404 });
return prismaToWeb(data);
});