From 9d13727f3e55ede5677a72a07a57142ead578af3 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Thu, 15 Jun 2023 10:00:22 +0300 Subject: [PATCH] renaming router --- package-lock.json | 18 +++++++++++++++++- package.json | 4 +++- src/index.ts | 6 +++--- src/middlewares/checkUuid.ts | 11 +++++++++++ src/routes/{cart.ts => cartRoute.ts} | 7 ++++--- src/routes/{product.ts => productRouter.ts} | 2 +- src/routes/{user.ts => userRouter.ts} | 2 +- 7 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/middlewares/checkUuid.ts rename src/routes/{cart.ts => cartRoute.ts} (61%) rename src/routes/{product.ts => productRouter.ts} (92%) rename src/routes/{user.ts => userRouter.ts} (90%) diff --git a/package-lock.json b/package-lock.json index d35178c..a2a8c04 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,13 +17,15 @@ "express": "^4.18.2", "jsonwebtoken": "^9.0.0", "mongodb": "^5.6.0", - "mongoose": "^7.2.2" + "mongoose": "^7.2.2", + "uuid": "^9.0.0" }, "devDependencies": { "@types/bcryptjs": "^2.4.2", "@types/cookie-parser": "^1.4.3", "@types/express": "^4.17.17", "@types/jsonwebtoken": "^9.0.2", + "@types/uuid": "^9.0.2", "nodemon": "^2.0.22" } }, @@ -177,6 +179,12 @@ "@types/node": "*" } }, + "node_modules/@types/uuid": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.2.tgz", + "integrity": "sha512-kNnC1GFBLuhImSnV7w4njQkUiJi0ZXUycu1rUaouPqiKlXkh77JKgdRnTAp1x5eBwcIwbtI+3otwzuIDEuDoxQ==", + "dev": true + }, "node_modules/@types/webidl-conversions": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.0.tgz", @@ -1551,6 +1559,14 @@ "node": ">= 0.4.0" } }, + "node_modules/uuid": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", + "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index 815fdef..6ccca66 100644 --- a/package.json +++ b/package.json @@ -19,13 +19,15 @@ "express": "^4.18.2", "jsonwebtoken": "^9.0.0", "mongodb": "^5.6.0", - "mongoose": "^7.2.2" + "mongoose": "^7.2.2", + "uuid": "^9.0.0" }, "devDependencies": { "@types/bcryptjs": "^2.4.2", "@types/cookie-parser": "^1.4.3", "@types/express": "^4.17.17", "@types/jsonwebtoken": "^9.0.2", + "@types/uuid": "^9.0.2", "nodemon": "^2.0.22" } } diff --git a/src/index.ts b/src/index.ts index ec1dc24..56407e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,9 +2,9 @@ import express from 'express'; import mongoose from 'mongoose'; import cookieParser from 'cookie-parser'; -import userRouter from './routes/user'; -import productRouter from './routes/product'; -import cartRouter from './routes/cart'; +import userRouter from './routes/userRouter'; +import productRouter from './routes/productRouter'; +import cartRouter from './routes/cartRoute'; const env = require('dotenv').config().parsed; diff --git a/src/middlewares/checkUuid.ts b/src/middlewares/checkUuid.ts new file mode 100644 index 0000000..06e1095 --- /dev/null +++ b/src/middlewares/checkUuid.ts @@ -0,0 +1,11 @@ +import { v4 as uuidv4, validate as validateUUID } from 'uuid'; +import { Request, Response, NextFunction } from 'express'; + +export function checkUuid(req: Request, res: Response, next: NextFunction) { + const { userId } = req.body; + if (!validateUUID(userId)) { + res.status(400).json({ error: 'Invalid user id' }); + return; + } + next(); +} diff --git a/src/routes/cart.ts b/src/routes/cartRoute.ts similarity index 61% rename from src/routes/cart.ts rename to src/routes/cartRoute.ts index ad3e7b0..2349da4 100644 --- a/src/routes/cart.ts +++ b/src/routes/cartRoute.ts @@ -1,13 +1,14 @@ import express from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; -import { addToCart, listCart, checkout, clearCart } from '../controllers/CartController'; +import { addToCart, listCart, checkout, clearCart } from '../controllers/cartController'; +import { checkUuid } from '../middlewares/checkUuid'; const cartRouter = express.Router(); -cartRouter.post('/', authenticateToken, addToCart); -cartRouter.get('/', authenticateToken, listCart); +cartRouter.post('/', [authenticateToken, checkUuid], addToCart); +cartRouter.get('/', [authenticateToken, checkUuid], listCart); cartRouter.post('/checkout', authenticateToken, checkout); cartRouter.delete('/', authenticateToken, clearCart) diff --git a/src/routes/product.ts b/src/routes/productRouter.ts similarity index 92% rename from src/routes/product.ts rename to src/routes/productRouter.ts index af0eb74..573427f 100644 --- a/src/routes/product.ts +++ b/src/routes/productRouter.ts @@ -1,6 +1,6 @@ import express from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; -import { createProduct, listProducts, getProduct } from '../controllers/ProductController'; +import { createProduct, listProducts, getProduct } from '../controllers/productController'; const productRouter = express.Router(); diff --git a/src/routes/user.ts b/src/routes/userRouter.ts similarity index 90% rename from src/routes/user.ts rename to src/routes/userRouter.ts index 671a81d..51bf3b9 100644 --- a/src/routes/user.ts +++ b/src/routes/userRouter.ts @@ -1,5 +1,5 @@ import express from 'express'; -import { createUser, login, logout, getAllUsers, deleteUser } from '../controllers/UserController'; +import { createUser, login, logout, getAllUsers, deleteUser } from '../controllers/userController'; const userRouter = express.Router();