yusuketk 7 лет назад
Родитель
Сommit
591f07962c
2 измененных файлов с 14 добавлено и 326 удалено
  1. 8 212
      src/server/models/page-tag-relation.js
  2. 6 114
      src/server/models/tag.js

+ 8 - 212
src/server/models/page-tag-relation.js

@@ -1,4 +1,3 @@
-const debug = require('debug')('growi:models:pageTagRelation');
 const mongoose = require('mongoose');
 const mongoosePaginate = require('mongoose-paginate');
 const ObjectId = mongoose.Schema.Types.ObjectId;
@@ -28,17 +27,6 @@ schema.plugin(mongoosePaginate);
  */
 class PageTagRelation {
 
-  // /**
-  //  * limit items num for pagination
-  //  *
-  //  * @readonly
-  //  * @static
-  //  * @memberof UserGroupRelation
-  //  */
-  // static get PAGE_ITEMS() {
-  //   return 50;
-  // }
-
   static set crowi(crowi) {
     this._crowi = crowi;
   }
@@ -77,7 +65,7 @@ class PageTagRelation {
    *
    * @static
    * @returns {Promise<PageTagRelation[]>}
-   * @memberof UserGroupRelation
+   * @memberof PageTagRelation
    */
   static findAllRelation() {
     return this
@@ -88,11 +76,11 @@ class PageTagRelation {
   }
 
   /**
-   * find all tag of page
+   * find all tag related to the page
    *
    * @static
    * @param {page} page
-   * @returns {ObjectId}
+   * @returns {Promise<ObjectId[]>}
    * @memberof PageTagRelation
    */
   static findAllTagIdForPage(page) {
@@ -107,11 +95,11 @@ class PageTagRelation {
   }
 
   /**
-   * find all page relate tag
+   * find all page related to the tag
    *
    * @static
    * @param {tag} tag
-   * @returns {ObjectId}
+   * @returns {Promise<ObjectId[]>}
    * @memberof PageTagRelation
    */
   static findAllPageIdForTag(tag) {
@@ -127,164 +115,8 @@ class PageTagRelation {
     });
   }
 
-  // /**
-  //  * find all user and group relation of UserGroups
-  //  *
-  //  * @static
-  //  * @param {UserGroup[]} userGroups
-  //  * @returns {Promise<UserGroupRelation[]>}
-  //  * @memberof UserGroupRelation
-  //  */
-  // static findAllRelationForUserGroups(userGroups) {
-  //   return this
-  //     .find({
-  //       relatedGroup: {
-  //         $in: userGroups
-  //       }
-  //     })
-  //     .populate('relatedUser')
-  //     .exec();
-  // }
-
-  // /**
-  //  * find all user and group relation of User
-  //  *
-  //  * @static
-  //  * @param {User} user
-  //  * @returns {Promise<UserGroupRelation[]>}
-  //  * @memberof UserGroupRelation
-  //  */
-  // static findAllRelationForUser(user) {
-  //   return this
-  //     .find({
-  //       relatedUser: user.id
-  //     })
-  //     .populate('relatedGroup')
-  //     // filter documents only relatedGroup is not null
-  //     .then(userGroupRelations => {
-  //       return userGroupRelations.filter(relation => {
-  //         return relation.relatedGroup != null;
-  //       });
-  //     });
-  // }
-
-  // /**
-  //  * find all UserGroup IDs that related to specified User
-  //  *
-  //  * @static
-  //  * @param {User} user
-  //  * @returns {Promise<ObjectId[]>}
-  //  */
-  // static async findAllUserGroupIdsRelatedToUser(user) {
-  //   const relations = await this.find({
-  //       relatedUser: user.id
-  //     })
-  //     .select('relatedGroup')
-  //     .exec();
-
-  //   return relations.map(relation => relation.relatedGroup);
-  // }
-
-  // /**
-  //  * 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 findUserGroupRelationsWithPagination(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);
-  //     });
-  // }
-
-  // /**
-  //  * count by related page id and related tag
-  //  *
-  //  * @static
-  //  * @param {string} userPageId find query param for relatedPage
-  //  * @param {User} tagId find query param for relatedTag
-  //  * @returns {Promise<number>}
-  //  */
-  // static async countByPageIdAndTag(pageId, tagId) {
-  //   const query = {
-  //     relatedPage: pageId,
-  //     relatedTag: tagId
-  //   };
-
-  //   return this.count(query);
-  // }
-
-  // /**
-  //  * find all "not" related user for UserGroup
-  //  *
-  //  * @static
-  //  * @param {UserGroup} userGroup for find users not related
-  //  * @returns {Promise<User>}
-  //  * @memberof UserGroupRelation
-  //  */
-  // static findUserByNotRelatedGroup(userGroup) {
-  //   const User = UserGroupRelation.crowi.model('User');
-
-  //   return this.findAllRelationForUserGroup(userGroup)
-  //     .then((relations) => {
-  //       const relatedUserIds = relations.map((relation) => {
-  //         return relation.relatedUser.id;
-  //       });
-  //       const query = {
-  //         _id: {
-  //           $nin: relatedUserIds
-  //         },
-  //         status: User.STATUS_ACTIVE
-  //       };
-
-  //       debug('findUserByNotRelatedGroup ', query);
-  //       return User.find(query).exec();
-  //     });
-  // }
-
-  // /**
-  //  * get if the user has relation for group
-  //  *
-  //  * @static
-  //  * @param {User} userData
-  //  * @param {UserGroup} userGroup
-  //  * @returns {Promise<boolean>} is user related for group(or not)
-  //  * @memberof UserGroupRelation
-  //  */
-  // static isRelatedUserForGroup(userData, userGroup) {
-  //   const query = {
-  //     relatedGroup: userGroup.id,
-  //     relatedUser: userData.id
-  //   };
-
-  //   return this
-  //     .count(query)
-  //     .exec()
-  //     .then((count) => {
-  //       // return true or false of the relation is exists(not count)
-  //       return (0 < count);
-  //     });
-  // }
-
   /**
-   * create tag and page relation
+   * create a page tag relation
    *
    * @static
    * @param {Page} page
@@ -299,26 +131,12 @@ class PageTagRelation {
     });
   }
 
-  // /**
-  //  * remove all relation for Page
-  //  *
-  //  * @static
-  //  * @param {Page} page related page for remove
-  //  * @returns {Promise<any>}
-  //  * @memberof PageTagRelation
-  //  */
-  // static removeAllByPage(page) {
-  //   return this.deleteMany({
-  //     relatedPage: page
-  //   });
-  // }
-
   /**
    * remove relation by id
    *
    * @static
    * @param {ObjectId} id
-   * @returns {Promise<any>}
+   * @returns {Promise<PageTagRelation>}
    * @memberof PageTagRelation
    */
   static removeByEachId(pageId, tagId) {
@@ -327,32 +145,10 @@ class PageTagRelation {
         if (err) {
           reject(err);
         }
-        resolve();
+        resolve(removedData);
       });
     });
   }
-
-  // /**
-  //  * remove relation by id
-  //  *
-  //  * @static
-  //  * @param {ObjectId} id
-  //  * @returns {Promise<any>}
-  //  * @memberof UserGroupRelation
-  //  */
-  // static removeById(id) {
-
-  //   return this.findById(id)
-  //     .then((relationData) => {
-  //       if (relationData == null) {
-  //         throw new Error('PageTagRelation data is not exists. id:', id);
-  //       }
-  //       else {
-  //         relationData.remove();
-  //       }
-  //     });
-  // }
-
 }
 
 module.exports = function(crowi) {

+ 6 - 114
src/server/models/tag.js

@@ -1,41 +1,22 @@
-module.exports = function (crowi) {
+module.exports = function(crowi) {
   var debug = require('debug')('growi:models:tag'),
     mongoose = require('mongoose'),
     ObjectId = mongoose.Schema.Types.ObjectId,
-    // USER_PUBLIC_FIELDS = '_id name username createdAt',
     USER_PUBLIC_FIELDS = '_id name',
     tagSchema;
 
   tagSchema = new mongoose.Schema({
-    // page: {
-    //   type: ObjectId,
-    //   ref: 'Page',
-    //   index: true
-    // },
-    // creator: {
-    //   type: ObjectId,
-    //   ref: 'User',
-    //   index: true
-    // },
-    // revision: {
-    //   type: ObjectId,
-    //   ref: 'Revision',
-    //   index: true
-    // },
     name: {
       type: String,
       required: true
     },
-    // createdAt: {
-    //   type: Date,
-    //   default: Date.now
-    // },
   });
 
   /**
    * create a tag (Promise wrapper)
    */
   tagSchema.statics.createTag = function(tag) {
+    debug('create new tag:', tag);
     return new Promise((resolve, reject) => {
       this.create({name: tag}, function(err, createdTag) {
         if (err) {
@@ -46,7 +27,9 @@ module.exports = function (crowi) {
     });
   };
 
-  // get a tag by id (Promise wrpper)
+  /**
+   * get a tag by id(Promise wrpper)
+   */
   tagSchema.statics.getOneById = function(id) {
     return new Promise((resolve, reject) => {
       this.find({
@@ -60,106 +43,15 @@ module.exports = function (crowi) {
     });
   };
 
-//   commentSchema.statics.getCommentsByPageId = function (id) {
-//     var self = this;
-
-//     return new Promise(function (resolve, reject) {
-//       self
-//         .find({
-//           page: id
-//         })
-//         .sort({
-//           'createdAt': -1
-//         })
-//         .populate('creator', USER_PUBLIC_FIELDS)
-//         .exec(function (err, data) {
-//           if (err) {
-//             return reject(err);
-//           }
-
-//           if (data.length < 1) {
-//             return resolve([]);
-//           }
-
-//           //debug('Comment loaded', data);
-//           return resolve(data);
-//         });
-//     });
-//   };
-
-//   commentSchema.statics.getCommentsByRevisionId = function (id) {
-//     var self = this;
-
-//     return new Promise(function (resolve, reject) {
-//       self
-//         .find({
-//           revision: id
-//         })
-//         .sort({
-//           'createdAt': -1
-//         })
-//         .populate('creator', USER_PUBLIC_FIELDS)
-//         .exec(function (err, data) {
-//           if (err) {
-//             return reject(err);
-//           }
-
-//           if (data.length < 1) {
-//             return resolve([]);
-//           }
-
-//           debug('Comment loaded', data);
-//           return resolve(data);
-//         });
-//     });
-//   };
-
-//   commentSchema.statics.countCommentByPageId = function (page) {
-//     var self = this;
-
-//     return new Promise(function (resolve, reject) {
-//       self.count({
-//         page: page
-//       }, function (err, data) {
-//         if (err) {
-//           return reject(err);
-//         }
-
-//         return resolve(data);
-//       });
-//     });
-//   };
-
   tagSchema.statics.removeById = function(tagId) {
     const Tag = this;
     Tag.remove({_id: tagId}, function(err, done) {
       if (err) {
         throw new Error(err);
       }
+      debug('deleta tag:', tagId);
     });
   };
 
-  tagSchema.statics.removeInactiveTag = function(tagId) {
-    const Tag = this;
-    Tag.remove({_id: tagId}, function(err, done) {
-      if (err) {
-        throw new Error(err);
-      }
-    });
-  };
-
-//   /**
-//    * post save hook
-//    */
-//   commentSchema.post('save', function (savedComment) {
-//     var Page = crowi.model('Page'),
-//       Comment = crowi.model('Comment');
-
-//     Page.updateCommentCount(savedComment.page)
-//       .then(function (page) {
-//         debug('CommentCount Updated', page);
-//       }).catch(function () {});
-//   });
-
   return mongoose.model('Tag', tagSchema);
 };