IGNITE-3262 Added JSDoc,  extracted private methods, changed service inject 
name.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/a2657388
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/a2657388
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/a2657388

Branch: refs/heads/ignite-3262
Commit: a265738875a8b6a07fd2628b7518260714826745
Parents: 020c96a
Author: Maxim Afanasiev <[email protected]>
Authored: Thu Jun 30 10:26:58 2016 +0700
Committer: Maxim Afanasiev <[email protected]>
Committed: Thu Jun 30 10:26:58 2016 +0700

----------------------------------------------------------------------
 .../src/main/js/serve/routes/caches.js          |   7 +-
 .../src/main/js/serve/services/cache.js         | 171 +++++++++++--------
 2 files changed, 104 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/a2657388/modules/web-console/src/main/js/serve/routes/caches.js
----------------------------------------------------------------------
diff --git a/modules/web-console/src/main/js/serve/routes/caches.js 
b/modules/web-console/src/main/js/serve/routes/caches.js
index 784d872..aa25288 100644
--- a/modules/web-console/src/main/js/serve/routes/caches.js
+++ b/modules/web-console/src/main/js/serve/routes/caches.js
@@ -21,7 +21,7 @@
 
 module.exports = {
     implements: 'caches-routes',
-    inject: ['require(lodash)', 'require(express)', 'mongo', 
'services/cacheService']
+    inject: ['require(lodash)', 'require(express)', 'mongo', 'services/cache']
 };
 
 module.exports.factory = function(_, express, mongo, cacheService) {
@@ -30,14 +30,11 @@ module.exports.factory = function(_, express, mongo, 
cacheService) {
 
         /**
          * Get spaces and caches accessed for user account.
-         *
-         * @param req Request.
-         * @param res Response.
          */
         router.post('/list', (req, res) => {
             cacheService.listByUser(req.currentUserId(), 
req.header('IgniteDemoMode'))
                 .then(res.api.ok)
-                .catch(req.api.error);
+                .catch(res.api.error);
         });
 
         /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/a2657388/modules/web-console/src/main/js/serve/services/cache.js
----------------------------------------------------------------------
diff --git a/modules/web-console/src/main/js/serve/services/cache.js 
b/modules/web-console/src/main/js/serve/services/cache.js
index ff867ff..c097f78 100644
--- a/modules/web-console/src/main/js/serve/services/cache.js
+++ b/modules/web-console/src/main/js/serve/services/cache.js
@@ -20,105 +20,138 @@
 // Fire me up!
 
 module.exports = {
-    implements: 'services/cacheService',
+    implements: 'services/cache',
     inject: ['require(lodash)',
-        'require(express)',
         'mongo',
         'errors']
 };
 
-module.exports.factory = (_, express, mongo, errors) => {
+module.exports.factory = (_, mongo, errors) => {
+
+
+    /**
+     * Convert remove status operation to own presentation.
+     * @param {RemoveResult} result - The results of remove operation.
+     */
+    const convertRemoveStatus = ({result}) => ({rowsAffected: result.n});
+
+    /**
+     * Update existing cache
+     * @param {Object} cache - The cache for updating
+     * @returns {Promise.<Integer>} that resolves cache id
+     */
+    const update = (cache) => {
+        const cacheId = cache._id;
+
+        return mongo.Cache.update({_id: cacheId}, cache, {upsert: true}).exec()
+            .then(() => mongo.Cluster.update({_id: {$in: cache.clusters}}, 
{$addToSet: {caches: cacheId}}, {multi: true}).exec())
+            .then(() => mongo.Cluster.update({_id: {$nin: cache.clusters}}, 
{$pull: {caches: cacheId}}, {multi: true}).exec())
+            .then(() => mongo.DomainModel.update({_id: {$in: cache.domains}}, 
{$addToSet: {caches: cacheId}}, {multi: true}).exec())
+            .then(() => mongo.DomainModel.update({_id: {$nin: cache.domains}}, 
{$pull: {caches: cacheId}}, {multi: true}).exec())
+            .then(() => cacheId);
+    };
+
+    /**
+     * Create new cache.
+     * @param {Object} cache - The cache for creation.
+     * @returns {Promise.<Integer>} that resolves cache id.
+     */
+    const create = (cache) => {
+        return mongo.Cache.create(cache)
+            .then((createdCache) =>
+                mongo.Cluster.update({_id: {$in: createdCache.clusters}}, 
{$addToSet: {caches: createdCache._id}}, {multi: true}).exec()
+                    .then(() => mongo.DomainModel.update({_id: {$in: 
createdCache.domains}}, {$addToSet: {caches: createdCache._id}}, {multi: 
true}).exec())
+                    .then(() => createdCache._id)
+            )
+    };
+
+    /**
+     * Remove all caches by space ids.
+     * @param {Integer[]} spaceIds - The space ids for cache deletion.
+     * @returns {Promise.<Integer>} that resolves number of affected rows.
+     */
+    const removeAllBySpaces = (spaceIds) => {
+        return mongo.Cluster.update({space: {$in: spaceIds}}, {caches: []}, 
{multi: true}).exec()
+            .then(() => mongo.DomainModel.update({space: {$in: spaceIds}}, 
{caches: []}, {multi: true}).exec())
+            .then(() => mongo.Cache.remove({space: {$in: spaceIds}}).exec())
+            .then(convertRemoveStatus)
+    };
+
+    /**
+     * Load cache by its name and space.
+     * @param {Object} cache - The cache object for load.
+     * @returns {Promise.<mongo.Cache>|null} that resolves cache if its exits, 
or null if not exitst.
+     */
+    const loadExistingCache = (cache) => {
+        return mongo.Cache.findOne({space: cache.space, name: 
cache.name}).exec();
+    };
 
     class CacheService {
-                
-        static update(cache) {
-            const cacheId = cache._id;
-
-            return mongo.Cache.update({_id: cacheId}, cache, {upsert: 
true}).exec()
-                .then(() => mongo.Cluster.update({_id: {$in: cache.clusters}}, 
{$addToSet: {caches: cacheId}}, {multi: true}).exec())
-                .then(() => mongo.Cluster.update({_id: {$nin: 
cache.clusters}}, {$pull: {caches: cacheId}}, {multi: true}).exec())
-                .then(() => mongo.DomainModel.update({_id: {$in: 
cache.domains}}, {$addToSet: {caches: cacheId}}, {multi: true}).exec())
-                .then(() => mongo.DomainModel.update({_id: {$nin: 
cache.domains}}, {$pull: {caches: cacheId}}, {multi: true}).exec())
-                .then(() => cacheId);
-        }
-
-        static create(cache) {
-            // TODO: Replace to mongo.Cache.create()
-            return (new mongo.Cache(cache)).save()
-                .then((cache) =>
-                    mongo.Cluster.update({_id: {$in: cache.clusters}}, 
{$addToSet: {caches: cache._id}}, {multi: true}).exec()
-                        .then(() => mongo.DomainModel.update({_id: {$in: 
cache.domains}}, {$addToSet: {caches: cache._id}}, {multi: true}).exec())
-                        .then(() => cache._id)
-                )
-        }
-
+        /**
+         * Create or update cache.
+         * @param {Object} cache - The cache.
+         * @returns {Promise.<Integer>} that resolves cache id of merge 
operation.
+         */
         static merge(cache) {
-            return this.loadByName(cache)
+            return loadExistingCache(cache)
                 .then((existingCache) => {
                     const cacheId = cache._id;
 
                     if (existingCache && cacheId !== 
existingCache._id.toString())
                         throw new errors.DuplicateKeyException('Cache with 
name: "' + existingCache.name + '" already exist.');
 
-                    if (cacheId) {
-                        return this.update(cache);
-                    }
+                    if (cacheId)
+                        return update(cache);
 
-                    return this.create(cache);
+                    return create(cache);
                 });
         }
 
-        static loadByName(cache) {
-            return mongo.Cache.findOne({space: cache.space, name: 
cache.name}).exec();
-        };
-
+        /**
+         * Get caches and linked objects by user.
+         * @param {Integer} userId - The user id that own caches.
+         * @param {Boolean} demo - The flag indicates that need lookup in demo 
space.
+         * @returns {Promise.<[mongo.Cache[], mongo.Cluster[], 
mongo.DomainModel[], mongo.Space[]]>} - contains requested caches and array of 
linked objects: clusters, domains, spaces.
+         */
         static listByUser(userId, demo) {
-            const result = {};
-            let spaceIds = [];
-
             // Get owned space and all accessed space.
             return mongo.spaces(userId, demo)
                 .then((spaces) => {
-                    result.spaces = spaces;
-                    spaceIds = spaces.map((space) => space._id);
-
-                    return mongo.Cluster.find({space: {$in: 
spaceIds}}).sort('name').lean().exec();
-                })
-                .then((clusters) => {
-                    result.clusters = clusters;
-
-                    return mongo.DomainModel.find({space: {$in: 
spaceIds}}).sort('name').lean().exec();
-                })
-                .then((domains) => {
-                    result.domains = domains;
-
-                    return mongo.Cache.find({space: {$in: 
spaceIds}}).sort('name').lean().exec();
-                })
-                .then((caches) => {
-                    result.caches = caches;
-
-                    return result;
-                })
+                    const spaceIds = spaces.map((space) => space._id);
+
+                    return Promise.all([
+                        mongo.Cluster.find({space: {$in: 
spaceIds}}).sort('name').lean().exec(),
+                        mongo.DomainModel.find({space: {$in: 
spaceIds}}).sort('name').lean().exec(),
+                        mongo.Cache.find({space: {$in: 
spaceIds}}).sort('name').lean().exec()
+                    ])
+                        .then(([clusters, domains, caches]) => ({caches, 
clusters, domains, spaces}));
+                });
         }
 
+        /**
+         * Remove cache.
+         * @param {Object} cache - The cache object with _id property.
+         * @returns {Promise.<{rowsAffected}>} - The number of affected rows.
+         */
         static remove(cache) {
             const cacheId = cache._id;
 
             return mongo.Cluster.update({caches: {$in: [cacheId]}}, {$pull: 
{caches: cacheId}}, {multi: true}).exec()
                 .then(() => mongo.DomainModel.update({caches: {$in: 
[cacheId]}}, {$pull: {caches: cacheId}}, {multi: true}).exec())
                 .then(() => mongo.Cache.remove(cache).exec())
-                .then(() => ({}))
-        };
-
-        static removeAll(user, demo) {
-            return mongo.spaceIds(user, demo)
-                .then((spaceIds) =>
-                    mongo.Cluster.update({space: {$in: spaceIds}}, {caches: 
[]}, {multi: true}).exec()
-                        .then(() => mongo.DomainModel.update({space: {$in: 
spaceIds}}, {caches: []}, {multi: true}).exec())
-                        .then(() => mongo.Cache.remove({space: {$in: 
spaceIds}}).exec())
-                        .then(() => ({}))
-                );
-        };
+                .then(convertRemoveStatus);
+        }
+
+        /**
+         * Remove all caches by user.
+         * @param {Integer} userId - The user id that own caches.
+         * @param {Boolean} demo - The flag indicates that need lookup in demo 
space.
+         * @returns {Promise.<{rowsAffected}>} - The number of affected rows.
+         */
+        static removeAll(userId, demo) {
+            return mongo.spaceIds(userId, demo)
+                .then(removeAllBySpaces);
+        }
     }
 
     return CacheService;

Reply via email to