Skip to content

Commit

Permalink
feat(apis): set api data request v0 ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 26, 2020
1 parent 5468277 commit b3c254a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
22 changes: 19 additions & 3 deletions modules/apis/controllers/apis.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,30 @@ exports.load = async (req, res) => {
};

/**
* @desc Endpoint to getData stocked from load
* @desc Endpoint to getData stocked from load on apis
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.getApiData = async (req, res) => {
exports.listApi = 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);
const data = await ApisService.listApi(req.api);
responses.success(res, 'api getData')(data);
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};


/**
* @desc Endpoint to getData stocked from load on apis
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.getApi = 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.getApi(req.api, req.body);
responses.success(res, 'api getData')(data);
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
Expand Down
30 changes: 27 additions & 3 deletions modules/apis/repositories/apis.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,35 @@ exports.import = (collection, items) => {


/**
* @desc Function to import list of locations in db
* @desc Function to get api data from db
* @param {string} colletion name
* @return [{Object}] data
*/
exports.listApi = (collection) => {
const _schema = new mongoose.Schema({}, {
collection,
strict: false,
timestamps: true,
});

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

return model.find().sort('-updatedAt').exec();
};


/**
* @desc Function to ask for api data in db
* @param {Object} locations
* @return {Object} locations
*/
exports.getApiData = (collection) => {
exports.getApi = (collection, filters) => {
console.log(collection, filters);
const _schema = new mongoose.Schema({}, {
collection,
strict: false,
Expand All @@ -89,5 +113,5 @@ exports.getApiData = (collection) => {
model = mongoose.model(collection, _schema);
}

return model.find().sort('-updatedAt').exec();
return model.findOne(filters).exec();
};
3 changes: 2 additions & 1 deletion modules/apis/routes/apis.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module.exports = (app) => {
.get(apis.load);

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

// Finish by binding the api middleware
app.param('apiId', apis.apiByID);
Expand Down
14 changes: 12 additions & 2 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,17 @@ exports.load = async (api, start) => {
* @param {Object} scrap - original scrap
* @return {Promise} scrap
*/
exports.getApiData = async (api) => {
const result = await ApisRepository.getApiData(api.slug);
exports.listApi = async (api) => {
const result = await ApisRepository.listApi(api.slug);
return Promise.resolve(result);
};

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

0 comments on commit b3c254a

Please sign in to comment.