mirror of
https://github.com/GithubOrgProjectPiza/server.git
synced 2026-09-04 08:36:10 +02:00
Add pizza and orders controller
_Stiifi hüf ma des zu fixn_
This commit is contained in:
@@ -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);
|
||||||
|
};
|
||||||
@@ -50,7 +50,7 @@ export const updateOrganization = async (req: Request, res: Response) => {
|
|||||||
res.status(HttpStatusCodes.OK).json(organization);
|
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);
|
const id = Number(req.params.id);
|
||||||
|
|
||||||
if (Number.isNaN(id)) {
|
if (Number.isNaN(id)) {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
};
|
||||||
@@ -79,7 +79,7 @@ export const getRestaurant = async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
const restaurant = await prisma.restaurant.findUnique({
|
const restaurant = await prisma.restaurant.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
import { sayHello } from "../controller/testcontroller.controller";
|
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();
|
const router = express.Router();
|
||||||
|
|
||||||
@@ -9,8 +9,8 @@ router.get("/", (request: Request, response: Response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post("/test1", sayHello);
|
router.post("/test1", sayHello);
|
||||||
router.post("/addtest/", addRestaurant);
|
router.post("/addtest/", addOrder);
|
||||||
router.put("/updatetest/:id", updateRestaurant);
|
router.put("/updatetest/:id", updateOrder);
|
||||||
router.delete("/deletetest/:id", deleteRestaurant);
|
router.delete("/deletetest/:id", deleteOrder);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user