Selaa lähdekoodia

Extended for both path and id

Taichi Masuyama 4 vuotta sitten
vanhempi
sitoutus
902cf65527

+ 3 - 2
packages/app/src/server/models/page.ts

@@ -273,16 +273,17 @@ schema.statics.findAncestorsByPathOrId = async function(pathOrId: string): Promi
 };
 
 /*
- * Find all children by parent's id
+ * Find all children by parent's path or id. Using id should be prioritized
  */
 schema.statics.findChildrenByParentPathOrIdAndViewer = async function(parentPathOrId: string, user, userGroups = null): Promise<PageDocument[]> {
   let queryBuilder: PageQueryBuilder;
   if (hasSlash(parentPathOrId)) {
     const path = parentPathOrId;
     const regexp = generateChildrenRegExp(path);
-    queryBuilder = new PageQueryBuilder(this.find({ parent: { $regex: regexp } }));
+    queryBuilder = new PageQueryBuilder(this.find({ path: { $regex: regexp.source } }));
   }
   else {
+    const parentId = parentPathOrId;
     queryBuilder = new PageQueryBuilder(this.find({ parent: parentId }));
   }
   await addViewerCondition(queryBuilder, user, userGroups);

+ 8 - 2
packages/app/src/server/routes/apiv3/page-listing.ts

@@ -73,6 +73,9 @@ export default (crowi: Crowi): Router => {
     return res.apiv3({ target, siblings });
   });
 
+  /*
+   * In most cases, using path should be prioritized
+   */
   // eslint-disable-next-line max-len
   router.get('/ancestors', accessTokenParser, loginRequiredStrictly, validator.pageIdOrPathRequired, apiV3FormValidator, async(req: AuthorizedRequest, res: ApiV3Response): Promise<any> => {
     const { id, path } = req.query;
@@ -95,14 +98,17 @@ export default (crowi: Crowi): Router => {
     return res.apiv3({ ancestors });
   });
 
+  /*
+   * In most cases, using id should be prioritized
+   */
   // eslint-disable-next-line max-len
-  router.get('/child-pages', accessTokenParser, loginRequiredStrictly, validator.pageIdOrPathRequired, async(req: AuthorizedRequest, res: ApiV3Response) => {
+  router.get('/children', accessTokenParser, loginRequiredStrictly, validator.pageIdOrPathRequired, async(req: AuthorizedRequest, res: ApiV3Response) => {
     const { id, path } = req.query;
 
     const Page: PageModel = crowi.model('Page');
 
     try {
-      const pages = await Page.findChildrenByParentIdAndViewer((id || path)as string, req.user);
+      const pages = await Page.findChildrenByParentPathOrIdAndViewer((id || path)as string, req.user);
       return res.apiv3({ pages });
     }
     catch (err) {