Compare commits

...

2 commits

Author SHA1 Message Date
4c25aa65fe improving responses + added clear cart 2023-06-14 14:41:02 +03:00
68d0c2600f deleteUser + get all routes 2023-06-14 14:32:16 +03:00
4 changed files with 54 additions and 8 deletions

View file

@ -1,5 +1,5 @@
import { Request, Response } from 'express'; 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 { sendEmailasync } from '../services/sendGrid';
import { config } from 'dotenv'; import { config } from 'dotenv';
@ -8,8 +8,13 @@ config();
export async function addToCart(req: Request, res: Response) { export async function addToCart(req: Request, res: Response) {
try { try {
const { userId, productId } = req.body; const { userId, productId } = req.body;
if (!productId) { if (!productId || productId.length !== 24) {
res.status(400).json({ error: 'Product id is required.' }); 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; return;
} }
let cart: ICart | null = await Cart.findOne({ userId }); let cart: ICart | null = await Cart.findOne({ userId });
@ -28,7 +33,9 @@ export async function addToCart(req: Request, res: Response) {
cart.markModified('products'); cart.markModified('products');
} }
await cart.save(); await cart.save();
res.status(200).json(cart); res.status(200).json({
products: cart.products,
});
} catch (error) { } catch (error) {
console.error('Error adding product to cart:', error); console.error('Error adding product to cart:', error);
res.status(500).json({ error: 'An error occurred while adding the product to the cart.' }); 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) { export async function checkout(req: Request, res: Response) {
const { userId } = req.body; const { userId } = req.body;
@ -72,4 +90,5 @@ export async function checkout(req: Request, res: Response) {
async function removeCart(userId: string) { async function removeCart(userId: string) {
await Cart.deleteOne({ userId }); await Cart.deleteOne({ userId });
} }

View file

@ -84,4 +84,28 @@ export async function logout(req: Request, res: Response) {
console.error('Error during logout:', error); console.error('Error during logout:', error);
res.status(500).json({ error: 'An error occurred during logout' }); 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 express from 'express';
import { authenticateToken } from '../middlewares/checkAuth'; 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.get('/', authenticateToken, listCart);
cartRouter.post('/checkout', authenticateToken, checkout); cartRouter.post('/checkout', authenticateToken, checkout);
cartRouter.delete('/', authenticateToken, clearCart)
export default cartRouter; export default cartRouter;

View file

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