Просмотр исходного кода

# Feature/196, 198, 199 Grouping users
* Refactor PageGroupRelation model.
* Fix comments for UserGroupRelation model methods.

Tatsuya Ise 8 лет назад
Родитель
Сommit
fc5c2d2bf6
2 измененных файлов с 238 добавлено и 144 удалено
  1. 217 130
      lib/models/page-group-relation.js
  2. 21 14
      lib/models/user-group-relation.js

+ 217 - 130
lib/models/page-group-relation.js

@@ -1,152 +1,239 @@
-module.exports = function(crowi) {
-  var debug = require('debug')('crowi:models:pageGroupRelation')
-    , mongoose = require('mongoose')
-    , mongoosePaginate = require('mongoose-paginate')
-    , ObjectId = mongoose.Schema.Types.ObjectId
-
-    , PAGE_ITEMS = 50
-
-    , pageGroupRelationSchema;
-
-  pageGroupRelationSchema = new mongoose.Schema({
-    pageGroupRelationId: String,
-    relatedGroup: { type: ObjectId, ref: 'UserGroup', required: true },
-    targetPage: { type: ObjectId, ref: 'Page', required: true },
-    createdAt: { type: Date, default: Date.now },
-  },{
-    toJSON: { getters: true },
-    toObject: { getters: true }
-  });
-  pageGroupRelationSchema.plugin(mongoosePaginate);
-
-  // すべてのグループ所属関係を取得
-  pageGroupRelationSchema.statics.findAllRelation = function() {
-    debug('findAllRelations is called');
-    var PageGroupRelation = this;
-
-    return PageGroupRelation.find({ relatedGroup: group} )
-        .populate('targetPage')
-        .exec();
-  };
+const debug = require('debug')('crowi:models:pageGroupRelation');
+const mongoose = require('mongoose');
+const mongoosePaginate = require('mongoose-paginate');
+const ObjectId = mongoose.Schema.Types.ObjectId;
+
+
+/*
+ * define schema
+ */
+const schema = new mongoose.Schema({
+  relatedGroup: { type: ObjectId, ref: 'UserGroup', required: true },
+  targetPage: { type: ObjectId, ref: 'Page', required: true },
+  createdAt: { type: Date, default: Date.now },
+}, {
+  toJSON: { getters: true },
+  toObject: { getters: true }
+});
+// apply plugins
+schema.plugin(mongoosePaginate);
+
+
+/**
+ * PageGroupRelation Class
+ *
+ * @class PageGroupRelation
+ */
+class PageGroupRelation {
+
+    /**
+   * limit items num for pagination
+   *
+   * @readonly
+   * @static
+   * @memberof PageGroupRelation
+   */
+   static get PAGE_ITEMS() {
+     return 50;
+    }
+
+  static set crowi(crowi) {
+    this._crowi = crowi;
+  }
 
 
-  // 指定グループに対するすべてのグループ所属関係を取得
-  pageGroupRelationSchema.statics.findAllRelationForUserGroup = function (userGroup) {
-    debug('findAllRelation is called', userGroup);
-    var PageGroupRelation = this;
+  static get crowi() {
+    return this._crowi;
+  }
 
 
-    return PageGroupRelation.find({ relatedGroup: userGroup.id })
-        .populate('targetPage')
-        .exec();
-  };
+  /**
+   * find all page and group relation
+   *
+   * @static
+   * @returns {Promise<PageGroupRelation[]>}
+   * @memberof PageGroupRelation
+   */
+  static findAllRelation() {
+
+    return this
+      .find()
+      .populate('targetPage')
+      .exec();
+  }
 
 
-  // ページネーション利用の検索
-  pageGroupRelationSchema.statics.findPageGroupRelationsWithPagination = function (userGroup, options, callback) {
+  /**
+   * find all page and group relation for UserGroup
+   *
+   * @static
+   * @param {UserGroup} userGroup
+   * @returns {Promise<PageGroupRelation[]>}
+   * @memberof PageGroupRelation
+   */
+  static findAllRelationForUserGroup(userGroup) {
+    debug('findAllRelationForUserGroup is called', userGroup);
+
+    return this
+      .find({ relatedGroup: userGroup.id })
+      .populate('targetPage')
+      .exec();
+  }
 
 
-    this.paginate({ relatedGroup: userGroup }, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
-      if (err) {
+  /**
+   * find all entities with pagination
+   *
+   * @see https://github.com/edwardhotchkiss/mongoose-paginate
+   *
+   * @static
+   * @param {UserGroup} userGroup
+   * @param {any} opts mongoose-paginate options object
+   * @returns {Promise<any>} mongoose-paginate result object
+   * @memberof UserGroupRelation
+   */
+  static findPageGroupRelationsWithPagination(userGroup, opts) {
+    const query = { relatedGroup: userGroup };
+    const options = Object.assign({}, opts);
+    if (options.page == null) {
+      options.page = 1;
+    }
+    if (options.limit == null) {
+      options.limit = UserGroupRelation.PAGE_ITEMS;
+    }
+
+    return this.paginate(query, options)
+      .catch((err) => {
         debug('Error on pagination:', err);
         debug('Error on pagination:', err);
-        return callback(err, null);
-      }
-
-      return callback(err, result);
-    });
-  };
-
-  // ページとグループを元に関係性が存在するか確認
-  pageGroupRelationSchema.statics.isExistsRelationForPageAndGroup = function (page, userGroup) {
-    var PageGroupRelation = this
-
-    return new Promise(function (resolve, reject) {
-      PageGroupRelation
-        .count({ targetPage: page.id, relatedGroup: userGroup.id })
-        .exec(function (err, count) {
-          if (err) {
-            return reject(err);
-          }
-          return resolve(0 < count);
-        });
-    });
-  };
-
-  // ページを元に関係性を取得
-  pageGroupRelationSchema.statics.findByPage = function(page) {
-    var PageGroupRelation = this
-
-    return PageGroupRelation.find({ targetPage: page.id })
-        .populate('relatedGroup')
-        .exec();
-  };
+      });
+  }
 
 
-  // 関係性の生成
-  pageGroupRelationSchema.statics.createRelation = function(userGroup, page) {
-    var PageGroupRelation = this
-      , newPageGroupRelation = new PageGroupRelation();
+  /**
+   * get if the page has relation for group
+   *
+   * @static
+   * @param {Page} page
+   * @param {UserGroup} userGroup
+   * @returns {Promise<boolean>} is the relation for page and group exists(or not)
+   * @memberof PageGroupRelation
+   */
+  static isExistsRelationForPageAndGroup(page, userGroup) {
+    const query = { targetPage: page.id, relatedGroup: userGroup.id };
+
+    return this
+      .count(query)
+      .then((count) => {
+        return (0 < count);
+      })
+      .catch((err) => {
+        debug('An Error occured.', err);
+        return reject(err);
+      });
+  }
 
 
-    debug('create new page-group-relation for group ', userGroup);
-    newPageGroupRelation.relatedGroup = userGroup.id;
-    newPageGroupRelation.targetPage = page.id;
-    newPageGroupRelation.createdAt = Date.now();
+  /**
+   * find page and group relation for Page
+   *
+   * @static
+   * @param {Page} page
+   * @returns {Promise<PageGroupRelation[]>}
+   * @memberof PageGroupRelation
+   */
+  static findByPage(page) {
+
+    return this
+      .find({ targetPage: page.id })
+      .populate('relatedGroup')
+      .exec();
+  }
 
 
-    debug('create new page-group-relation ', newPageGroupRelation);
-    return newPageGroupRelation.save();
+  /**
+   * create page and group relation
+   *
+   * @static
+   * @param {any} userGroup
+   * @param {any} page
+   * @returns
+   * @memberof PageGroupRelation
+   */
+  static createRelation(userGroup, page) {
+    return this.create({
+      relatedGroup: userGroup.id,
+      targetPage: page.id,
+    });
   };
   };
 
 
-  // グループに紐づく関係性の全削除
-  pageGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup) {
-    var PageGroupRelation = this
-
-    return PageGroupRelation.findAllRelationForUserGroup(userGroup)
-      .then( function(relations) {
+  /**
+   * remove all relation for UserGroup
+   *
+   * @static
+   * @param {UserGroup} userGroup related group for remove
+   * @returns {Promise<any>}
+   * @memberof PageGroupRelation
+   */
+  static removeAllByUserGroup(userGroup) {
+
+    return this.findAllRelationForUserGroup(userGroup)
+      .then((relations) => {
         if (relations == null) {
         if (relations == null) {
-          resolve();
+          return;
         }
         }
         else {
         else {
-          relations.map(relation => relation.remove());
+          relations.map((relation) => {
+            relation.remove();
+          });
         }
         }
-      })
+      });
   }
   }
 
 
-  // ページに紐づく関係性の全削除
-  pageGroupRelationSchema.statics.removeAllByPage = function (page) {
-    var PageGroupRelation = this
-    debug('removeAllByPage is called', page);
-
-    return PageGroupRelation.findByPage(page)
-    .then(function(relations) {
-      debug('remove relations are ', relations);
-      if (relations == null) {
-        resolve();
-      }
-      else {
-        relations.map(relation => relation.remove());
-      }
-    });
+  /**
+   * remove all relation for Page
+   *
+   * @static
+   * @param {Page} page related page for remove
+   * @returns {Promise<any>}
+   * @memberof PageGroupRelation
+   */
+  static removeAllByPage(page) {
+
+    return this.findByPage(page)
+      .then((relations) => {
+        debug('remove relations are ', relations);
+        if (relations == null) {
+          return;
+        }
+        else {
+          relations.map((relation) => {
+            relation.remove();
+          });
+        }
+      });
   }
   }
 
 
-  // ユーザグループの関係性を削除
-  pageGroupRelationSchema.statics.removeById = function (id, callback) {
-    var PageGroupRelation = this
-    PageGroupRelation.findById(id, function (err, relationData) {
-      if (err) {
-        debug('Error on find a removing user-group-relation', err);
-        return callback(err);
-      }
-      debug('relationData is ', relationData);
-      if (relationData == null || relationData == undefined) {
-        debug('Cannot find user group relation by id', id);
-        return callback(new Error('Cannot find user group relation by id'));
-      }
-
-      relationData.remove(function(err) {
-        if (err) {
-          return callback(err);
+  /**
+   * remove relation by id
+   *
+   * @static
+   * @param {ObjectId} id for remove
+   * @returns {Promise<any>}
+   * @memberof PageGroupRelation
+   */
+  static removeById(id) {
+
+    return this.findById(id)
+      .then((relationData) => {
+        if (relationData == null) {
+          throw new Exception('PageGroupRelation data is not exists. id:', id);
         }
         }
-        return callback(null);
+        else {
+          relationData.remove();
+        }
+      })
+      .catch((err) => {
+        debug('Error on find a removing page-group-relation', err);
+        return reject(err);
       });
       });
-    });
   }
   }
+}
 
 
-  pageGroupRelationSchema.statics.PAGE_ITEMS         = PAGE_ITEMS;
-
-  return mongoose.model('PageGroupRelation', pageGroupRelationSchema);
-};
+module.exports = function (crowi) {
+  PageGroupRelation.crowi = crowi;
+  schema.loadClass(PageGroupRelation);
+  return mongoose.model('PageGroupRelation', schema);
+}

+ 21 - 14
lib/models/user-group-relation.js

@@ -158,6 +158,7 @@ class UserGroupRelation {
   /**
   /**
    * get if the user has relation for group
    * get if the user has relation for group
    *
    *
+   * @static
    * @param {User} userData
    * @param {User} userData
    * @param {UserGroup} userGroup
    * @param {UserGroup} userGroup
    * @returns {Promise<boolean>} is user related for group(or not)
    * @returns {Promise<boolean>} is user related for group(or not)
@@ -171,19 +172,21 @@ class UserGroupRelation {
 
 
     return this
     return this
       .count(query)
       .count(query)
+      .exec()
       .then((count) => {
       .then((count) => {
         // return true or false of the relation is exists(not count)
         // return true or false of the relation is exists(not count)
-        return resolve(0 < count);
+        return (0 < count);
       })
       })
       .catch((err) => {
       .catch((err) => {
         debug('An Error occured.', err);
         debug('An Error occured.', err);
-        return reject(err);
+        reject(err);
       });
       });
   }
   }
 
 
   /**
   /**
    * create user and group relation
    * create user and group relation
    *
    *
+   * @static
    * @param {UserGroup} userGroup
    * @param {UserGroup} userGroup
    * @param {User} user
    * @param {User} user
    * @returns {Promise<UserGroupRelation>} created relation
    * @returns {Promise<UserGroupRelation>} created relation
@@ -197,28 +200,32 @@ class UserGroupRelation {
   }
   }
 
 
   /**
   /**
-   * remove all relation for group
-   * @param {UserGroup} userGroup
+   * remove all relation for UserGroup
+   *
+   * @static
+   * @param {UserGroup} userGroup related group for remove
    * @returns {Promise<any>}
    * @returns {Promise<any>}
    * @memberof UserGroupRelation
    * @memberof UserGroupRelation
    */
    */
   static removeAllByUserGroup(userGroup) {
   static removeAllByUserGroup(userGroup) {
 
 
     return this.findAllRelationForUserGroup(userGroup)
     return this.findAllRelationForUserGroup(userGroup)
-    .then((relations) => {
-      if (relations == null) {
-        return;
-      }
-      else {
-        relations.map((relation) => {
-          relation.remove();
-        });
-      }
-    });
+      .then((relations) => {
+        if (relations == null) {
+          return;
+        }
+        else {
+          relations.map((relation) => {
+            relation.remove();
+          });
+        }
+      });
   }
   }
 
 
   /**
   /**
    * remove relation by id
    * remove relation by id
+   *
+   * @static
    * @param {ObjectId} id
    * @param {ObjectId} id
    * @returns {Promise<any>}
    * @returns {Promise<any>}
    * @memberof UserGroupRelation
    * @memberof UserGroupRelation