Compare commits

..

No commits in common. "4c25aa65febf03d6f9a1963b7339e8adae31d310" and "5723e3516c4c23851f871961dbca940baf7a9d58" have entirely different histories.

4 changed files with 8 additions and 54 deletions

View file

@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { Cart, ICart, Order, Product } from '../mongoose/Schema';
import { Cart, ICart, Order } from '../mongoose/Schema';
import { sendEmailasync } from '../services/sendGrid';
import { config } from 'dotenv';
@ -8,13 +8,8 @@ config();
export async function addToCart(req: Request, res: Response) {
try {
const { userId, productId } = req.body;
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.' });
if (!productId) {
res.status(400).json({ error: 'Product id is required.' });
return;
}
let cart: ICart | null = await Cart.findOne({ userId });
@ -33,9 +28,7 @@ export async function addToCart(req: Request, res: Response) {
cart.markModified('products');
}
await cart.save();
res.status(200).json({
products: cart.products,
});
res.status(200).json(cart);
} 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.' });
@ -57,17 +50,6 @@ 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;
@ -90,5 +72,4 @@ export async function checkout(req: Request, res: Response) {
async function removeCart(userId: string) {
await Cart.deleteOne({ userId });
}
}

View file

@ -84,28 +84,4 @@ export async function logout(req: Request, res: Response) {
console.error('Error during logout:', error);
res.status(500).json({ error: 'An error occurred during logout' });
}
}
export async function getAllUsers(req: Request, res: Response) {
try {
const users = await User.find().select('-__v -password');
res.status(200).json({ users });
} catch (error) {
console.error('Error getting all users:', error);
res.status(500).json({ error: 'An error occurred while getting all users' });
}
}
export async function deleteUser(req: Request, res: Response) {
try {
const { id } = req.params;
const user = await User.findByIdAndDelete(id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.status(200).json({ message: 'User deleted successfully' });
} catch (error) {
console.error('Error deleting user:', error);
res.status(500).json({ error: 'An error occurred while deleting the user' });
}
}

View file

@ -1,6 +1,6 @@
import express from 'express';
import { authenticateToken } from '../middlewares/checkAuth';
import { addToCart, listCart, checkout, clearCart } from '../controllers/CartController';
import { addToCart, listCart, checkout } from '../controllers/CartController';
@ -10,6 +10,5 @@ cartRouter.post('/', authenticateToken, addToCart);
cartRouter.get('/', authenticateToken, listCart);
cartRouter.post('/checkout', authenticateToken, checkout);
cartRouter.delete('/', authenticateToken, clearCart)
export default cartRouter;

View file

@ -1,12 +1,10 @@
import express from 'express';
import { createUser, login, logout, getAllUsers, deleteUser } from '../controllers/UserController';
import { createUser, login, logout } from '../controllers/UserController';
const userRouter = express.Router();
userRouter.post('/', createUser);
userRouter.post('/login', login);
userRouter.post('/logout', logout);
userRouter.get('/all', getAllUsers);
userRouter.delete('/:id', deleteUser)
export default userRouter;