mirror of
https://github.com/GithubOrgProjectPiza/server.git
synced 2026-09-04 08:36:10 +02:00
Fix orders and pizzas controller
This commit is contained in:
@@ -4,12 +4,10 @@ import { DataType } from "../datatypes";
|
|||||||
import prisma from "../lib/prisma.lib";
|
import prisma from "../lib/prisma.lib";
|
||||||
|
|
||||||
export const addOrder = async (req: Request, res: Response) => {
|
export const addOrder = async (req: Request, res: Response) => {
|
||||||
const userId = Number(req.body.userId);
|
if (typeof req.body.userId !== "number" || !Array.isArray(req.body.pizzas)) {
|
||||||
|
|
||||||
if (Number.isNaN(userId)) {
|
|
||||||
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
||||||
userId: DataType.NUMBER,
|
userId: DataType.NUMBER,
|
||||||
//???
|
pizzas: DataType.ARRAY,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -19,8 +17,8 @@ console.log(req.body.pizzas);
|
|||||||
const order = await prisma.order.create({
|
const order = await prisma.order.create({
|
||||||
data: {
|
data: {
|
||||||
userId: req.body.userId,
|
userId: req.body.userId,
|
||||||
//???
|
pizzas: req.body.pizzas,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(HttpStatusCodes.CREATED).json(order);
|
res.status(HttpStatusCodes.CREATED).json(order);
|
||||||
@@ -33,10 +31,10 @@ export const updateOrder = async (req: Request, res: Response) => {
|
|||||||
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
||||||
id: DataType.NUMBER,
|
id: DataType.NUMBER,
|
||||||
});
|
});
|
||||||
} else if (typeof req.body.userId !== "string") {
|
} else if (typeof req.body.userId !== "string" || !Array.isArray(req.body.pizzas)) {
|
||||||
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
||||||
name: DataType.STRING,
|
name: DataType.STRING,
|
||||||
//???
|
pizzas: DataType.ARRAY,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -47,7 +45,7 @@ export const updateOrder = async (req: Request, res: Response) => {
|
|||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
userId: req.body.userId,
|
userId: req.body.userId,
|
||||||
//???
|
pizzas: req.body.pizzas,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -66,7 +64,7 @@ export const deleteOrder = async (req: Request, res: Response) => {
|
|||||||
await prisma.order.delete({
|
await prisma.order.delete({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(HttpStatusCodes.NO_CONTENT).json();
|
res.status(HttpStatusCodes.NO_CONTENT).json();
|
||||||
@@ -84,7 +82,7 @@ export const getOrder = async (req: Request, res: Response) => {
|
|||||||
const order = await prisma.restaurant.findUnique({
|
const order = await prisma.restaurant.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(HttpStatusCodes.OK).json(order);
|
res.status(HttpStatusCodes.OK).json(order);
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ import { DataType } from "../datatypes";
|
|||||||
import prisma from "../lib/prisma.lib";
|
import prisma from "../lib/prisma.lib";
|
||||||
|
|
||||||
export const addPizza = async (req: Request, res: Response) => {
|
export const addPizza = async (req: Request, res: Response) => {
|
||||||
if (typeof req.body.name !== "string" || typeof req.body.description !== "string") {
|
if (typeof req.body.name !== "string" || typeof req.body.description !== "string" || typeof req.body.restaurantId !== "number") {
|
||||||
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
||||||
name: DataType.STRING,
|
name: DataType.STRING,
|
||||||
description: DataType.STRING,
|
description: DataType.STRING,
|
||||||
|
restaurantId: DataType.NUMBER
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -15,7 +16,8 @@ export const addPizza = async (req: Request, res: Response) => {
|
|||||||
const pizza = await prisma.pizza.create({
|
const pizza = await prisma.pizza.create({
|
||||||
data: {
|
data: {
|
||||||
name: req.body.name,
|
name: req.body.name,
|
||||||
restaurantId: -1
|
description: req.body.description,
|
||||||
|
restaurantId: req.body.restaurantId
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -29,10 +31,11 @@ export const updatePizza = async (req: Request, res: Response) => {
|
|||||||
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
||||||
id: DataType.NUMBER,
|
id: DataType.NUMBER,
|
||||||
});
|
});
|
||||||
} else if (typeof req.body.name !== "string" || typeof req.body.description !== "string") {
|
} else if (typeof req.body.name !== "string" || typeof req.body.description !== "string" || typeof req.body.restaurantId !== "number") {
|
||||||
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
||||||
name: DataType.STRING,
|
name: DataType.STRING,
|
||||||
description: DataType.STRING,
|
description: DataType.STRING,
|
||||||
|
restaurantId: DataType.NUMBER
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
export const DataType = {
|
export const DataType = {
|
||||||
STRING: "String",
|
STRING: "String",
|
||||||
NUMBER: "Number"
|
NUMBER: "Number",
|
||||||
|
ARRAY: "Array",
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user