improving responses + added clear cart
This commit is contained in:
parent
68d0c2600f
commit
4c25aa65fe
2 changed files with 26 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { Cart, ICart, Order } from '../mongoose/Schema';
|
||||
import { Cart, ICart, Order, Product } from '../mongoose/Schema';
|
||||
import { sendEmailasync } from '../services/sendGrid';
|
||||
import { config } from 'dotenv';
|
||||
|
||||
|
@ -8,8 +8,13 @@ config();
|
|||
export async function addToCart(req: Request, res: Response) {
|
||||
try {
|
||||
const { userId, productId } = req.body;
|
||||
if (!productId) {
|
||||
res.status(400).json({ error: 'Product id is required.' });
|
||||
if (!productId || productId.length !== 24) {
|
||||
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 });
|
||||
|
@ -28,7 +33,9 @@ export async function addToCart(req: Request, res: Response) {
|
|||
cart.markModified('products');
|
||||
}
|
||||
await cart.save();
|
||||
res.status(200).json(cart);
|
||||
res.status(200).json({
|
||||
products: cart.products,
|
||||
});
|
||||
} 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.' });
|
||||
|
@ -50,6 +57,17 @@ export async function listCart(req: Request, res: Response) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function clearCart(req: Request, res: Response) {
|
||||
try {
|
||||
const { userId } = req.body;
|
||||
await removeCart(userId);
|
||||
res.status(200).json({ message: 'Cart cleared successfully' });
|
||||
} catch (error) {
|
||||
console.error('Error clearing cart:', error);
|
||||
res.status(500).json({ error: 'An error occurred while clearing the cart.' });
|
||||
}
|
||||
}
|
||||
|
||||
export async function checkout(req: Request, res: Response) {
|
||||
|
||||
const { userId } = req.body;
|
||||
|
@ -72,4 +90,5 @@ export async function checkout(req: Request, res: Response) {
|
|||
|
||||
async function removeCart(userId: string) {
|
||||
await Cart.deleteOne({ userId });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import express from 'express';
|
||||
import { authenticateToken } from '../middlewares/checkAuth';
|
||||
import { addToCart, listCart, checkout } from '../controllers/CartController';
|
||||
import { addToCart, listCart, checkout, clearCart } from '../controllers/CartController';
|
||||
|
||||
|
||||
|
||||
|
@ -10,5 +10,6 @@ cartRouter.post('/', authenticateToken, addToCart);
|
|||
cartRouter.get('/', authenticateToken, listCart);
|
||||
|
||||
cartRouter.post('/checkout', authenticateToken, checkout);
|
||||
cartRouter.delete('/', authenticateToken, clearCart)
|
||||
|
||||
export default cartRouter;
|
Loading…
Reference in a new issue