router.get('/:id', async (req, res) => { const params = req.params; try { await indicative.validator.validate(params, { id: 'required|regex:^[0-9a-f]{24}$' }); const post = await req.app.locals.db.collection('posts').findOne({ _id: new ObjectID(req.params.id) }); if (!post) { sendErrorResponse(req, res, 404, `Post with ID=${req.params.id} does not exist`); return; } res.json(post); } catch (errors) { sendErrorResponse(req, res, 400, `Invalid post data: ${errors.map(e => e.message).join(', ')}`, errors); } });
router.post('/', (req, res) => { const todo = { title: req.body.todo, description: req.body.description, priority: req.body.priority, createdAt: new Date() }; indicative .validate(todo, todoRules, todoRulesMessage) .then(() => { knex('todo') .insert(todo, 'id') .then(ids => { const id = ids[0]; res.redirect(`/todo/${id}`); }); }) .catch( (errors) => { res.render('app/new', {errors: errors}); }); });
router.post('/', function(req, res) { const post = req.body; indicative.validator.validate(post, { // id: 'required|regex:^[0-9a-f]{24}', title: 'required|string|min:3|max:60', subtitle: 'string|max:120', content: 'string', imageUrl: 'url' }).then(() => { req.app.locals.db.collection('posts').insertOne(post).then(r => { if (r.result.ok && r.insertedCount === 1) { delete post._id; post.id = r.insertedId; console.log(`Created post: ${post.id}: ${post.title}`); res.status(201).location(`/posts/${post.id}`).json(post); } else { sendErrorResponse(req, res, 500, `Server error: ${err.message}`, err); } }).catch(err => { console.error(`Unable to create post: ${post.id}: ${post.title}.`); console.error(err); sendErrorResponse(req, res, 500, `Server error: ${err.message}`, err); }); }).catch(errors => { sendErrorResponse(req, res, 400, `Invalid post data: ${errors.map(e => e.message).join(', ')}`, errors); }); });
router.delete('/:id', async (req, res) => { const params = req.params; try { await indicative.validator.validate(params, { id: 'required|regex:^[0-9a-f]{24}$' }); const old = await req.app.locals.db.collection('users').findOne({ _id: new ObjectID(req.params.id) }); if (!old) { sendErrorResponse(req, res, 404, `User with ID=${req.params.id} does not exist`); return; } replaceId(old); const r = await req.app.locals.db.collection('users').deleteOne({ _id: new ObjectID(req.params.id) }); if (r.result.ok && r.deletedCount === 1) { res.json(old); } else { console.log(`Unable to delete user: ${old.id}: ${old.firstName} ${old.lastName}`); sendErrorResponse(req, res, 500, `Unable to delete user: ${old.id}: ${old.firstName} ${old.lastName}`); } } catch (errors) { sendErrorResponse(req, res, 400, `Invalid user data: ${errors.map(e => e.message).join(', ')}`, errors); } });
router.delete('/:id', async (req, res) => { const params = req.params; try { await indicative.validator.validate(params, { id: 'required|regex:^[0-9a-f]{24}$' }); const old = await req.app.locals.db.collection('posts').findOne({ _id: new ObjectID(req.params.id) }); if (!old) { sendErrorResponse(req, res, 404, `Post with ID=${req.params.id} does not exist`); return; } replaceId(old); const r = await req.app.locals.db.collection('posts').deleteOne({ _id: new ObjectID(req.params.id) }); if(r.result.ok && r.deletedCount === 1) { console.log(`Deleted post: ${old.id}: ${old.title}`); res.json(old); } else { console.log(`Unable to delete post: ${post.id}: ${post.title}`); sendErrorResponse(req, res, 500, `Unable to delete post: ${old.id}: ${old.title}`); } } catch (errors) { sendErrorResponse(req, res, 400, `Invalid post data: ${errors.map(e => e.message).join(', ')}`, errors); } });
router.get('/:id', async (req, res) => { const params = req.params; try { await indicative.validator.validate(params, { id: 'required|regex:^[0-9a-f]{24}$' }); const user = await req.app.locals.db.collection('users').findOne({ _id: new ObjectID(req.params.id) }); if (!user) { sendErrorResponse(req, res, 404, `User with ID=${req.params.id} does not exist`); return; } res.json(user); } catch (errors) { sendErrorResponse(req, res, 400, `Invalid user data: ${errors.map(e => e.message).join(', ')}`, errors); } });