-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
402 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,131 @@ | ||
const AppError = require("../utils/appError"); | ||
const APIFeatures = require("../utils/apiFeatures"); | ||
const AppError = require('../utils/app_error'); | ||
const APIFeatures = require('../utils/api_features'); | ||
|
||
/** | ||
* Delete a document by ID | ||
* @param {Model} Model - The mongoose model | ||
* @returns {Function} - Express middleware function | ||
*/ | ||
exports.deleteOne = Model => async (req, res, next) => { | ||
try { | ||
const doc = await Model.findByIdAndDelete(req.params.id); | ||
|
||
if (!doc) { | ||
return next(new AppError(404, "fail", "No document found with that id"), req, res, next); | ||
} | ||
|
||
res.status(204).json({ | ||
status: "success", | ||
data: null | ||
}); | ||
} catch (error) { | ||
next(error); | ||
exports.deleteOne = (Model) => async (req, res, next) => { | ||
try { | ||
const doc = await Model.findByIdAndDelete(req.params.id); | ||
|
||
if (!doc) { | ||
return next( | ||
new AppError(404, 'fail', 'No document found with that id'), | ||
req, | ||
res, | ||
next | ||
); | ||
} | ||
}; | ||
|
||
res.status(204).json({ | ||
status: 'success', | ||
data: null, | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
/** | ||
* Update a document by ID | ||
* @param {Model} Model - The mongoose model | ||
* @returns {Function} - Express middleware function | ||
*/ | ||
exports.updateOne = Model => async (req, res, next) => { | ||
try { | ||
const doc = await Model.findByIdAndUpdate(req.params.id, req.body, { | ||
new: true, | ||
runValidators: true | ||
}); | ||
|
||
if (!doc) { | ||
return next(new AppError(404, "fail", "No document found with that id"), req, res, next); | ||
} | ||
|
||
res.status(200).json({ | ||
status: "success", | ||
data: { | ||
doc | ||
} | ||
}); | ||
|
||
} catch (error) { | ||
next(error); | ||
exports.updateOne = (Model) => async (req, res, next) => { | ||
try { | ||
const doc = await Model.findByIdAndUpdate(req.params.id, req.body, { | ||
new: true, | ||
runValidators: true, | ||
}); | ||
|
||
if (!doc) { | ||
return next( | ||
new AppError(404, 'fail', 'No document found with that id'), | ||
req, | ||
res, | ||
next | ||
); | ||
} | ||
}; | ||
|
||
res.status(200).json({ | ||
status: 'success', | ||
data: { | ||
doc, | ||
}, | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
/** | ||
* Create a new document | ||
* @param {Model} Model - The mongoose model | ||
* @returns {Function} - Express middleware function | ||
*/ | ||
exports.createOne = Model => async (req, res, next) => { | ||
try { | ||
const doc = await Model.create(req.body); | ||
|
||
res.status(201).json({ | ||
status: "success", | ||
data: { | ||
doc | ||
} | ||
}); | ||
|
||
} catch (error) { | ||
next(error); | ||
} | ||
exports.createOne = (Model) => async (req, res, next) => { | ||
try { | ||
const doc = await Model.create(req.body); | ||
|
||
res.status(201).json({ | ||
status: 'success', | ||
data: { | ||
doc, | ||
}, | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
/** | ||
* Get a document by ID | ||
* @param {Model} Model - The mongoose model | ||
* @returns {Function} - Express middleware function | ||
*/ | ||
exports.getOne = Model => async (req, res, next) => { | ||
try { | ||
const doc = await Model.findById(req.params.id); | ||
|
||
if (!doc) { | ||
return next(new AppError(404, "fail", "No document found with that id"), req, res, next); | ||
} | ||
|
||
res.status(200).json({ | ||
status: "success", | ||
data: { | ||
doc | ||
} | ||
}); | ||
} catch (error) { | ||
next(error); | ||
exports.getOne = (Model) => async (req, res, next) => { | ||
try { | ||
const doc = await Model.findById(req.params.id); | ||
|
||
if (!doc) { | ||
return next( | ||
new AppError(404, 'fail', 'No document found with that id'), | ||
req, | ||
res, | ||
next | ||
); | ||
} | ||
|
||
res.status(200).json({ | ||
status: 'success', | ||
data: { | ||
doc, | ||
}, | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
/** | ||
* Get all documents | ||
* @param {Model} Model - The mongoose model | ||
* @returns {Function} - Express middleware function | ||
*/ | ||
exports.getAll = Model => async (req, res, next) => { | ||
try { | ||
const features = new APIFeatures(Model.find(), req.query) | ||
.sort() | ||
.paginate(); | ||
|
||
const doc = await features.query; | ||
|
||
res.status(200).json({ | ||
status: "success", | ||
results: doc.length, | ||
data: { | ||
data: doc | ||
} | ||
}); | ||
|
||
} catch (error) { | ||
next(error); | ||
} | ||
|
||
exports.getAll = (Model) => async (req, res, next) => { | ||
try { | ||
const features = new APIFeatures(Model.find(), req.query).sort().paginate(); | ||
|
||
const doc = await features.query; | ||
|
||
res.status(200).json({ | ||
status: 'success', | ||
results: doc.length, | ||
data: { | ||
data: doc, | ||
}, | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
Oops, something went wrong.