Skip to content

Commit

Permalink
feat(domain): add domain path
Browse files Browse the repository at this point in the history
  • Loading branch information
tericcabrel committed Oct 23, 2022
1 parent 8b58dd6 commit 0a61e9e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
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

0 comments on commit 0a61e9e

Please sign in to comment.