Skip to content

Commit

Permalink
Added try catch blocks for all the modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant-jain-94 committed Nov 6, 2017
1 parent 683f5d5 commit e2f3b3b
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 77 deletions.
7 changes: 7 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ app.use('/history', history);
app.use('/question', question);
app.use('/questions/draft', draft);

app.use((err, req, res, next) => {
req.log.error(err);
res.status(err.status || 500);
res.json({ message: err.message });
next();
});

module.exports = app;
26 changes: 19 additions & 7 deletions draft/draft.controller.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
const Draft = require('./draft.model');

const draftQuestion = async (draft) => {
const draftedQuestion = await Draft.draft(draft);
return draftedQuestion;
try {
const draftedQuestion = await Draft.draft(draft);
return draftedQuestion;
} catch (error) {
throw error;
}
};

const fetchDrafts = async (query = {}, page = 1, limit = 50) => {
const drafts = await Draft.fetch(query, page, limit);
return drafts;
try {
const drafts = await Draft.fetch(query, page, limit);
return drafts;
} catch (error) {
throw error;
}
};

const deleteDrafts = async (drafts) => {
const draftIds = drafts.map(draft => draft.id);
const draftedQuestions = await Draft.deleteDrafts(draftIds);
return draftedQuestions;
try {
const draftIds = drafts.map(draft => draft.id);
const draftedQuestions = await Draft.deleteDrafts(draftIds);
return draftedQuestions;
} catch (error) {
throw error;
}
};

module.exports = {
Expand Down
48 changes: 30 additions & 18 deletions draft/draft.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,44 @@ const DraftSchema = new Schema({
DraftSchema.set('toJSON', { getters: true });

DraftSchema.statics.draft = async function patch(question) {
const query = { _id: question.id ? question.id : mongoose.Types.ObjectId() };
const queryOptions = {
upsert: true,
new: true,
setDefaultsOnInsert: true,
runSettersOnQuery: true,
};
try {
const query = { _id: question.id ? question.id : mongoose.Types.ObjectId() };
const queryOptions = {
upsert: true,
new: true,
setDefaultsOnInsert: true,
runSettersOnQuery: true,
};

const patchedQuestion = await this.findOneAndUpdate(
query,
question,
queryOptions,
).exec();
const patchedQuestion = await this.findOneAndUpdate(
query,
question,
queryOptions,
).exec();

return patchedQuestion.toJSON();
return patchedQuestion.toJSON();
} catch (error) {
throw error;
}
};

DraftSchema.statics.fetch = async function fetchAll(query = {}, page = 1, limit = 100) {
const skips = limit * (page - 1);
const questions = await this.find(query).skip(skips).limit(limit).exec();
return questions;
try {
const skips = limit * (page - 1);
const questions = await this.find(query).skip(skips).limit(limit).exec();
return questions;
} catch (error) {
throw error;
}
};

DraftSchema.statics.deleteDrafts = async function deleteDrafts(draftIds) {
const draftedQuestions = await this.deleteMany({ _id: { $in: draftIds } });
return draftedQuestions;
try {
const draftedQuestions = await this.deleteMany({ _id: { $in: draftIds } });
return draftedQuestions;
} catch (error) {
throw error;
}
};

module.exports = mongoose.model('Draft', DraftSchema, 'draft');
34 changes: 23 additions & 11 deletions draft/draft.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@ const express = require('express');
const router = express.Router();
const DraftController = require('./draft.controller.js');

router.post('/', async (req, res) => {
const draftedQuestion = await DraftController.draftQuestion(req.body);
res.json(draftedQuestion);
router.post('/', async (req, res, next) => {
try {
const draftedQuestion = await DraftController.draftQuestion(req.body);
res.json(draftedQuestion);
} catch (error) {
next(error);
}
});

router.get('/', async (req, res) => {
const { limit, page } = req.query;
const fetchedQuestions = await DraftController.fetchDrafts(limit, page);
res.json(fetchedQuestions);
router.get('/', async (req, res, next) => {
try {
const { limit, page } = req.query;
const fetchedQuestions = await DraftController.fetchDrafts(limit, page);
res.json(fetchedQuestions);
} catch (error) {
next(error);
}
});

router.delete('/', async (req, res) => {
const { body: drafts } = req;
const deletedQuestions = await DraftController.deleteDrafts(drafts);
res.json(deletedQuestions);
router.delete('/', async (req, res, next) => {
try {
const { body: drafts } = req;
const deletedQuestions = await DraftController.deleteDrafts(drafts);
res.json(deletedQuestions);
} catch (error) {
next(error);
}
});

module.exports = router;
42 changes: 29 additions & 13 deletions history/history.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,46 @@ const jdr = require('json-diff-rfc6902');

// Calculates the differences between the oldQuestion and the newQuestion
const calculateDiffs = (oldQuestion, newQuestion) => {
const diffs = jdr.diff(oldQuestion, newQuestion);
return diffs;
try {
const diffs = jdr.diff(oldQuestion, newQuestion);
return diffs;
} catch (error) {
throw Error(error);
}
};

// Stores the differences between the Old and the New Question in History Collection
const storeDiffs = async (changeSet) => {
const storedChangeSet = await History.insert(changeSet);
return storedChangeSet;
try {
const storedChangeSet = await History.insert(changeSet);
return storedChangeSet;
} catch (error) {
throw Error(error);
}
};

const calculateAndStoreDiffs = async (oldQuestion, newQuestion) => {
const diffs = calculateDiffs(oldQuestion, newQuestion);
const changeSet = {
questionId: newQuestion.id,
changes: diffs,
};
const savedChangeSet = await storeDiffs(changeSet);
return savedChangeSet;
try {
const diffs = calculateDiffs(oldQuestion, newQuestion);
const changeSet = {
questionId: newQuestion.id,
changes: diffs,
};
const savedChangeSet = await storeDiffs(changeSet);
return savedChangeSet;
} catch (error) {
throw Error(error);
}
};

// Fetches changeSet of a given questionId
const fetchChanges = async (questionId) => {
const fetchedQuestion = await History.fetch(questionId);
return fetchedQuestion;
try {
const fetchedQuestion = await History.fetch(questionId);
return fetchedQuestion;
} catch (error) {
throw Error(error);
}
};

module.exports = {
Expand Down
20 changes: 14 additions & 6 deletions history/history.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ historySchema.set('toJSON', { getters: true });

// Inserts the ChangeSet in the History Collection
historySchema.statics.insert = async function insertChangeSet(changeSet) {
const diffs = new this(changeSet);
const savedChangeSet = await diffs.save();
return savedChangeSet.toJSON();
try {
const diffs = new this(changeSet);
const savedChangeSet = await diffs.save();
return savedChangeSet.toJSON();
} catch (error) {
throw error;
}
};

// Fetches the ChangeSet for a questionId
historySchema.statics.fetch = async function fetchChangeSet(questionId) {
const changeSets = await this.find({ questionId }).exec();
const transformedChangeSets = changeSets.map(changeSet => changeSet.toJSON());
return transformedChangeSets;
try {
const changeSets = await this.find({ questionId }).exec();
const transformedChangeSets = changeSets.map(changeSet => changeSet.toJSON());
return transformedChangeSets;
} catch (error) {
throw error;
}
};

module.exports = mongoose.model('History', historySchema, 'history');
10 changes: 7 additions & 3 deletions history/history.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ const express = require('express');
const router = express.Router();
const historyController = require('./history.controller');

router.get('/:questionId', async (req, res) => {
const fetchedChangeSets = await historyController.fetchChanges(req.params.questionId);
res.json(fetchedChangeSets);
router.get('/:questionId', async (req, res, next) => {
try {
const fetchedChangeSets = await historyController.fetchChanges(req.params.questionId);
res.json(fetchedChangeSets);
} catch (error) {
next(error);
}
});

module.exports = router;
8 changes: 6 additions & 2 deletions ping/ping.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ const express = require('express');

const router = express.Router();

router.get('/', (req, res) => {
res.send('pong');
router.get('/', (req, res, next) => {
try {
res.send('pong');
} catch (error) {
next(error);
}
});

module.exports = router;
8 changes: 6 additions & 2 deletions question/question.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ const Question = require('./question.model');
const History = require('../history/history.controller');

const fetch = async (query, limit, page) => {
const questions = await Question.fetch(query, limit, page);
return questions;
try {
const questions = await Question.fetch(query, limit, page);
return questions;
} catch (error) {
throw Error(error);
}
};

const publish = async (question) => {
Expand Down
28 changes: 17 additions & 11 deletions question/question.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ questionSchema.set('toJSON', { getters: true, hide: '_id' });
* @returns {Object} publishedQuestion
*/
questionSchema.statics.publish = async function publish(question) {
const [existingQuestion] = question.id ?
await this.find({ _id: question.id }).exec() :
[new this()];

Object.assign(existingQuestion, question);
// const current = new this(question);
const publishedQuestion = await existingQuestion.save();
return publishedQuestion.toJSON();
try {
const [existingQuestion] = question.id ?
await this.find({ _id: question.id }).exec() :
[new this()];
Object.assign(existingQuestion, question);
const publishedQuestion = await existingQuestion.save();
return publishedQuestion.toJSON();
} catch (error) {
throw error;
}
};

/**
Expand All @@ -92,9 +94,13 @@ questionSchema.statics.publish = async function publish(question) {
* @returns {Array<Question>} An array of published question matching the query.
*/
questionSchema.statics.fetch = async function fetch(query = {}, limit = 100, page = 1) {
const skips = limit * (page - 1);
const fetchedQuestions = await this.find(query).skip(skips).limit(limit).exec();
return fetchedQuestions.map(question => question.toJSON());
try {
const skips = limit * (page - 1);
const fetchedQuestions = await this.find(query).skip(skips).limit(limit).exec();
return fetchedQuestions.map(question => question.toJSON());
} catch (error) {
throw error;
}
};

module.exports = mongoose.model('Question', questionSchema, 'question');
10 changes: 6 additions & 4 deletions question/question.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ const _ = require('lodash');

const router = express.Router();

router.post('/', async (req, res) => {
router.post('/', async (req, res, next) => {
try {
const publishedQuestion = await questionController.publish(req.body);
req.log.info(publishedQuestion);
res.json(publishedQuestion);
} catch (error) {
res.status(500).json(error);
next(error);
}
});

router.get('/', async (req, res) => {
router.get('/', async (req, res, next) => {
try {
const query = _.omit(req.query, 'limit', 'page');
const publishedQuestions = await questionController.fetch(
query,
req.query.limit,
req.query.page,
);
req.log.info(publishedQuestions);
res.json(publishedQuestions);
} catch (error) {
res.status(500).json(error);
next(error);
}
});

Expand Down

0 comments on commit e2f3b3b

Please sign in to comment.