Taichi Masuyama před 4 roky
rodič
revize
91c35b8352

+ 11 - 0
packages/app/src/server/models/page.ts

@@ -38,6 +38,7 @@ export interface PageModel extends Model<PageDocument> {
   findByPathAndViewer(path: string | null, user, userGroups?, useFindOne?): Promise<PageDocument[]>
   findSiblingsByPathAndViewer(path: string | null, user, userGroups?): Promise<PageDocument[]>
   findAncestorsByPath(path: string): Promise<PageDocument[]>
+  findChildrenByParentIdAndViewer(parentId: string, user, userGroups?): Promise<PageDocument[]>
 }
 
 const ObjectId = mongoose.Schema.Types.ObjectId;
@@ -247,6 +248,16 @@ schema.statics.findAncestorsByPath = async function(path: string): Promise<PageD
   return ancestors;
 };
 
+/*
+ * Find all children by parent's id
+ */
+schema.statics.findChildrenByParentIdAndViewer = async function(parentId: string, user, userGroups = null): Promise<PageDocument[]> {
+  const queryBuilder = new PageQueryBuilder(this.find({ parent: parentId }));
+  await addViewerCondition(queryBuilder, user, userGroups);
+
+  return queryBuilder.query.lean().exec();
+};
+
 
 /*
  * Merge obsolete page model methods and define new methods which depend on crowi instance

+ 15 - 0
packages/app/src/server/routes/apiv3/page-tree.ts

@@ -72,5 +72,20 @@ export default (crowi: Crowi): Router => {
     return res.apiv3({ target, ancestors, pages: siblings });
   });
 
+  router.get('/child-pages', accessTokenParser, loginRequiredStrictly, ...validator.getPagesAroundTarget, async(req: AuthorizedRequest, res: ApiV3Response) => {
+    const { id } = req.query;
+
+    const Page: PageModel = crowi.model('Page');
+
+    try {
+      const pages = await Page.findChildrenByParentIdAndViewer(id as string, req.user);
+      return res.apiv3({ pages });
+    }
+    catch (err) {
+      logger.error('Error occurred while finding children.', err);
+      return res.apiv3Err(new ErrorV3('Error occurred while finding children.'));
+    }
+  });
+
   return router;
 };