From 8efe84447436f2b5e04eb3bcb9d76dd6009f05a0 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 23 May 2016 21:37:06 +0200 Subject: [PATCH] Use generic adapter --- lib/cartodb/controllers/map.js | 32 ++++++------------- lib/cartodb/models/mapconfig/adapter/index.js | 26 +++++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 lib/cartodb/models/mapconfig/adapter/index.js diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index ecce9632..4646eac3 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -20,6 +20,7 @@ var AnalysisMapConfigAdapter = require('../models/mapconfig/adapter/analysis-map var NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider'); var CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider'); var SqlWrapMapConfigAdapter = require('../models/mapconfig/adapter/sql-wrap-mapconfig-adapter'); +var MapConfigAdapter = require('../models/mapconfig/adapter'); /** * @param {AuthApi} authApi @@ -54,6 +55,14 @@ function MapController(authApi, pgConnection, templateMaps, mapBackend, metadata this.namedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps, pgConnection); this.overviewsAdapter = overviewsAdapter; this.sqlWrapMapConfigAdapter = new SqlWrapMapConfigAdapter(); + + this.mapConfigAdapter = new MapConfigAdapter([ + this.sqlWrapMapConfigAdapter, + this.analysisMapConfigAdapter, + this.namedLayersAdapter, + this.overviewsAdapter, + this.turboCartoAdapter + ]); } util.inherits(MapController, BaseController); @@ -135,7 +144,6 @@ MapController.prototype.create = function(req, res, prepareConfigFn) { var mapConfig; - var user = req.context.user; var context = {}; step( @@ -158,27 +166,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn) { apiKey: req.params.api_key } }; - return requestMapConfig; - }, - function prepareSqlWrap(err, requestMapConfig) { - assert.ifError(err); - self.sqlWrapMapConfigAdapter.getMapConfig(user, requestMapConfig, req.params, context, this); - }, - function prepareAnalysisLayers(err, requestMapConfig) { - assert.ifError(err); - self.analysisMapConfigAdapter.getMapConfig(user, requestMapConfig, req.params, context, this); - }, - function beforeLayergroupCreate(err, requestMapConfig) { - assert.ifError(err); - self.namedLayersAdapter.getMapConfig(user, requestMapConfig, req.params, context, this); - }, - function addOverviewsInformation(err, requestMapConfig) { - assert.ifError(err); - self.overviewsAdapter.getMapConfig(req.context.user, requestMapConfig, req.params, context, this); - }, - function parseTurboCarto(err, requestMapConfig) { - assert.ifError(err); - self.turboCartoAdapter.getMapConfig(req.context.user, requestMapConfig, req.params, context, this); + self.mapConfigAdapter.getMapConfig(req.context.user, requestMapConfig, req.params, context, this); }, function createLayergroup(err, requestMapConfig) { assert.ifError(err); diff --git a/lib/cartodb/models/mapconfig/adapter/index.js b/lib/cartodb/models/mapconfig/adapter/index.js new file mode 100644 index 00000000..f7b49ebc --- /dev/null +++ b/lib/cartodb/models/mapconfig/adapter/index.js @@ -0,0 +1,26 @@ +'use strict'; + +function MapConfigAdapter(adapters) { + this.adapters = adapters; +} + +module.exports = MapConfigAdapter; + +MapConfigAdapter.prototype.getMapConfig = function(user, requestMapConfig, params, context, callback) { + var self = this; + var i = 0; + var tasksLeft = this.adapters.length; + + function next(err, _requestMapConfig) { + if (err) { + return callback(err); + } + if (tasksLeft-- === 0) { + return callback(null, _requestMapConfig); + } + var nextAdapter = self.adapters[i++]; + nextAdapter.getMapConfig(user, _requestMapConfig, params, context, next); + } + + next(null, requestMapConfig); +};