mirror of
https://github.com/GithubOrgProjectPiza/server.git
synced 2026-09-04 08:36:10 +02:00
Add restaurant and organization controller
- Add restaurant controller - Add organization controller - Add files for types
This commit is contained in:
@@ -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 addOrganization = async (req: Request, res: Response) => {
|
||||
if (typeof req.body.name !== "string" || typeof req.body.domain !== "string") {
|
||||
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
||||
name: DataType.STRING,
|
||||
description: DataType.STRING,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const organization = await prisma.organization.create({
|
||||
data: {
|
||||
name: req.body.name,
|
||||
domain: req.body.domain
|
||||
}
|
||||
});
|
||||
|
||||
res.status(HttpStatusCodes.CREATED).json(organization);
|
||||
};
|
||||
|
||||
export const updateOrganization = 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.domain !== "string") {
|
||||
res.status(HttpStatusCodes.BAD_REQUEST).json({
|
||||
name: DataType.STRING,
|
||||
description: DataType.STRING,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const organization = await prisma.organization.update({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
data: {
|
||||
name: req.body.name,
|
||||
domain: req.body.domain
|
||||
},
|
||||
});
|
||||
|
||||
res.status(HttpStatusCodes.OK).json(organization);
|
||||
};
|
||||
|
||||
export const deleteRestaurant = 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.organization.delete({
|
||||
where: {
|
||||
id: id,
|
||||
}
|
||||
});
|
||||
|
||||
res.status(HttpStatusCodes.NO_CONTENT).json();
|
||||
};
|
||||
|
||||
export const getOrganization = 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 organization = await prisma.restaurant.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
}
|
||||
});
|
||||
|
||||
res.status(HttpStatusCodes.OK).json(organization);
|
||||
};
|
||||
@@ -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 addRestaurant = 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 restaurant = await prisma.restaurant.create({
|
||||
data: {
|
||||
name: req.body.name,
|
||||
description: req.body.description,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(HttpStatusCodes.CREATED).json(restaurant);
|
||||
};
|
||||
|
||||
export const updateRestaurant = 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 restaurant = await prisma.restaurant.update({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
data: {
|
||||
name: req.body.name,
|
||||
description: req.body.description,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(HttpStatusCodes.OK).json(restaurant);
|
||||
};
|
||||
|
||||
export const deleteRestaurant = 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.restaurant.delete({
|
||||
where: {
|
||||
id: id,
|
||||
}
|
||||
});
|
||||
|
||||
res.status(HttpStatusCodes.NO_CONTENT).json();
|
||||
};
|
||||
|
||||
export const getRestaurant = 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 restaurant = await prisma.restaurant.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
}
|
||||
});
|
||||
|
||||
res.status(HttpStatusCodes.OK).json(restaurant);
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
export const DataType = {
|
||||
STRING: "String",
|
||||
NUMBER: "Number"
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import express, { Request, Response } from "express";
|
||||
import { sayHello } from "../controller/testcontroller.controller";
|
||||
import { addRestaurant } from "../controller/restaurants.controller";
|
||||
import { addRestaurant, updateRestaurant, deleteRestaurant } from "../controller/restaurants.controller";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -9,6 +9,8 @@ router.get("/", (request: Request, response: Response) => {
|
||||
});
|
||||
|
||||
router.post("/test1", sayHello);
|
||||
router.post("/test2", addRestaurant);
|
||||
router.post("/addtest/", addRestaurant);
|
||||
router.put("/updatetest/:id", updateRestaurant);
|
||||
router.delete("/deletetest/:id", deleteRestaurant);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
export const HttpStatusCodes = {
|
||||
OK: 200,
|
||||
CREATED: 201,
|
||||
MODIFIED: 202,
|
||||
NO_CONTENT: 204,
|
||||
BAD_REQUEST: 400,
|
||||
NOT_FOUND: 404,
|
||||
INTERNAL_SERVER: 500,
|
||||
};
|
||||
Reference in New Issue
Block a user