small stuff
This commit is contained in:
parent
4e1ace0586
commit
ca14c2ec4f
5 changed files with 13 additions and 10 deletions
|
@ -55,7 +55,7 @@ export async function checkout(req: Request, res: Response) {
|
||||||
|
|
||||||
const { userId } = req.body;
|
const { userId } = req.body;
|
||||||
const usersCart = await Cart.findOne({ userId });
|
const usersCart = await Cart.findOne({ userId });
|
||||||
console.log(usersCart)
|
|
||||||
if (!usersCart) {
|
if (!usersCart) {
|
||||||
res.status(404).json({ error: 'Cart not found.' });
|
res.status(404).json({ error: 'Cart not found.' });
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -25,10 +25,8 @@ export async function createProduct(req: Request, res: Response) {
|
||||||
export async function listProducts(req: Request, res: Response) {
|
export async function listProducts(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const { page, limit } = req.query;
|
const { page, limit } = req.query;
|
||||||
const products = await Product.find()
|
const dbLimit = Number(limit) || 50;
|
||||||
.sort({ price: 1 })
|
const products = await Product.find().sort({ price: 1 }).skip(Number(page) * Number(dbLimit)).limit(Number(dbLimit));
|
||||||
.skip(Number(page) * Number(limit))
|
|
||||||
.limit(Number(limit));
|
|
||||||
|
|
||||||
res.json(products);
|
res.json(products);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -11,6 +11,11 @@ export async function createUser(req: Request, res: Response) {
|
||||||
if (!(email && password && firstName && lastName && address)) {
|
if (!(email && password && firstName && lastName && address)) {
|
||||||
return res.status(400).json({ error: 'All inputs are required' });
|
return res.status(400).json({ error: 'All inputs are required' });
|
||||||
}
|
}
|
||||||
|
// checkIfUserExists return true if the user exists
|
||||||
|
const userExists = await User.exists({ email });
|
||||||
|
if(userExists) {
|
||||||
|
return res.status(400).json({ error: 'User already exists' });
|
||||||
|
}
|
||||||
|
|
||||||
const hashedPassword = await bcrypt.hash(password, 10);
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
const user: IUser = await User.create({
|
const user: IUser = await User.create({
|
||||||
|
@ -21,7 +26,9 @@ export async function createUser(req: Request, res: Response) {
|
||||||
address,
|
address,
|
||||||
});
|
});
|
||||||
|
|
||||||
res.json(user);
|
res.status(200).json({
|
||||||
|
massage: 'User created successfully'
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating user:', error);
|
console.error('Error creating user:', error);
|
||||||
res.status(500).json({ error: 'An error occurred while creating the user.' });
|
res.status(500).json({ error: 'An error occurred while creating the user.' });
|
||||||
|
@ -54,7 +61,7 @@ export async function login(req: Request, res: Response) {
|
||||||
|
|
||||||
// Send the JWT as the response
|
// Send the JWT as the response
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
username: user.firstName
|
token
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error during login:', error);
|
console.error('Error during login:', error);
|
||||||
|
|
|
@ -14,7 +14,7 @@ export function authenticateToken(req: AuthenticatedRequest, res: Response, next
|
||||||
return res.status(401).json({ error: 'Unauthorized' });
|
return res.status(401).json({ error: 'Unauthorized' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const user_id = jwt.verify(token, process.env.JWT_SECRET as string, (err, decoded) => {
|
jwt.verify(token, process.env.JWT_SECRET as string, (err: any, decoded: { userId: any; }) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return res.status(401).json({ error: 'In Valid Token' });
|
return res.status(401).json({ error: 'In Valid Token' });
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,6 @@ import express, { Request } from 'express';
|
||||||
import { authenticateToken } from '../middlewares/checkAuth';
|
import { authenticateToken } from '../middlewares/checkAuth';
|
||||||
import { createProduct, listProducts } from '../controllers/ProductController';
|
import { createProduct, listProducts } from '../controllers/ProductController';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const productRouter = express.Router();
|
const productRouter = express.Router();
|
||||||
|
|
||||||
productRouter.post('/', authenticateToken, createProduct)
|
productRouter.post('/', authenticateToken, createProduct)
|
||||||
|
|
Loading…
Reference in a new issue