-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(BE) Add API for Creating a Document in Workspace (#61)
* Add base code for workspace documents * Fix lint & Add service for workspace document * Add controller for creating a workspace document
- Loading branch information
Showing
11 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
backend/src/workspace-documents/dto/create-workspace-document.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
|
||
export class CreateWorkspaceDocumentDto { | ||
@ApiProperty({ description: "Title of document to create", type: String }) | ||
title: string; | ||
} |
3 changes: 3 additions & 0 deletions
3
backend/src/workspace-documents/types/create-workspace-document-response.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { WorkspaceDocumentDomain } from "./workspace-document-domain.type"; | ||
|
||
export class CreateWorkspaceDocumentResponse extends WorkspaceDocumentDomain {} |
18 changes: 18 additions & 0 deletions
18
backend/src/workspace-documents/types/workspace-document-domain.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
|
||
export class WorkspaceDocumentDomain { | ||
@ApiProperty({ type: String, description: "ID of the document" }) | ||
id: string; | ||
@ApiProperty({ type: String, description: "Yorkie Document ID of the document" }) | ||
yorkieDocumentId: string; | ||
@ApiProperty({ type: String, description: "Title of the document" }) | ||
title: string; | ||
@ApiProperty({ type: String, description: "Content of the document", required: false }) | ||
content?: string; | ||
@ApiProperty({ type: Date, description: "Created date of the document" }) | ||
createdAt: Date; | ||
@ApiProperty({ type: Date, description: "Updated date of the document" }) | ||
updatedAt: Date; | ||
@ApiProperty({ type: String, description: "ID of the workspace that includes the document" }) | ||
workspaceId: string; | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/workspace-documents/workspace-documents.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { WorkspaceDocumentsController } from "./workspace-documents.controller"; | ||
|
||
describe("WorkspaceDocumentsController", () => { | ||
let controller: WorkspaceDocumentsController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [WorkspaceDocumentsController], | ||
}).compile(); | ||
|
||
controller = module.get<WorkspaceDocumentsController>(WorkspaceDocumentsController); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
backend/src/workspace-documents/workspace-documents.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Body, Controller, Param, Post, Req } from "@nestjs/common"; | ||
import { WorkspaceDocumentsService } from "./workspace-documents.service"; | ||
import { | ||
ApiBearerAuth, | ||
ApiBody, | ||
ApiCreatedResponse, | ||
ApiNotFoundResponse, | ||
ApiOperation, | ||
ApiTags, | ||
} from "@nestjs/swagger"; | ||
import { AuthroizedRequest } from "src/utils/types/req.type"; | ||
import { CreateWorkspaceDocumentDto } from "./dto/create-workspace-document.dto"; | ||
import { CreateWorkspaceDocumentResponse } from "./types/create-workspace-document-response.type"; | ||
import { HttpExceptionResponse } from "src/utils/types/http-exception-response.type"; | ||
|
||
@ApiTags("Workspace.Documents") | ||
@ApiBearerAuth() | ||
@Controller("workspaces/:workspace_id/documents") | ||
export class WorkspaceDocumentsController { | ||
constructor(private workspaceDocumentsService: WorkspaceDocumentsService) {} | ||
|
||
@Post() | ||
@ApiOperation({ | ||
summary: "Create a Document in a Workspace", | ||
description: "Create a document with the title in a workspace", | ||
}) | ||
@ApiBody({ type: CreateWorkspaceDocumentDto }) | ||
@ApiCreatedResponse({ type: CreateWorkspaceDocumentResponse }) | ||
@ApiNotFoundResponse({ | ||
type: HttpExceptionResponse, | ||
description: "The workspace does not exist, or the user lacks the appropriate permissions.", | ||
}) | ||
async create( | ||
@Req() req: AuthroizedRequest, | ||
@Param("workspace_id") workspaceId: string, | ||
@Body() createWorkspaceDocumentDto: CreateWorkspaceDocumentDto | ||
): Promise<CreateWorkspaceDocumentResponse> { | ||
return this.workspaceDocumentsService.create( | ||
req.user.id, | ||
workspaceId, | ||
createWorkspaceDocumentDto.title | ||
); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/workspace-documents/workspace-documents.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { WorkspaceDocumentsService } from "./workspace-documents.service"; | ||
import { WorkspaceDocumentsController } from "./workspace-documents.controller"; | ||
import { PrismaService } from "src/db/prisma.service"; | ||
|
||
@Module({ | ||
providers: [WorkspaceDocumentsService, PrismaService], | ||
controllers: [WorkspaceDocumentsController], | ||
}) | ||
export class WorkspaceDocumentsModule {} |
18 changes: 18 additions & 0 deletions
18
backend/src/workspace-documents/workspace-documents.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { WorkspaceDocumentsService } from "./workspace-documents.service"; | ||
|
||
describe("WorkspaceDocumentsService", () => { | ||
let service: WorkspaceDocumentsService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [WorkspaceDocumentsService], | ||
}).compile(); | ||
|
||
service = module.get<WorkspaceDocumentsService>(WorkspaceDocumentsService); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
backend/src/workspace-documents/workspace-documents.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Injectable, NotFoundException } from "@nestjs/common"; | ||
import { PrismaService } from "src/db/prisma.service"; | ||
|
||
@Injectable() | ||
export class WorkspaceDocumentsService { | ||
constructor(private prismaService: PrismaService) {} | ||
|
||
async create(userId: string, workspaceId: string, title: string) { | ||
try { | ||
await this.prismaService.userWorkspace.findFirstOrThrow({ | ||
where: { | ||
userId, | ||
workspaceId, | ||
}, | ||
}); | ||
} catch (e) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return this.prismaService.document.create({ | ||
data: { | ||
title, | ||
workspaceId, | ||
yorkieDocumentId: Math.random().toString(36).substring(7), | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters