Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(folder): improve folder structure management #54

Merged
merged 3 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(domain): add domain path
  • Loading branch information
tericcabrel committed Oct 20, 2022
commit 1334a0415b12beac3141c8cf753e65323dda041f
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE `folders` ADD COLUMN `path` TEXT NULL;
1 change: 1 addition & 0 deletions packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ model Folder {
userId String @map("user_id") @db.VarChar(50)
parentId String? @map("parent_id")
name String @db.VarChar(255)
path String? @db.Text
isFavorite Boolean @default(false) @map("is_favorite")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
Expand Down
11 changes: 11 additions & 0 deletions packages/domain/src/folders/folder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ export default class FolderService {

const input = createFolderDto.toFolder();

const parentFolder = await this.findById(createFolderDto.parentFolderId);

return dbClient.folder.create({
data: {
id: input.id,
name: input.name,
parentId: input.parentId,
path: this.buildFolderPath(parentFolder),
userId: input.userId,
},
});
Expand Down Expand Up @@ -152,6 +155,14 @@ export default class FolderService {
});
}

private buildFolderPath(parentFolder: Folder): string {
if (!parentFolder.path) {
return parentFolder.id;
}

return [parentFolder.path, parentFolder.id].join('/');
}

private async listParentFolderRecursively(folderId: string, result: Folder[] = []): Promise<Folder[]> {
const folder = await dbClient.folder.findFirstOrThrow({ where: { id: folderId } });

Expand Down