From b3c254a3494418795fe0bb533abcb25cdd47a261 Mon Sep 17 00:00:00 2001 From: Pierre Brisorgueil Date: Thu, 26 Mar 2020 15:13:23 +0100 Subject: [PATCH] =?UTF-8?q?feat(apis):=20set=20api=20data=20request=20v0?= =?UTF-8?q?=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/apis/controllers/apis.controller.js | 22 ++++++++++++-- modules/apis/repositories/apis.repository.js | 30 ++++++++++++++++++-- modules/apis/routes/apis.routes.js | 3 +- modules/apis/services/apis.service.js | 14 +++++++-- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/modules/apis/controllers/apis.controller.js b/modules/apis/controllers/apis.controller.js index f1fbfd9f7..e2c243d8b 100644 --- a/modules/apis/controllers/apis.controller.js +++ b/modules/apis/controllers/apis.controller.js @@ -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); diff --git a/modules/apis/repositories/apis.repository.js b/modules/apis/repositories/apis.repository.js index 4d4ec4570..27812fd5a 100644 --- a/modules/apis/repositories/apis.repository.js +++ b/modules/apis/repositories/apis.repository.js @@ -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, @@ -89,5 +113,5 @@ exports.getApiData = (collection) => { model = mongoose.model(collection, _schema); } - return model.find().sort('-updatedAt').exec(); + return model.findOne(filters).exec(); }; diff --git a/modules/apis/routes/apis.routes.js b/modules/apis/routes/apis.routes.js index eb85fda7c..6c1f09c43 100644 --- a/modules/apis/routes/apis.routes.js +++ b/modules/apis/routes/apis.routes.js @@ -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); diff --git a/modules/apis/services/apis.service.js b/modules/apis/services/apis.service.js index 7ce60e513..5973af815 100644 --- a/modules/apis/services/apis.service.js +++ b/modules/apis/services/apis.service.js @@ -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); };