Spaces:
Runtime error
Runtime error
Download controllers/productsController.js from Beshoy-mounir/firstapp: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/spaces/Beshoy-mounir/firstapp/resolve/main/controllers/productsController.js
- Command line
-
hf download hf://spaces/Beshoy-mounir/firstapp/controllers/productsController.js
-
curl -L -o productsController.js https://huggingface.co/spaces/Beshoy-mounir/firstapp/resolve/main/controllers/productsController.js
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, | |
| }); | |
| }); | |