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:
parent
be1e3909b6
commit
eebf25198d
39 changed files with 1081 additions and 1292 deletions
|
@ -1,19 +1,22 @@
|
|||
import { defineEventHandler } from "h3";
|
||||
import { type ResultSetHeader } from "mysql2";
|
||||
|
||||
import { database } from "~/server/utils/database";
|
||||
|
||||
import { createError } from "#imports";
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const id = e.context.params?.id;
|
||||
const id = e.context.params?.id as string;
|
||||
|
||||
const [result] = await database.query(
|
||||
"DELETE FROM `orders` WHERE `id` = ?",
|
||||
[id],
|
||||
) as unknown as [ResultSetHeader];
|
||||
|
||||
if (result.affectedRows === 0) throw createError({ statusCode: 404 });
|
||||
try {
|
||||
await database.order.delete({
|
||||
where: {
|
||||
id: BigInt(id),
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
// FIXME: should be 500 on errors other than "RecordNotFound"
|
||||
throw createError({ statusCode: 404 });
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
|
|
@ -1,109 +1,38 @@
|
|||
import { defineEventHandler } from "h3";
|
||||
import { createError } from "#imports";
|
||||
|
||||
import { type offer as offerType, type order } from "~/utils/types/database";
|
||||
import { database, type data } from "~/server/utils/database";
|
||||
import { database } from "~/server/utils/database";
|
||||
import { prismaToWeb } from "~/server/utils/prismaToWeb";
|
||||
|
||||
export async function orderExists(id: string) {
|
||||
const [[exists]] = await database.query(
|
||||
"SELECT EXISTS(*) AS `exists` FROM `orders` WHERE `id` = ?",
|
||||
[id],
|
||||
) as data<{exists: 0 | 1}>;
|
||||
|
||||
return exists.exists === 1;
|
||||
export async function orderExists(id: bigint) {
|
||||
const exists = await database.order.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
return exists !== null;
|
||||
}
|
||||
|
||||
export async function getImportedProducts(id: string) {
|
||||
const [importedProducts] = await database.query(
|
||||
["SELECT",
|
||||
"CONVERT(`id`, CHAR) AS `id`,",
|
||||
"`name`,",
|
||||
"`link`,",
|
||||
"`price`,",
|
||||
"`price_imported`",
|
||||
"FROM `imported_products`",
|
||||
"WHERE `order` = ?",
|
||||
].join(" "),
|
||||
[id],
|
||||
) as data<{
|
||||
id: string,
|
||||
name: string | null,
|
||||
link: string,
|
||||
price: string,
|
||||
price_imported: string
|
||||
}>;
|
||||
|
||||
return importedProducts;
|
||||
}
|
||||
|
||||
export async function getWork(id: string) {
|
||||
const [work] = await database.query(
|
||||
["SELECT",
|
||||
"CONVERT(`id`, CHAR) AS `id`,",
|
||||
"CONVERT(`offer`, CHAR) AS `offer`,",
|
||||
"`price`,",
|
||||
"`notes`,",
|
||||
"`is_fulfilled`",
|
||||
"FROM `work`",
|
||||
"WHERE `order` = ?",
|
||||
].join(" "),
|
||||
[id],
|
||||
) as data<{
|
||||
id: string,
|
||||
offer: offerType,
|
||||
price: number,
|
||||
notes: string | null,
|
||||
is_fulfilled: 0 | 1,
|
||||
}>;
|
||||
|
||||
const [offer] = await database.query(
|
||||
["SELECT",
|
||||
"CONVERT(`offer`.`id`, CHAR) AS `id`,",
|
||||
"`offer`.`name`,",
|
||||
"`offer`.`description`,",
|
||||
"`offer`.`recommended_price`",
|
||||
"FROM",
|
||||
"`work`",
|
||||
"LEFT JOIN `offer` ON `work`.`offer` = `offer`.`id`",
|
||||
"WHERE `work`.`order` = ?",
|
||||
].join(" "),
|
||||
[id],
|
||||
) as data<offerType>;
|
||||
|
||||
// @ts-ignore i.offer is string, but it needs to be an offer object
|
||||
for (const i of work) i.offer = offer.find(e => e.id === i.offer) as offerType;
|
||||
return work;
|
||||
}
|
||||
|
||||
export async function getOrder(id: string): Promise<order> {
|
||||
const [[order]] = await database.query(
|
||||
["SELECT",
|
||||
"CONVERT(`id`, CHAR) AS `id`,",
|
||||
"CONVERT(`client`, CHAR) AS `client`,",
|
||||
"CONVERT(`user`, CHAR) AS `user`, ",
|
||||
"`is_draft`,",
|
||||
"`value`",
|
||||
"FROM `orderSummaries`",
|
||||
"WHERE `id` = ?",
|
||||
].join(" "),
|
||||
[id],
|
||||
) as data<{
|
||||
id: string,
|
||||
client: string,
|
||||
user: string,
|
||||
is_draft: 0 | 1,
|
||||
value: number,
|
||||
}>;
|
||||
export async function getOrder(id: bigint) {
|
||||
const order = await database.order.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
include: {
|
||||
imported_products: true,
|
||||
work: {
|
||||
include: {
|
||||
offer: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!order) throw createError({ statusCode: 404 });
|
||||
|
||||
const importedProducts = await getImportedProducts(id);
|
||||
const work = await getWork(id);
|
||||
|
||||
return { ...order, imported_products: importedProducts, work };
|
||||
return order;
|
||||
}
|
||||
|
||||
export default defineEventHandler((e) => {
|
||||
const key = e.context.params?.id;
|
||||
return getOrder(key as string);
|
||||
const key = e.context.params?.id as string;
|
||||
return getOrder(BigInt(key)).then(prismaToWeb);
|
||||
});
|
||||
|
|
|
@ -1,19 +1,27 @@
|
|||
import { defineEventHandler, readBody } from "h3";
|
||||
|
||||
import { checkIsOrder } from "../orders.post";
|
||||
import { database as db } from "~/server/utils/database";
|
||||
import { getOrder } 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 body = await readBody(e);
|
||||
const id = e.context.params?.id;
|
||||
const id = e.context.params?.id as string;
|
||||
|
||||
if (!checkIsOrder(e, true)) throw createError({ message: "Invalid body", statusCode: 400 });
|
||||
if (!checkIsOrder(body, true)) throw createError({ message: "Invalid body", statusCode: 400 });
|
||||
|
||||
const database = await db.new();
|
||||
await database.beginTransaction();
|
||||
await database.order.update({
|
||||
where: {
|
||||
id: BigInt(id),
|
||||
},
|
||||
data: {
|
||||
clientId: body.clientId ? BigInt(body.clientId) : undefined,
|
||||
draft: body.draft,
|
||||
},
|
||||
});
|
||||
|
||||
for (const [k, v] of Object.entries(body))
|
||||
database.query(`UPDATE TABLE \`orders\` SET \`${k}\` = ? WHERE \`id\` = ?`, [v, id]);
|
||||
return getOrder(BigInt(id)).then(prismaToWeb);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue