-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
78d26c3
commit 9398a7b
Showing
8 changed files
with
957 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,87 @@ | ||
const { findByIdAndDelete } = require('../models/Bootcamp'); | ||
const Bootcamp = require('../models/Bootcamp'); | ||
const ErrorResponse = require('../utils/errorResponse'); | ||
const asyncHandler = require('../middleware/async'); | ||
|
||
/** | ||
* NOTES | ||
* - Need body parser middleware (in server.js) in order to access "req.body" within these functions | ||
* - Always wrap in trycatch | ||
* - Always wrap in trycatch OR use asyncHandler | ||
* - If client sends data that is not in the model, it gets ignored | ||
*/ | ||
|
||
// @desc Get all bootcamps | ||
// @route GET /api/v1/bootcamps | ||
// @access Public | ||
exports.getBootcamps = async (req, res, next) => { | ||
try { | ||
const bootcamps = await Bootcamp.find(); | ||
res.status(200).json({ | ||
success: true, | ||
count: bootcamps.length, | ||
data: bootcamps | ||
}) | ||
} catch (err) { | ||
res.status(400).json({ | ||
success: false | ||
}); | ||
} | ||
}; | ||
exports.getBootcamps = asyncHandler(async (req, res, next) => { | ||
const bootcamps = await Bootcamp.find(); | ||
res.status(200).json({ | ||
success: true, | ||
count: bootcamps.length, | ||
data: bootcamps | ||
}) | ||
|
||
// Catch block --> next(err); | ||
}); | ||
|
||
// @desc Get single bootcamps | ||
// @route GET /api/v1/bootcamps/:id | ||
// @access Public | ||
exports.getBootcamp = async (req, res, next) => { | ||
try { | ||
const bootcamp = await Bootcamp.findById(req.params.id); | ||
exports.getBootcamp = asyncHandler(async (req, res, next) => { | ||
const bootcamp = await Bootcamp.findById(req.params.id); | ||
|
||
// handle err if id does not match any bootcamps | ||
if (!bootcamp) return res.status(400).json({success: false}); | ||
// handle err if id does not match any bootcamps in database | ||
if (!bootcamp) return next(new ErrorResponse(`Bootcamp not found with id of ${req.params.id}`, 404)); | ||
|
||
res.status(200).json({ | ||
success: true, | ||
data: bootcamp | ||
}) | ||
} catch (err) { | ||
// handle err if id is not correct format (ie. # of digits) | ||
|
||
// res.status(400).json({ | ||
// success: false | ||
// }); | ||
next(err); | ||
} | ||
}; | ||
res.status(200).json({ | ||
success: true, | ||
data: bootcamp | ||
}) | ||
|
||
// Catch block - handle err if id is not correct format (ie. # of digits) --> asyncHandler | ||
// next(err); | ||
|
||
}); | ||
|
||
// @desc Create new bootcamp | ||
// @route POST /api/v1/bootcamps | ||
// @access Private | ||
exports.createBootcamp = async (req, res, next) => { | ||
try { | ||
const bootcamp = await Bootcamp.create(req.body); | ||
exports.createBootcamp = asyncHandler(async (req, res, next) => { | ||
const bootcamp = await Bootcamp.create(req.body); | ||
|
||
res.status(201).json({ | ||
success: true, | ||
data: bootcamp | ||
}); | ||
} catch (err) { | ||
res.status(400).json({success: false}); | ||
} | ||
}; | ||
res.status(201).json({ | ||
success: true, | ||
data: bootcamp | ||
}); | ||
|
||
// Catch block --> handles duplicate keys AND handles form validation | ||
// next(err); | ||
}); | ||
|
||
// @desc Update single bootcamp | ||
// @route PUT /api/v1/bootcamps/:id | ||
// @access Private | ||
exports.updateBootcamp = async (req, res, next) => { | ||
try { | ||
const bootcamp = await Bootcamp.findByIdAndUpdate( | ||
req.params.id, | ||
req.body, | ||
{ | ||
new: true, // response become supdates data | ||
runValidators: true | ||
} | ||
); | ||
|
||
if (!bootcamp) return res.status(400).json({success: false}); | ||
|
||
res.status(200).json({success: true, data: bootcamp}); | ||
} catch (err) { | ||
res.status(400).json({success: false}); | ||
} | ||
|
||
}; | ||
exports.updateBootcamp = asyncHandler(async (req, res, next) => { | ||
const bootcamp = await Bootcamp.findByIdAndUpdate( | ||
req.params.id, | ||
req.body, | ||
{ | ||
new: true, // response become supdates data | ||
runValidators: true | ||
} | ||
); | ||
|
||
if (!bootcamp) return next(new ErrorResponse(`Bootcamp not found with id of ${req.params.id}`, 404)); | ||
|
||
res.status(200).json({success: true, data: bootcamp}); | ||
}); | ||
|
||
// @desc Delete Single Bootcamp | ||
// @route DELETE /api/v1/bootcamps/:id | ||
// @access Provate | ||
exports.deleteBootcamp = async (req, res, next) => { | ||
try { | ||
const bootcamp = await Bootcamp.findByIdAndDelete(req.params.id); | ||
|
||
if (!bootcamp) return res.status(400).json({success: false}); | ||
exports.deleteBootcamp = asyncHandler(async (req, res, next) => { | ||
const bootcamp = await Bootcamp.findByIdAndDelete(req.params.id); | ||
|
||
res.status(200).json({success: true, data: {}}); | ||
if (!bootcamp) return next(new ErrorResponse(`Bootcamp not found with id of ${req.params.id}`, 404)); | ||
|
||
} catch (err) { | ||
res.status(400).json({success: false}); | ||
} | ||
}; | ||
res.status(200).json({success: true, data: {}}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const asyncHandler = fn => (req, res, next) => | ||
Promise.resolve(fn(req, res, next)).catch(next); | ||
|
||
module.exports = asyncHandler; |
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
Oops, something went wrong.