@kapvm/create-express-app
Version:
A CLI tool to scaffold an Express.js boilerplate project
93 lines (78 loc) • 2.15 kB
JavaScript
const catchAsync = require('../utils/catchAsync');
const AppError = require('./../utils/appError');
const APIFeatures = require('../utils/apiFeatures');
//=>
// Getter [All] Factory Function - Gets a data of "Model"s Data
exports.getAll = (Model) =>
catchAsync(async (req, res, next) => {
const features = new APIFeatures(Model.find(), req.query)
.filter()
.sort()
.paginate()
.limitField();
const doc = await features.query;
res.status(200).json({
status: 'success',
requestedAt: req.requestTime,
results: doc.length,
data: {
doc,
},
});
});
//=>
// Getter [ONE] Factory Function - Gets a data of "Model"s Data
exports.getOne = (Model) =>
catchAsync(async (req, res, next) => {
const doc = await Model.findById(req.params.id);
if (!doc) {
return next(new AppError('Can not find any document with that ID', 404));
}
res.status(200).json({
status: 'success',
data: {
doc,
},
});
});
//=>
// Creating Factory Function - Creates data of "Model"s Data
exports.createOne = (Model) =>
catchAsync(async (req, res, next) => {
const doc = await Model.create(req.body);
res.status(201).json({
status: 'success',
data: {
doc,
},
});
});
//=>
// Updating Factory Function - Updates data of "Model"s Data
exports.updateOne = (Model) =>
catchAsync(async (req, res, next) => {
const doc = await Model.findByIdAndUpdate(req.params.id, req.body, {
new: true,
runValidators: true,
});
if (!doc) {
return next(new AppError('Can not find any document with that ID', 404));
}
res.status(200).json({
status: 'success',
data: {
doc,
},
});
});
//=>
// Deleting Factory Function - Delete data of "Model"'s Data
exports.deleteOne = (Model) =>
catchAsync(async (req, res, next) => {
const doc = await Model.findByIdAndDelete(req.params.id);
if (!doc) return next(new AppError('No document found with that ID', 404));
res.status(204).json({
status: 'success',
data: null,
});
});