Compare commits
2 commits
f3d506c005
...
23f030ed56
Author | SHA1 | Date | |
---|---|---|---|
23f030ed56 | |||
3f3aecbe84 |
6 changed files with 116 additions and 36 deletions
|
@ -1,44 +1,66 @@
|
||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import { Product } from '../schemas/productSchema';
|
import { Product } from '../schemas/productSchema';
|
||||||
// import { handleCreateProductError } from '../middlewares/errorHandler';
|
import { createProduct, getAllProducts, getOne } from '../models/productModel';
|
||||||
|
import { ApiError } from '../utils/ApiError';
|
||||||
|
|
||||||
|
const create = async (req: Request, res: Response, next) => {
|
||||||
export async function createProduct(req: Request, res: Response) {
|
|
||||||
try {
|
try {
|
||||||
const product = new Product(req.body);
|
const { name, description, price } = req.body;
|
||||||
await product.save();
|
const product = await createProduct({
|
||||||
res.json(product);
|
name,
|
||||||
} catch (error) {
|
description,
|
||||||
console.error('Error creating product:', error);
|
price,
|
||||||
// handleCreateProductError(res, error);
|
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 {
|
try {
|
||||||
const { page, limit } = req.query;
|
const products = await getAllProducts({
|
||||||
const dbPage = Number(page) || 0;
|
page: Number(req.query.page),
|
||||||
const dbLimit = Number(limit) || 50;
|
limit: Number(req.query.limit)
|
||||||
const products = await Product.find(null, 'name description price').sort({ price: 1 }).skip(Number(dbPage) * Number(dbLimit)).limit(Number(dbLimit));
|
});
|
||||||
|
if(products instanceof Error) {
|
||||||
res.json(products);
|
console.log("Error in get all products")
|
||||||
} catch (error) {
|
return res.status(products.statusCode).json({ error: products.message });
|
||||||
console.error('Error listing products:', error);
|
}
|
||||||
res.status(500).json({ error: 'An error occurred while listing the products.' });
|
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) {
|
export async function getProduct(req: Request, res: Response) {
|
||||||
const { id } = req.params;
|
|
||||||
try {
|
try {
|
||||||
const product = await Product.findById(id, 'name description price');
|
const product = await getOne(req.params.id);
|
||||||
if(!product) {
|
if(product instanceof Error) {
|
||||||
res.status(404).json({ error: 'Product not found.' });
|
return res.status(product.statusCode).json({ error: product.message });
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
res.json(product);
|
res.status(200).json(product);
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.error('Error getting product:', error);
|
const error = new ApiError('Error during product creation');
|
||||||
res.status(404).json({ error: 'Product not found.' });
|
error.statusCode = 500;
|
||||||
|
error.status = 'fail';
|
||||||
|
res.status(error.statusCode).json({ error: error.message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
create,
|
||||||
|
getAll,
|
||||||
|
getOne
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import { createUser, loginUser, getAllUsers, deleteUser } from '../models/userModel';
|
import { createUser, loginUser, getAllUsers, deleteUser } from '../models/userModel';
|
||||||
import { ApiError } from '../utils/ApiError';
|
import { ApiError } from '../utils/ApiError';
|
||||||
import bcrypt from 'bcryptjs';
|
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth';
|
import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth';
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,6 @@ app.use('/products', productRouter);
|
||||||
app.use('/cart', cartRouter);
|
app.use('/cart', cartRouter);
|
||||||
|
|
||||||
app.all('*', (req, res, next) => {
|
app.all('*', (req, res, next) => {
|
||||||
// res.status(404).json({ error: 'Route not found' });
|
|
||||||
const error = new ApiError('Are you lost?');
|
const error = new ApiError('Are you lost?');
|
||||||
error.statusCode = 404;
|
error.statusCode = 404;
|
||||||
error.status = 'fail';
|
error.status = 'fail';
|
||||||
|
|
62
src/models/productModel.ts
Normal file
62
src/models/productModel.ts
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
await newProduct.save();
|
||||||
|
return newProduct;
|
||||||
|
} catch {
|
||||||
|
const error = new ApiError('Error during product creation');
|
||||||
|
error.statusCode = 500;
|
||||||
|
error.status = 'fail';
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
getAllProducts,
|
||||||
|
getOne
|
||||||
|
}
|
|
@ -1,10 +1,8 @@
|
||||||
import { User, IUser } from "../schemas/userSchema";
|
import { User } from "../schemas/userSchema";
|
||||||
import validate from 'deep-email-validator';
|
import validate from 'deep-email-validator';
|
||||||
import { ApiError } from "../utils/ApiError";
|
import { ApiError } from "../utils/ApiError";
|
||||||
import { response } from "express";
|
|
||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
|
|
||||||
|
|
||||||
const createUser = async (user: any) => {
|
const createUser = async (user: any) => {
|
||||||
|
|
||||||
if (!user.email || !user.password || !user.address) {
|
if (!user.email || !user.password || !user.address) {
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { authenticateToken } from '../middlewares/checkAuth';
|
import { authenticateToken } from '../middlewares/checkAuth';
|
||||||
import { createProduct, listProducts, getProduct } from '../controllers/productController';
|
import { create, getAll, getProduct } from '../controllers/productController';
|
||||||
|
|
||||||
const productRouter = express.Router();
|
const productRouter = express.Router();
|
||||||
|
|
||||||
productRouter.post('/', authenticateToken, createProduct)
|
productRouter.get('/', getAll);
|
||||||
productRouter.get('/', listProducts);
|
productRouter.post('/', authenticateToken, create)
|
||||||
productRouter.get('/:id', getProduct);
|
productRouter.get('/:id', getProduct);
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue