done adding new product
This commit is contained in:
parent
da17d8cebf
commit
2baa649390
5 changed files with 39 additions and 10 deletions
21
src/controllers/ProductController.ts
Normal file
21
src/controllers/ProductController.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { Product, IProduct } from '../models/Schema';
|
||||
|
||||
export async function createProduct(req: Request, res: Response) {
|
||||
try {
|
||||
const { name, description, price, userId } = req.body;
|
||||
const product: IProduct = await Product.create({
|
||||
name,
|
||||
description,
|
||||
price,
|
||||
userId: userId,
|
||||
});
|
||||
|
||||
res.status(201).json(product);
|
||||
} catch (error) {
|
||||
console.error('Error creating product:', error);
|
||||
res.status(500).json({ error: 'An error occurred while creating the product.' });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,14 +1,17 @@
|
|||
import express, { Request, Response } from 'express';
|
||||
import { Request, Response } from 'express';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { User, IUser } from '../models/User';
|
||||
import { User, IUser } from '../models/Schema';
|
||||
import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth';
|
||||
|
||||
const app = express();
|
||||
|
||||
export async function createUser(req: Request, res: Response) {
|
||||
try {
|
||||
const { firstName, lastName, email, password, address } = req.body;
|
||||
// validate the request body first
|
||||
if (!(email && password && firstName && lastName && address)) {
|
||||
return res.status(400).json({ error: 'All inputs are required' });
|
||||
}
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
const user: IUser = await User.create({
|
||||
firstName,
|
||||
|
|
|
@ -18,7 +18,7 @@ export function authenticateToken(req: AuthenticatedRequest, res: Response, next
|
|||
if (err) {
|
||||
return res.status(401).json({ error: 'In Valid Token' });
|
||||
}
|
||||
req.userId = decoded.userId;
|
||||
req.body.userId = decoded.userId;
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -33,11 +33,13 @@ const ProductSchema: Schema = new Schema({
|
|||
name: { type: String, required: true },
|
||||
description: { type: String, required: true },
|
||||
price: { type: Number, required: true },
|
||||
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
||||
userId: { type: Schema.Types.ObjectId, ref: 'User' , required: true},
|
||||
createdAt: { type: Date, default: Date.now },
|
||||
updatedAt: { type: Date, default: Date.now },
|
||||
});
|
||||
|
||||
ProductSchema.index({ name: 1, userId: 1 }, { unique: true });
|
||||
|
||||
const User = mongoose.model<IUser>('User', UserSchema);
|
||||
const Product = mongoose.model<IProduct>('Product', ProductSchema);
|
||||
|
|
@ -1,10 +1,13 @@
|
|||
import express from 'express';
|
||||
import express, { Request } from 'express';
|
||||
import { authenticateToken } from '../middlewares/checkAuth';
|
||||
import { createProduct } from '../controllers/ProductController';
|
||||
|
||||
|
||||
|
||||
const productRouter = express.Router();
|
||||
|
||||
productRouter.post('/', authenticateToken, (req, res) => {
|
||||
res.send('Create product');
|
||||
})
|
||||
// Create a product: This should require an authenticated user to provide a
|
||||
// product name, description, and price.
|
||||
productRouter.post('/', authenticateToken, createProduct)
|
||||
|
||||
export default productRouter;
|
Loading…
Reference in a new issue