Skip to content

Commit

Permalink
feat(apis): init get data road on api ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 17, 2020
1 parent 7ccb023 commit 8d82a8e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
15 changes: 15 additions & 0 deletions modules/apis/controllers/apis.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ exports.load = async (req, res) => {
}
};

/**
* @desc Endpoint to getData stocked from load
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.getApiData = async (req, res) => {
// TODO if (req.scrap && req.user && req.scrap.user && req.scrap.user.id === req.user.id) next();
try {
const data = await ApisService.getApiData(req.api);
responses.success(res, 'api getData')(data);
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};


/**
* @desc MiddleWare to ask the service the api for this id
Expand Down
3 changes: 3 additions & 0 deletions modules/apis/policies/apis.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ exports.invokeRolesPolicies = () => {
}, {
resources: '/api/apis/load/:apiId',
permissions: '*',
}, {
resources: '/api/apis/data/:apiId',
permissions: '*',
}],
}]);
};
22 changes: 22 additions & 0 deletions modules/apis/repositories/apis.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,25 @@ exports.import = (collection, items) => {
},
})));
};


/**
* @desc Function to import list of locations in db
* @param {Object} locations
* @return {Object} locations
*/
exports.getApiData = (collection) => {
const _schema = new mongoose.Schema({}, {
collection,
strict: false,
});

let model;
try {
model = mongoose.model(collection);
} catch (error) {
model = mongoose.model(collection, _schema);
}

return model.find().sort('-createdAt').exec();
};
3 changes: 3 additions & 0 deletions modules/apis/routes/apis.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ module.exports = (app) => {
app.route('/api/apis/load/:apiId')
.get(apis.load);

app.route('/api/apis/data/:apiId')
.get(apis.getApiData);

// Finish by binding the api middleware
app.param('apiId', apis.apiByID);
};
11 changes: 11 additions & 0 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,14 @@ exports.load = async (api) => {
result,
});
};


/**
* @desc Functio to ask repository to get data stocker from apis request
* @param {Object} scrap - original scrap
* @return {Promise} scrap
*/
exports.getApiData = async (api) => {
const result = await ApisRepository.getApiData(api.slug);
return Promise.resolve(result);
};

0 comments on commit 8d82a8e

Please sign in to comment.