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

add api method for get pageTags on routes/tag.js

yusuketk 7 лет назад
Родитель
Сommit
1d0962b5bc
1 измененных файлов с 15 добавлено и 1 удалено
  1. 15 1
      src/server/routes/tag.js

+ 15 - 1
src/server/routes/tag.js

@@ -2,6 +2,7 @@ module.exports = function(crowi, app) {
   'use strict';
 
   const Tag = crowi.model('Tag');
+  const PageTagRelation = crowi.model('PageTagRelation');
   const ApiResponse = require('../util/apiResponse');
   const actions = {};
   const api = {};
@@ -11,7 +12,7 @@ module.exports = function(crowi, app) {
 
   /**
    * @api {get} /tags.search search tags
-   * @apiName searchTag
+   * @apiName SearchTag
    * @apiGroup Tag
    *
    * @apiParam {String} q keyword
@@ -22,5 +23,18 @@ module.exports = function(crowi, app) {
     return res.json(ApiResponse.success({tags}));
   };
 
+  /**
+   * @api {get} /tags.get get page tags
+   * @apiName GetTag
+   * @apiGroup Tag
+   *
+   * @apiParam {String} pageId
+   */
+  api.get = async function(req, res) {
+    let tags = await PageTagRelation.find({relatedPage: req.query.pageId}).populate('relatedTag').select('-_id relatedTag');
+    tags = tags.map(tag => tag.relatedTag.name);
+    return res.json(ApiResponse.success({tags}));
+  };
+
   return actions;
 };