From e2852b6b90d35b8b9486599cce90cdb768b712b5 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Mon, 26 Jun 2023 09:28:32 +0300 Subject: [PATCH] noImplicitAny modifications --- src/controllers/CartController.ts | 39 +++++++++++++++++----------- src/controllers/ProductController.ts | 4 +-- src/controllers/UserController.ts | 12 ++++----- src/index.ts | 3 --- src/middlewares/errorHandler.ts | 15 ----------- src/models/productModel.ts | 16 ++++++++++-- src/routes/userRouter.ts | 2 +- tsconfig.json | 1 + 8 files changed, 48 insertions(+), 44 deletions(-) diff --git a/src/controllers/CartController.ts b/src/controllers/CartController.ts index 16f3135..b66d75b 100644 --- a/src/controllers/CartController.ts +++ b/src/controllers/CartController.ts @@ -14,26 +14,23 @@ export async function addToCart(req: Request, res: Response) { res.status(400).json({ error: 'A Valid Product id is required.' }); return; } - const isProductExists = await Product.exists({ _id: productId }); - if (!isProductExists) { - res.status(404).json({ error: 'Product not found.' }); - return; - } - let cart: ICart | null = await Cart.findOne({ userId }); + + let [product, cart] = await getProductAndCart(productId, userId); + console.log(`product: ${product}, cart: ${cart}`); + if (!cart) { cart = await Cart.create({ userId, - products: { [productId]: 1 }, + products: {}, }); - } else { - const currentQuantity = cart.products[productId]; - if (currentQuantity === undefined) { - cart.products[productId] = 1; - } else { - cart.products[productId] += 1; - } - cart.markModified('products'); } + const currentQuantity = cart.products[productId]; + if (currentQuantity === undefined) { + cart.products[productId] = 1; + } else { + cart.products[productId] += 1; + } + cart.markModified('products'); await cart.save(); res.status(200).json({ products: cart.products, @@ -44,6 +41,18 @@ export async function addToCart(req: Request, res: Response) { } } +async function getProductAndCart(productId: string, userId: string) { + try { + return Promise.all([ + Product.findById(productId), + Cart.findOne({ userId }), + ]) + } catch (error) { + console.error('Error adding product to cart:', error); + // res.status(500).json({ error: 'An error occurred while adding the product to the cart.' }); + } +} + export async function listCart(req: Request, res: Response) { try { const { userId } = req.body; diff --git a/src/controllers/ProductController.ts b/src/controllers/ProductController.ts index 473e6d8..8ead88d 100644 --- a/src/controllers/ProductController.ts +++ b/src/controllers/ProductController.ts @@ -1,9 +1,9 @@ -import { Request, Response } from 'express'; +import { Request, Response, NextFunction} from 'express'; import { Product } from '../schemas/productSchema'; import { createProduct, getAllProducts, getOne } from '../models/productModel'; import { ApiError } from '../utils/ApiError'; -const create = async (req: Request, res: Response, next) => { +const create = async (req: Request, res: Response, next: NextFunction) => { try { const { name, description, price } = req.body; const product = await createProduct({ diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 407f1f4..1f01c4e 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -1,10 +1,10 @@ -import { Request, Response } from 'express'; +import { Request, Response, NextFunction } from 'express'; import { createUser, loginUser, getAllUsers, deleteUser } from '../models/userModel'; import { ApiError } from '../utils/ApiError'; import jwt from 'jsonwebtoken'; import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth'; -const create = async (req: Request, res: Response, next) => { +const create = async (req: Request, res: Response, next: NextFunction) => { try { const { email, password, address } = req.body; const user = await createUser({ @@ -24,7 +24,7 @@ const create = async (req: Request, res: Response, next) => { } } -const login = async (req: Request, res: Response, next) => { +const login = async (req: Request, res: Response, next: NextFunction) => { try { const { email, password } = req.body; const user: any = await loginUser({ @@ -53,7 +53,7 @@ const login = async (req: Request, res: Response, next) => { } } -const logout = async (req: Request, res: Response, next) => { +const logout = async (req: Request, res: Response, next: NextFunction) => { try { clearJwtCookie(res); res.status(200).json({ message: 'Logout successful' }); @@ -65,7 +65,7 @@ const logout = async (req: Request, res: Response, next) => { } } -const getAll = async (req: Request, res: Response, next) => { +const getAll = async (req: Request, res: Response, next: NextFunction) => { try { const users = await getAllUsers(); res.status(200).json(users); @@ -77,7 +77,7 @@ const getAll = async (req: Request, res: Response, next) => { } } -const deleteHandler = async (req: Request, res: Response, next) => { +const deleteHandler = async (req: Request, res: Response, next: NextFunction) => { try { const { id } = req.params; const user = await deleteUser(id); diff --git a/src/index.ts b/src/index.ts index 1f63eaf..7ae82c4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,6 @@ import userRouter from './routes/userRouter'; import productRouter from './routes/productRouter'; import cartRouter from './routes/cartRouter'; -import { errorHandler } from './middlewares/errorHandler'; import { ApiError } from './utils/ApiError'; @@ -47,8 +46,6 @@ app.all('*', (req, res, next) => { next(error) }); -app.use(errorHandler); - // Start server app.listen(PORT, () => { diff --git a/src/middlewares/errorHandler.ts b/src/middlewares/errorHandler.ts index 5aca5bf..e69de29 100644 --- a/src/middlewares/errorHandler.ts +++ b/src/middlewares/errorHandler.ts @@ -1,15 +0,0 @@ -const errorHandler = (error, req, res, next) => { - error.statusCode = error.statusCode || 500; - error.message = error.message || 'Internal server error'; - error.status = error.status || 'error'; - res.status(error.statusCode).json({ - status: error.status, - statusCode: error.statusCode, - message: error.message - }); - -} - -export { - errorHandler -} \ No newline at end of file diff --git a/src/models/productModel.ts b/src/models/productModel.ts index c582875..9619818 100644 --- a/src/models/productModel.ts +++ b/src/models/productModel.ts @@ -1,10 +1,9 @@ import { Product } from '../schemas/productSchema'; import { Request, Response } from 'express'; -import { errorHandler } from '../middlewares/errorHandler'; import { ApiError } from '../utils/ApiError'; const createProduct = async (product: any) => { - try { + try { const newProduct = new Product(product); const isExist = await Product.findOne({ name: product.name, userId: product.userId }); @@ -54,6 +53,19 @@ const getOne = async (id: string) => { } } +const removeProduct = async (id: string) => { + try { + const product = await Product.findByIdAndDelete(id); + return product; + } catch { + const error = new ApiError('Product not found'); + error.statusCode = 404; + error.status = 'fail'; + return error; + } +} + + export { createProduct, diff --git a/src/routes/userRouter.ts b/src/routes/userRouter.ts index bbfd01a..15bee12 100644 --- a/src/routes/userRouter.ts +++ b/src/routes/userRouter.ts @@ -9,6 +9,6 @@ userRouter.post('/', isValidCreateUser, create); userRouter.get('/', getAll); userRouter.post('/login', isValidLogin, login); userRouter.post('/logout', logout); -userRouter.delete('/:id', authenticateToken ,deleteHandler) +userRouter.delete('/:id', authenticateToken, deleteHandler) export default userRouter; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index e6fc176..64c2ec2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,5 +9,6 @@ "sourceMap": true, "outDir": "./dist", "rootDir": "./src", + "noImplicitAny": true, } } \ No newline at end of file