create for product

This commit is contained in:
Kfir Dayan 2023-06-22 13:52:04 +03:00
parent f3d506c005
commit 3f3aecbe84
5 changed files with 63 additions and 19 deletions

View file

@ -1,20 +1,31 @@
import { Request, Response } from 'express';
import { Product } from '../schemas/productSchema';
// import { handleCreateProductError } from '../middlewares/errorHandler';
import { createProduct } from '../models/productModel';
import { ApiError } from '../utils/ApiError';
export async function createProduct(req: Request, res: Response) {
const create = async (req: Request, res: Response, next) => {
try {
const product = new Product(req.body);
await product.save();
res.json(product);
} catch (error) {
console.error('Error creating product:', error);
// handleCreateProductError(res, error);
const { name, description, price } = req.body;
const product = await createProduct({
name,
description,
price,
userId: req.body.userId
});
if(product instanceof Error) {
console.log("Error in create product")
return next(product);
}
res.status(201).json(product);
} catch {
const error = new ApiError('Error during product creation');
error.statusCode = 500;
error.status = 'fail';
next(error);
}
}
export async function listProducts(req: Request, res: Response) {
const getAll = async (req: Request, res: Response) => {
try {
const { page, limit } = req.query;
const dbPage = Number(page) || 0;
@ -32,7 +43,7 @@ export async function getProduct(req: Request, res: Response) {
const { id } = req.params;
try {
const product = await Product.findById(id, 'name description price');
if(!product) {
if (!product) {
res.status(404).json({ error: 'Product not found.' });
return;
}
@ -42,3 +53,8 @@ export async function getProduct(req: Request, res: Response) {
res.status(404).json({ error: 'Product not found.' });
}
}
export {
create,
getAll
}

View file

@ -41,7 +41,6 @@ app.use('/products', productRouter);
app.use('/cart', cartRouter);
app.all('*', (req, res, next) => {
// res.status(404).json({ error: 'Route not found' });
const error = new ApiError('Are you lost?');
error.statusCode = 404;
error.status = 'fail';

View file

@ -0,0 +1,31 @@
import { Product } from '../schemas/productSchema';
import { Request, Response } from 'express';
import { errorHandler } from '../middlewares/errorHandler';
import { ApiError } from '../utils/ApiError';
const createProduct = async (product: any) => {
try {
const newProduct = new Product(product);
const isExist = await Product.findOne({ name: product.name, userId: product.userId });
if(isExist) {
const error = new ApiError('Product already exists');
error.statusCode = 400;
error.status = 'fail';
return error;
}
console.log("newProduct", newProduct);
await newProduct.save();
return newProduct;
} catch {
const error = new ApiError('Error during product creation');
error.statusCode = 500;
error.status = 'fail';
return error;
}
}
export {
createProduct
}

View file

@ -1,10 +1,8 @@
import { User, IUser } from "../schemas/userSchema";
import { User } from "../schemas/userSchema";
import validate from 'deep-email-validator';
import { ApiError } from "../utils/ApiError";
import { response } from "express";
import bcrypt from 'bcryptjs';
const createUser = async (user: any) => {
if (!user.email || !user.password || !user.address) {

View file

@ -1,12 +1,12 @@
import express from 'express';
import { authenticateToken } from '../middlewares/checkAuth';
import { createProduct, listProducts, getProduct } from '../controllers/productController';
import { create, getAll } from '../controllers/productController';
const productRouter = express.Router();
productRouter.post('/', authenticateToken, createProduct)
productRouter.get('/', listProducts);
productRouter.get('/:id', getProduct);
// productRouter.get('/', listProducts);
productRouter.post('/', authenticateToken, create)
// productRouter.get('/:id', getProduct);
export default productRouter;