Browse Source

recreate Tag model

yusuketk 7 years ago
parent
commit
0ded09ed02
1 changed files with 143 additions and 117 deletions
  1. 143 117
      src/server/models/tag.js

+ 143 - 117
src/server/models/tag.js

@@ -1,118 +1,144 @@
-const debug = require('debug')('growi:models:tag');
-const mongoose = require('mongoose');
-const mongoosePaginate = require('mongoose-paginate');
-const ObjectId = mongoose.Schema.Types.ObjectId;
-
-
-/*
- * define schema
- */
-const schema = new mongoose.Schema({
-  name: { type: String, required: true, unique: true },
-});
-schema.plugin(mongoosePaginate);
-
-class Tag {
-
-  /**
-   * public fields for Tag model
-   *
-   * @readonly
-   * @static
-   * @memberof Tag
-   */
-  static get TAG_PUBLIC_FIELDS() {
-    return '_id name';
-  }
-
-  // /**
-  //  * limit items num for pagination
-  //  *
-  //  * @readonly
-  //  * @static
-  //  * @memberof UserGroup
-  //  */
-  // static get PAGE_ITEMS() {
-  //   return 10;
-  // }
-
-  /*
-   * model static methods
-   */
-
-  // // すべてのタグを取得(オプション指定可)
-  // static findAllTags(option) {
-  //   return this.find().exec();
-  // }
-
-  // /**
-  //  * find all entities with pagination
-  //  *
-  //  * @see https://github.com/edwardhotchkiss/mongoose-paginate
-  //  *
-  //  * @static
-  //  * @param {any} opts mongoose-paginate options object
-  //  * @returns {Promise<any>} mongoose-paginate result object
-  //  * @memberof Tag
-  //  */
-  // static findTagsWithPagination(opts) {
-  //   const query = {};
-  //   const options = Object.assign({}, opts);
-  //   if (options.page == null) {
-  //     options.page = 1;
-  //   }
-  //   if (options.limit == null) {
-  //     options.limit = Tag.PAGE_ITEMS;
-  //   }
-
-  //   return this.paginate(query, options)
-  //     .catch((err) => {
-  //       debug('Error on pagination:', err);
-  //     });
-  // }
-
-  // // 作成可能なタグ名かの判別
-  // static isRegisterableName(name) {
-  //   const query = { name: name };
-
-  //   return this.findOne(query)
-  //     .then((userGroupData) => {
-  //       return (userGroupData == null);
-  //     });
-  // }
-
-  // // タグ削除
-  // static removeTag(name) {
-  //   const PageTagRelation = mongoose.model('PageTagRelation');
-
-  //   let removed = undefined;
-  //   return this.find({name: name})
-  //     .then(pageTagData => {
-  //       if (pageTagData == null) {
-  //         throw new Exception('UserGroup data is not exists. id:', id);
-  //       }
-  //       return pageTagData.remove();
-  //     })
-  //     .then(removedPageTagData => {
-  //       removed = removedPageTagData;
-  //     })
-  //     // remove relations
-  //     .then(() => {
-  //       return Promise.all([
-  //         PageTagRelation.removeAllByUserGroup(removed),
-  //       ]);
-  //     });
-  // }
-
-  // タグ生成
-  static createTag(name) {
-    return this.create({name: name});
-  }
-}
-
-module.exports = function(crowi) {
-  Tag.crowi = crowi;
-  schema.loadClass(Tag);
-  return mongoose.model('Tag', schema);
+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
+    // },
+  });
+
+  tagSchema.statics.settingTags = function(page, tag) {
+    const PageTagRelation = crowi.model('PageTagRelation');
+    return this.create({name: tag}, function(err, createdTag) {
+      // console.log(createdTag);
+      // Relation を作成
+      PageTagRelation.createRelation(page.id, createdTag.id);
+    });
+  };
+
+//   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);
+//       });
+//     });
+//   };
+
+//   commentSchema.statics.removeCommentsByPageId = function (pageId) {
+//     var Comment = this;
+
+//     return new Promise(function (resolve, reject) {
+//       Comment.remove({
+//         page: pageId
+//       }, function (err, done) {
+//         if (err) {
+//           return reject(err);
+//         }
+
+//         resolve(done);
+//       });
+//     });
+//   };
+
+//   /**
+//    * 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);
 };
 };
-