firstapp / controllers /productsController.js
Beshoy-mounir's picture
Upload 15 files
29a937c verified
Raw History Blame Contribute Delete
3.25 kB
const Product = require('../models/productModel');
const catchAsync = require('../utils/catchAsync');
/*
====================================================
GET ALL PRODUCTS
Supports:
- filtering
- sorting
- field limiting
- pagination
====================================================
*/
exports.getAllProducts = catchAsync(async (req, res) => {
// ---------- Filtering ----------
const queryObj = { ...req.query };
const excluded = ['page', 'sort', 'limit', 'fields'];
excluded.forEach((el) => delete queryObj[el]);
let queryStr = JSON.stringify(queryObj);
queryStr = queryStr.replace(/\b(gte|gt|lte|lt)\b/g, (match) => `$${match}`);
let query = Product.find(JSON.parse(queryStr));
// ---------- Sorting ----------
if (req.query.sort) {
const sortBy = req.query.sort.split(',').join(' ');
query = query.sort(sortBy);
} else {
query = query.sort('-createdAt');
}
// ---------- Field Limiting ----------
if (req.query.fields) {
const fields = req.query.fields.split(',').join(' ');
query = query.select(fields);
}
// ---------- Pagination ----------
const page = req.query.page * 1 || 1;
const limit = req.query.limit * 1 || 20;
const skip = (page - 1) * limit;
query = query.skip(skip).limit(limit);
const products = await query;
res.status(200).json({
status: 'success',
results: products.length,
data: {
products,
},
});
});
/*
====================================================
GET SINGLE PRODUCT
====================================================
*/
exports.getProduct = catchAsync(async (req, res, next) => {
const product = await Product.findById(req.params.id);
if (!product) {
return res.status(404).json({
status: 'fail',
message: 'Product not found',
});
}
res.status(200).json({
status: 'success',
data: {
product,
},
});
});
/*
====================================================
CREATE PRODUCT
====================================================
*/
exports.createProduct = catchAsync(async (req, res) => {
const newProduct = await Product.create(req.body);
res.status(201).json({
status: 'success',
data: {
product: newProduct,
},
});
});
/*
====================================================
UPDATE PRODUCT
====================================================
*/
exports.updateProduct = catchAsync(async (req, res) => {
const product = await Product.findByIdAndUpdate(req.params.id, req.body, {
new: true,
runValidators: true,
});
if (!product) {
return res.status(404).json({
status: 'fail',
message: 'Product not found',
});
}
res.status(200).json({
status: 'success',
data: {
product,
},
});
});
/*
====================================================
DELETE PRODUCT
(Hard delete — simple project)
====================================================
*/
exports.deleteProduct = catchAsync(async (req, res) => {
const product = await Product.findByIdAndDelete(req.params.id);
if (!product) {
return res.status(404).json({
status: 'fail',
message: 'Product not found',
});
}
res.status(204).json({
status: 'success',
data: null,
});
});