87 lines
1.9 KiB
TypeScript
87 lines
1.9 KiB
TypeScript
|
|
|
||
|
|
import { Request } from "express";
|
||
|
|
import { prisma } from "../../lib/prisma";
|
||
|
|
import { AppError } from "../../utils/app_error";
|
||
|
|
|
||
|
|
const get_all_order_from_db = async (req: Request) => {
|
||
|
|
// define your own login here
|
||
|
|
const result = await prisma.order.findMany();
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
|
||
|
|
const get_single_order_from_db = async (req: Request) => {
|
||
|
|
// define your own login here
|
||
|
|
const { id } = req.params;
|
||
|
|
const result = await prisma.order.findUnique({ where: { id } });
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
|
||
|
|
const create_order_into_db = async (req: Request) => {
|
||
|
|
// define your own login here
|
||
|
|
|
||
|
|
const user = req.user
|
||
|
|
console.log(user)
|
||
|
|
const {
|
||
|
|
shopAccountId,
|
||
|
|
productPrice,
|
||
|
|
productQuantity,
|
||
|
|
productName,
|
||
|
|
customerName,
|
||
|
|
customerPhone,
|
||
|
|
customerAddress,
|
||
|
|
customerNote,
|
||
|
|
paymentType,
|
||
|
|
status,
|
||
|
|
} = req.body
|
||
|
|
const isUserExists = await prisma.account.findFirst({
|
||
|
|
where: {
|
||
|
|
id: user?.accountId
|
||
|
|
}
|
||
|
|
})
|
||
|
|
if (!isUserExists) {
|
||
|
|
throw new AppError("Account not found", 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
const result = await prisma.order.create({
|
||
|
|
data: {
|
||
|
|
productPrice,
|
||
|
|
productQuantity,
|
||
|
|
productName,
|
||
|
|
customerName,
|
||
|
|
customerPhone,
|
||
|
|
customerAddress,
|
||
|
|
customerNote,
|
||
|
|
paymentType,
|
||
|
|
status,
|
||
|
|
account: {
|
||
|
|
connect: {
|
||
|
|
id: user?.accountId,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
});
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
|
||
|
|
const update_order_into_db = async (req: Request) => {
|
||
|
|
// define your own login here
|
||
|
|
const { id } = req.params;
|
||
|
|
const result = await prisma.order.update({ where: { id }, data: req.body });
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
|
||
|
|
const delete_order_from_db = async (req: Request) => {
|
||
|
|
// define your own login here
|
||
|
|
const { id } = req.params;
|
||
|
|
const result = await prisma.order.delete({ where: { id } });
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
|
||
|
|
export const order_service = {
|
||
|
|
get_all_order_from_db,
|
||
|
|
get_single_order_from_db,
|
||
|
|
create_order_into_db,
|
||
|
|
update_order_into_db,
|
||
|
|
delete_order_from_db,
|
||
|
|
};
|