Compare commits
No commits in common. "4c25aa65febf03d6f9a1963b7339e8adae31d310" and "5723e3516c4c23851f871961dbca940baf7a9d58" have entirely different histories.
4c25aa65fe
...
5723e3516c
4 changed files with 8 additions and 54 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { Request, Response } from 'express';
|
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 { sendEmailasync } from '../services/sendGrid';
|
||||||
import { config } from 'dotenv';
|
import { config } from 'dotenv';
|
||||||
|
|
||||||
|
@ -8,13 +8,8 @@ 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 || productId.length !== 24) {
|
if (!productId) {
|
||||||
res.status(400).json({ error: 'A Valid Product id is required.' });
|
res.status(400).json({ error: '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 });
|
||||||
|
@ -33,9 +28,7 @@ export async function addToCart(req: Request, res: Response) {
|
||||||
cart.markModified('products');
|
cart.markModified('products');
|
||||||
}
|
}
|
||||||
await cart.save();
|
await cart.save();
|
||||||
res.status(200).json({
|
res.status(200).json(cart);
|
||||||
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.' });
|
||||||
|
@ -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) {
|
export async function checkout(req: Request, res: Response) {
|
||||||
|
|
||||||
const { userId } = req.body;
|
const { userId } = req.body;
|
||||||
|
@ -91,4 +73,3 @@ 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 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,27 +85,3 @@ export async function logout(req: Request, res: Response) {
|
||||||
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' });
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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, clearCart } from '../controllers/CartController';
|
import { addToCart, listCart, checkout } from '../controllers/CartController';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,6 +10,5 @@ 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;
|
|
@ -1,12 +1,10 @@
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { createUser, login, logout, getAllUsers, deleteUser } from '../controllers/UserController';
|
import { createUser, login, logout } 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;
|
Loading…
Reference in a new issue