done products

This commit is contained in:
Kfir Dayan 2023-06-23 08:57:01 +03:00
parent 3f3aecbe84
commit 23f030ed56
4 changed files with 62 additions and 26 deletions

View file

@ -1,6 +1,6 @@
import { Request, Response } from 'express';
import { Product } from '../schemas/productSchema';
import { createProduct } from '../models/productModel';
import { createProduct, getAllProducts, getOne } from '../models/productModel';
import { ApiError } from '../utils/ApiError';
const create = async (req: Request, res: Response, next) => {
@ -27,34 +27,40 @@ const create = async (req: Request, res: Response, next) => {
const getAll = async (req: Request, res: Response) => {
try {
const { page, limit } = req.query;
const dbPage = Number(page) || 0;
const dbLimit = Number(limit) || 50;
const products = await Product.find(null, 'name description price').sort({ price: 1 }).skip(Number(dbPage) * Number(dbLimit)).limit(Number(dbLimit));
res.json(products);
} catch (error) {
console.error('Error listing products:', error);
res.status(500).json({ error: 'An error occurred while listing the products.' });
const products = await getAllProducts({
page: Number(req.query.page),
limit: Number(req.query.limit)
});
if(products instanceof Error) {
console.log("Error in get all products")
return res.status(products.statusCode).json({ error: products.message });
}
res.status(200).json(products);
} catch {
const error = new ApiError('Error during product creation');
error.statusCode = 500;
error.status = 'fail';
res.status(error.statusCode).json({ error: error.message });
}
}
export async function getProduct(req: Request, res: Response) {
const { id } = req.params;
try {
const product = await Product.findById(id, 'name description price');
if (!product) {
res.status(404).json({ error: 'Product not found.' });
return;
const product = await getOne(req.params.id);
if(product instanceof Error) {
return res.status(product.statusCode).json({ error: product.message });
}
res.json(product);
} catch (error) {
console.error('Error getting product:', error);
res.status(404).json({ error: 'Product not found.' });
res.status(200).json(product);
} catch {
const error = new ApiError('Error during product creation');
error.statusCode = 500;
error.status = 'fail';
res.status(error.statusCode).json({ error: error.message });
}
}
export {
create,
getAll
getAll,
getOne
}

View file

@ -1,7 +1,6 @@
import { Request, Response } from 'express';
import { createUser, loginUser, getAllUsers, deleteUser } from '../models/userModel';
import { ApiError } from '../utils/ApiError';
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth';

View file

@ -14,7 +14,6 @@ const createProduct = async (product: any) => {
error.status = 'fail';
return error;
}
console.log("newProduct", newProduct);
await newProduct.save();
return newProduct;
} catch {
@ -25,7 +24,39 @@ const createProduct = async (product: any) => {
}
}
const getAllProducts = async (query: {
page: number,
limit: number
}) => {
try {
const { page, limit } = query;
const dbPage = page || 0;
const dbLimit = limit || 50;
const products = await Product.find(null, 'name description price').sort({ price: 1 }).skip(dbPage * dbLimit).limit(dbLimit);
return products;
} catch {
const error = new ApiError('Error during product creation');
error.statusCode = 500;
error.status = 'fail';
return error;
}
}
const getOne = async (id: string) => {
try {
const product = await Product.findById(id, 'name description price');
return product;
} catch {
const error = new ApiError('Product not found');
error.statusCode = 404;
error.status = 'fail';
return error;
}
}
export {
createProduct
createProduct,
getAllProducts,
getOne
}

View file

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