Browse Source

Merge branch 'feat/gw7833-create-bookmarkfolder-model-and-api' into feat/gw7895-create-new-folder-from-sidebar-bookamarks

Mudana-Grune 3 years ago
parent
commit
90024ca478

+ 19 - 4
packages/app/src/server/models/bookmark-folder.ts

@@ -1,4 +1,5 @@
 import { Ref, IUser } from '@growi/core';
+import ExtensibleCustomError from 'extensible-custom-error';
 import {
   Types, Document, Model, Schema,
 } from 'mongoose';
@@ -9,10 +10,13 @@ import { getOrCreateModel } from '../util/mongoose-utils';
 
 const logger = loggerFactory('growi:models:bookmark-folder');
 
+export class InvalidParentBookmarkFolder extends ExtensibleCustomError {}
+
+
 export type IBookmarkFolderDocument = {
   name: string
   owner: Ref<IUser>
-  parent?: string
+  parent?: Ref<BookmarkFolderDocument>
 }
 export interface BookmarkFolderDocument extends Document {
   _id: Types.ObjectId
@@ -32,15 +36,26 @@ export interface BookmarkFolderModel extends Model<BookmarkFolderDocument>{
 const bookmarkFolderSchema = new Schema<BookmarkFolderDocument, BookmarkFolderModel>({
   name: { type: String },
   owner: { type: Schema.Types.ObjectId, ref: 'User', index: true },
-  parent: { type: Schema.Types.ObjectId, refPath: 'BookmarkFolder', required: false },
+  parent: { type: Schema.Types.ObjectId, ref: 'BookmarkFolder', required: false },
 });
 
 
 bookmarkFolderSchema.statics.createByParameters = async function(params: IBookmarkFolderDocument): Promise<BookmarkFolderDocument> {
   const { name, owner, parent } = params;
-  const parentFolder = await this.findById(parent);
+  let bookmarkFolder;
+
+  if (parent === null) {
+    bookmarkFolder = await this.create({ name, owner, parent:  null }) as unknown as BookmarkFolderDocument;
+  }
+  else {
+    const parentFolder = await this.findById(parent);
+    if (!parentFolder) {
+      throw new InvalidParentBookmarkFolder("Parent folder doesn't exists");
+    }
+    bookmarkFolder = await this.create({ name, owner, parent:  parentFolder?._id }) as unknown as BookmarkFolderDocument;
+  }
+
 
-  const bookmarkFolder = await this.create({ name, owner, parent: parentFolder?._id || null }) as unknown as BookmarkFolderDocument;
   return bookmarkFolder;
 };
 

+ 7 - 4
packages/app/src/server/routes/apiv3/bookmark-folder.ts

@@ -1,9 +1,10 @@
-import { body, param } from 'express-validator';
+import { ErrorV3 } from '@growi/core';
+import { body } from 'express-validator';
 
 import { apiV3FormValidator } from '~/server/middlewares/apiv3-form-validator';
 import loggerFactory from '~/utils/logger';
 
-import BookmarkFolder from '../../models/bookmark-folder';
+import BookmarkFolder, { InvalidParentBookmarkFolder } from '../../models/bookmark-folder';
 
 const logger = loggerFactory('growi:routes:apiv3:bookmark-folder');
 
@@ -31,12 +32,14 @@ module.exports = (crowi) => {
     };
 
     try {
-      const bookmarkFolder = BookmarkFolder.createByParameters(params);
+      const bookmarkFolder = await BookmarkFolder.createByParameters(params);
       logger.debug('bookmark folder created', bookmarkFolder);
       return res.apiv3({ bookmarkFolder });
     }
     catch (err) {
-      logger.error('create bookmark folder failed', err);
+      if (err instanceof InvalidParentBookmarkFolder) {
+        return res.apiv3Err(new ErrorV3(err.message, 'failed_to_create_bookmark_folder'));
+      }
       return res.apiv3Err(err, 500);
     }
   });

+ 3 - 2
packages/app/src/stores/bookmark.ts

@@ -1,7 +1,7 @@
+import { IUserHasId, Nullable } from '@growi/core';
 import { SWRResponse } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 
-import { Nullable } from '~/interfaces/common';
 import { IPageHasId } from '~/interfaces/page';
 import { IBookmarkFolderDocument } from '~/server/models/bookmark-folder';
 
@@ -26,8 +26,9 @@ export const useSWRBookmarkInfo = (pageId: string | null | undefined): SWRRespon
 export const useSWRxCurrentUserBookmarks = (pageNum?: Nullable<number>): SWRResponse<IPageHasId[], Error> => {
   const { data: currentUser } = useCurrentUser();
   const currentPage = pageNum ?? 1;
+  const user = currentUser as IUserHasId;
   return useSWRImmutable(
-    currentUser != null ? `/bookmarks/${currentUser._id}` : null,
+    currentUser != null ? `/bookmarks/${user._id}` : null,
     endpoint => apiv3Get(endpoint, { page: currentPage }).then((response) => {
       const { paginationResult } = response.data;
       return paginationResult.docs.map((item) => {