From 5d621eb99abbf16a93eb09aaefde07e029350df7 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 1 Apr 2022 12:56:44 +0200 Subject: [PATCH] =?UTF-8?q?Add=20pizza=20and=20orders=20controller=20=5FSt?= =?UTF-8?q?iifi=20h=C3=BCf=20ma=20des=20zu=20fixn=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/orders.controller.ts | 91 ++++++++++++++++++++++ src/controller/organisations.controller.ts | 2 +- src/controller/pizzas.controller.ts | 87 +++++++++++++++++++++ src/controller/restaurants.controller.ts | 2 +- src/routes/testrouter.route.ts | 8 +- 5 files changed, 184 insertions(+), 6 deletions(-) create mode 100644 src/controller/orders.controller.ts create mode 100644 src/controller/pizzas.controller.ts diff --git a/src/controller/orders.controller.ts b/src/controller/orders.controller.ts new file mode 100644 index 0000000..89e6c38 --- /dev/null +++ b/src/controller/orders.controller.ts @@ -0,0 +1,91 @@ +import { Request, Response } from "express"; +import { HttpStatusCodes } from "../statusCodes"; +import { DataType } from "../datatypes"; +import prisma from "../lib/prisma.lib"; + +export const addOrder = async (req: Request, res: Response) => { + const userId = Number(req.body.userId); + + if (Number.isNaN(userId)) { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + userId: DataType.NUMBER, + //??? + }); + return; + } + +console.log(req.body.pizzas); + + const order = await prisma.order.create({ + data: { + userId: req.body.userId, + //??? + } + }); + + res.status(HttpStatusCodes.CREATED).json(order); +}; + +export const updateOrder = async (req: Request, res: Response) => { + const id = Number(req.params.id); + + if (Number.isNaN(id)) { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + id: DataType.NUMBER, + }); + } else if (typeof req.body.userId !== "string") { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + name: DataType.STRING, + //??? + }); + return; + } + + const order = await prisma.order.update({ + where: { + id: id, + }, + data: { + userId: req.body.userId, + //??? + }, + }); + + res.status(HttpStatusCodes.OK).json(order); +}; + +export const deleteOrder = async (req: Request, res: Response) => { + const id = Number(req.params.id); + + if (Number.isNaN(id)) { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + id: DataType.NUMBER, + }); + } + + await prisma.order.delete({ + where: { + id: id, + } + }); + + res.status(HttpStatusCodes.NO_CONTENT).json(); +}; + +export const getOrder = async (req: Request, res: Response) => { + const id = Number(req.params.id); + + if (Number.isNaN(id)) { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + id: DataType.NUMBER, + }); + } + + const order = await prisma.restaurant.findUnique({ + where: { + id: id, + } + }); + + res.status(HttpStatusCodes.OK).json(order); +}; diff --git a/src/controller/organisations.controller.ts b/src/controller/organisations.controller.ts index 7afba33..655adb4 100644 --- a/src/controller/organisations.controller.ts +++ b/src/controller/organisations.controller.ts @@ -50,7 +50,7 @@ export const updateOrganization = async (req: Request, res: Response) => { res.status(HttpStatusCodes.OK).json(organization); }; -export const deleteRestaurant = async (req: Request, res: Response) => { +export const deleteOrganization = async (req: Request, res: Response) => { const id = Number(req.params.id); if (Number.isNaN(id)) { diff --git a/src/controller/pizzas.controller.ts b/src/controller/pizzas.controller.ts new file mode 100644 index 0000000..3f0e314 --- /dev/null +++ b/src/controller/pizzas.controller.ts @@ -0,0 +1,87 @@ +import { Request, Response } from "express"; +import { HttpStatusCodes } from "../statusCodes"; +import { DataType } from "../datatypes"; +import prisma from "../lib/prisma.lib"; + +export const addPizza = async (req: Request, res: Response) => { + if (typeof req.body.name !== "string" || typeof req.body.description !== "string") { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + name: DataType.STRING, + description: DataType.STRING, + }); + return; + } + + const pizza = await prisma.pizza.create({ + data: { + name: req.body.name, + restaurantId: -1 + }, + }); + + res.status(HttpStatusCodes.CREATED).json(pizza); +}; + +export const updatePizza = async (req: Request, res: Response) => { + const id = Number(req.params.id); + + if (Number.isNaN(id)) { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + id: DataType.NUMBER, + }); + } else if (typeof req.body.name !== "string" || typeof req.body.description !== "string") { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + name: DataType.STRING, + description: DataType.STRING, + }); + return; + } + + const pizza = await prisma.pizza.update({ + where: { + id: id, + }, + data: { + name: req.body.name, + description: req.body.description, + }, + }); + + res.status(HttpStatusCodes.OK).json(pizza); +}; + +export const deletePizza = async (req: Request, res: Response) => { + const id = Number(req.params.id); + + if (Number.isNaN(id)) { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + id: DataType.NUMBER, + }); + } + + await prisma.pizza.delete({ + where: { + id: id, + } + }); + + res.status(HttpStatusCodes.NO_CONTENT).json(); +}; + +export const getPizza = async (req: Request, res: Response) => { + const id = Number(req.params.id); + + if (Number.isNaN(id)) { + res.status(HttpStatusCodes.BAD_REQUEST).json({ + id: DataType.NUMBER, + }); + } + + const pizza = await prisma.pizza.findUnique({ + where: { + id: id + } + }); + + res.status(HttpStatusCodes.OK).json(pizza); +}; diff --git a/src/controller/restaurants.controller.ts b/src/controller/restaurants.controller.ts index ffe8ba8..7113acc 100644 --- a/src/controller/restaurants.controller.ts +++ b/src/controller/restaurants.controller.ts @@ -79,7 +79,7 @@ export const getRestaurant = async (req: Request, res: Response) => { const restaurant = await prisma.restaurant.findUnique({ where: { - id: id, + id: id } }); diff --git a/src/routes/testrouter.route.ts b/src/routes/testrouter.route.ts index f03c449..695f5b4 100644 --- a/src/routes/testrouter.route.ts +++ b/src/routes/testrouter.route.ts @@ -1,6 +1,6 @@ import express, { Request, Response } from "express"; import { sayHello } from "../controller/testcontroller.controller"; -import { addRestaurant, updateRestaurant, deleteRestaurant } from "../controller/restaurants.controller"; +import { addOrder, updateOrder, deleteOrder } from "../controller/orders.controller"; const router = express.Router(); @@ -9,8 +9,8 @@ router.get("/", (request: Request, response: Response) => { }); router.post("/test1", sayHello); -router.post("/addtest/", addRestaurant); -router.put("/updatetest/:id", updateRestaurant); -router.delete("/deletetest/:id", deleteRestaurant); +router.post("/addtest/", addOrder); +router.put("/updatetest/:id", updateOrder); +router.delete("/deletetest/:id", deleteOrder); module.exports = router;