yusuketk 7 tahun lalu
induk
melakukan
a440b69080
2 mengubah file dengan 17 tambahan dan 19 penghapusan
  1. 0 17
      src/server/models/tag.js
  2. 17 2
      src/server/routes/tag.js

+ 0 - 17
src/server/models/tag.js

@@ -30,23 +30,6 @@ class Tag {
     return tag;
   }
 
-  static async findList(opt) {
-    const PageTagRelation = Tag.crowi.model('PageTagRelation');
-    const list = await PageTagRelation.createTagListWithCount(opt);
-
-    // get tag document for add name data to the list
-    const tags = await this.find({ _id: { $in: list } });
-
-    // add name data
-    const result = list.map((elm) => {
-      const tag = tags.find((tag) => { return (tag.id === String(elm._id)) });
-      elm.name = tag.name;
-      return elm;
-    });
-
-    return result;
-  }
-
 }
 
 module.exports = function(crowi) {

+ 17 - 2
src/server/routes/tag.js

@@ -1,6 +1,7 @@
 module.exports = function(crowi, app) {
 
   const Tag = crowi.model('Tag');
+  const PageTagRelation = crowi.model('PageTagRelation');
   const ApiResponse = require('../util/apiResponse');
   const actions = {};
   const api = {};
@@ -38,9 +39,23 @@ module.exports = function(crowi, app) {
     const sortOpt = { count: -1 };
     const queryOptions = { offset, limit, sortOpt };
     const result = {};
+
     try {
-      result.tags = await Tag.findList(queryOptions);
-      result.totalCount = await Tag.count({});
+      // get tag list contains id and count properties
+      const list = await PageTagRelation.createTagListWithCount(queryOptions);
+
+      // get tag documents for add name data to the list
+      const tags = await Tag.find({ _id: { $in: list } });
+
+      // add name data
+      result.tags = list.map((elm) => {
+        const tag = tags.find((tag) => { return (tag.id === String(elm._id)) });
+        elm.name = tag.name;
+        return elm;
+      });
+
+      result.totalCount = await Tag.count();
+
       return res.json(ApiResponse.success({ result }));
     }
     catch (err) {