Removed unused modules + error handling
This commit is contained in:
parent
ca14c2ec4f
commit
84af08450e
6 changed files with 14 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import { Cart, ICart, Product, Order, IOrder } 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';
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,13 @@ export async function createProduct(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const { name, description, price, userId } = req.body;
|
const { name, description, price, userId } = req.body;
|
||||||
if(!name || !description || !price || !userId) {
|
if(!name || !description || !price || !userId) {
|
||||||
res.status(400).json({ error: 'Name, description, price and user id are required.' });
|
res.status(400).json({ error: 'Name, description, price are required.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const productExists = await Product.exists({ name, userId });
|
||||||
|
if(productExists) {
|
||||||
|
res.status(400).json({ error: 'Product already exists.' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const product: IProduct = await Product.create({
|
const product: IProduct = await Product.create({
|
||||||
|
|
|
@ -14,10 +14,11 @@ export async function createUser(req: Request, res: Response) {
|
||||||
// checkIfUserExists return true if the user exists
|
// checkIfUserExists return true if the user exists
|
||||||
const userExists = await User.exists({ email });
|
const userExists = await User.exists({ email });
|
||||||
if(userExists) {
|
if(userExists) {
|
||||||
return res.status(400).json({ error: 'User already exists' });
|
return res.status(400).json({ error: 'User already exists, Try login :)' });
|
||||||
}
|
}
|
||||||
|
|
||||||
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({
|
||||||
firstName,
|
firstName,
|
||||||
lastName,
|
lastName,
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import express, { Request, Response, NextFunction } from 'express';
|
import { Request, Response, NextFunction } from 'express';
|
||||||
import jwt, { JwtPayload } from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import cookieParser from 'cookie-parser';
|
|
||||||
|
|
||||||
interface AuthenticatedRequest extends Request {
|
interface AuthenticatedRequest extends Request {
|
||||||
userId?: string;
|
userId?: string;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import express, { Request } 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 } from '../controllers/CartController';
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import express, { Request } from 'express';
|
import express from 'express';
|
||||||
import { authenticateToken } from '../middlewares/checkAuth';
|
import { authenticateToken } from '../middlewares/checkAuth';
|
||||||
import { createProduct, listProducts } from '../controllers/ProductController';
|
import { createProduct, listProducts } from '../controllers/ProductController';
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue