Skip to content

Commit

Permalink
feat(history): add get function ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Apr 21, 2020
1 parent eda9526 commit 6f1af80
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
31 changes: 31 additions & 0 deletions modules/history/controllers/historys.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,34 @@ exports.list = async (req, res) => {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};

/**
* @desc Endpoint to show the current history
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.get = (req, res) => {
const history = req.history ? req.history.toJSON() : {};
responses.success(res, 'history get')(history);
};

/**
* @desc MiddleWare to ask the service the history for this id
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
* @param {String} id - history id
*/
exports.historyByID = async (req, res, next, id) => {
try {
const history = await HistorysService.get(id);
if (!history) responses.error(res, 404, 'Not Found', 'No history with that identifier has been found')();
else {
req.history = history;
req.isOwner = history.user; // used if we proteck road by isOwner policy
next();
}
} catch (err) {
next(err);
}
};
4 changes: 4 additions & 0 deletions modules/history/models/historys.model.mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const HistoryMongoose = new Schema({
type: Schema.ObjectId,
ref: 'User',
},
api: {
type: Schema.ObjectId,
ref: 'Api',
},
}, {
timestamps: true,
});
Expand Down
1 change: 1 addition & 0 deletions modules/history/models/historys.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const HistorySchema = Joi.object().keys({
data: Joi.string().optional(),
time: Joi.number().default(0).required(),
user: Joi.string().trim().default(''),
api: Joi.string().trim().default(''),
});

module.exports = {
Expand Down
3 changes: 3 additions & 0 deletions modules/history/policies/historys.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ exports.invokeRolesPolicies = () => {
allows: [{
resources: '/api/historys',
permissions: ['get'],
}, {
resources: '/api/historys/:historyId',
permissions: ['get'],
}],
}]);
};
20 changes: 18 additions & 2 deletions modules/history/repositories/historys.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ const mongoose = require('mongoose');
const History = mongoose.model('History');
const Api = mongoose.model('Api');


const defaultPopulate = [{
path: 'api',
select: 'id title',
}];

/**
* @desc Function to get all history in db
* @return {Array} All historys
*/
exports.list = (user) => History.find({ user: user._id }).sort('-createdAt').limit(100).exec();

exports.list = (user) => History.find({ user: user._id }).sort('-createdAt').populate(defaultPopulate).limit(500)
.exec;
/**
* @desc Function to create a scrap in db
* @param {Object} scrap
* @return {Object} scrap
*/
exports.create = (history) => new History(history).save();

/**
* @desc Function to get a history from db
* @param {String} id
* @return {Object} history
*/
exports.get = (id) => {
if (!mongoose.Types.ObjectId.isValid(id)) return null;
return History.findOne({ _id: id }).exec();
};

/**
* @desc Function to update scrap history in db
* @param {Object} scrap
Expand Down
7 changes: 7 additions & 0 deletions modules/history/routes/historys.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ module.exports = (app) => {
// list & post
app.route('/api/historys').all(passport.authenticate('jwt'), policy.isAllowed)
.get(historys.list); // list

// classic crud
app.route('/api/historys/:historyId').all(passport.authenticate('jwt'), policy.isAllowed) // policy.isOwner available
.get(historys.get); // get

// Finish by binding the historys middleware
app.param('historyId', historys.historyByID);
};
11 changes: 11 additions & 0 deletions modules/history/services/historys.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ exports.list = async (user) => {
return Promise.resolve(result);
};

/**
* @desc Function to ask repository to get an history
* @param {String} id
* @return {Promise} task
*/
exports.get = async (id) => {
const result = await HistorysRepository.get(id);
return Promise.resolve(result);
};


/**
* @desc Functio to ask repository to add an history
* @param {Object} scrap - original scrap
Expand Down

0 comments on commit 6f1af80

Please sign in to comment.