Skip to content

Commit

Permalink
Add ability to modify document titles (#342)
Browse files Browse the repository at this point in the history
* updating the document title works

* now fetches current document and displays its name as well

* feedback on submit updated

* removed codes

* fixed feedback

* fixed credentials

* update on feedback
  • Loading branch information
hugosandsjo authored and minai621 committed Nov 5, 2024
1 parent ac7b3f7 commit 0fc4ded
Show file tree
Hide file tree
Showing 7 changed files with 495 additions and 286 deletions.
13 changes: 13 additions & 0 deletions backend/src/workspace-documents/dto/update-document-title.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsString, IsNotEmpty } from "class-validator";

export class UpdateDocumentTitleDto {
@ApiProperty({
description: "The new title of the document",
example: "Updated Document Title",
type: String,
})
@IsString()
@IsNotEmpty()
title: string;
}
44 changes: 41 additions & 3 deletions backend/src/workspace-documents/workspace-documents.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Param,
ParseIntPipe,
Post,
Put,
Query,
Req,
} from "@nestjs/common";
Expand All @@ -18,12 +19,14 @@ import {
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
ApiParam,
ApiQuery,
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 { UpdateDocumentTitleDto } from "./dto/update-document-title.dto";
import { HttpExceptionResponse } from "src/utils/types/http-exception-response.type";
import { FindWorkspaceDocumentsResponse } from "./types/find-workspace-documents-response.type";
import { CreateWorkspaceDocumentShareTokenResponse } from "./types/create-workspace-document-share-token-response.type";
Expand All @@ -36,6 +39,44 @@ import { FindWorkspaceDocumentResponse } from "./types/find-workspace-document-r
export class WorkspaceDocumentsController {
constructor(private workspaceDocumentsService: WorkspaceDocumentsService) {}

@Put(":document_id")
@ApiOperation({
summary: "Update the title of a document in the workspace",
description: "If the user has the access permissions, update the document's title.",
})
@ApiParam({
name: "workspace_id",
description: "ID of workspace",
})
@ApiParam({
name: "document_id",
description: "ID of document to change title",
})
@ApiOkResponse({
description: "Document title updated successfully",
})
@ApiNotFoundResponse({
type: HttpExceptionResponse,
description:
"The workspace or document does not exist, or the user lacks the appropriate permissions.",
})
@ApiBody({
description: "The new title of the document",
type: UpdateDocumentTitleDto,
})
async updateTitle(
@Param("workspace_id") workspaceId: string,
@Param("document_id") documentId: string,
@Body() updateDocumentTitleDto: UpdateDocumentTitleDto,
@Req() req: AuthroizedRequest
): Promise<void> {
await this.workspaceDocumentsService.updateTitle(
req.user.id,
workspaceId,
documentId,
updateDocumentTitleDto.title
);
}
@Get("")
@ApiOperation({
summary: "Retrieve the Documents in Workspace",
Expand Down Expand Up @@ -67,7 +108,6 @@ export class WorkspaceDocumentsController {
): Promise<FindWorkspaceDocumentsResponse> {
return this.workspaceDocumentsService.findMany(req.user.id, workspaceId, pageSize, cursor);
}

@Get(":document_id")
@ApiOperation({
summary: "Retrieve a Document in the Workspace",
Expand All @@ -86,7 +126,6 @@ export class WorkspaceDocumentsController {
): Promise<FindWorkspaceDocumentResponse> {
return this.workspaceDocumentsService.findOne(req.user.id, workspaceId, documentId);
}

@Post()
@ApiOperation({
summary: "Create a Document in a Workspace",
Expand All @@ -109,7 +148,6 @@ export class WorkspaceDocumentsController {
createWorkspaceDocumentDto.title
);
}

@Post(":document_id/share-token")
@ApiOperation({
summary: "Retrieve a Share Token for the Document",
Expand Down
25 changes: 25 additions & 0 deletions backend/src/workspace-documents/workspace-documents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ export class WorkspaceDocumentsService {
private configService: ConfigService
) {}

async updateTitle(
userId: string,
workspaceId: string,
documentId: string,
title: string
): Promise<void> {
try {
await this.prismaService.userWorkspace.findFirstOrThrow({
where: {
userId,
workspaceId,
},
});
} catch (e) {
throw new NotFoundException(
"The workspace does not exist, or the user lacks the appropriate permissions."
);
}

await this.prismaService.document.update({
where: { id: documentId },
data: { title: title },
});
}

async create(userId: string, workspaceId: string, title: string) {
try {
await this.prismaService.userWorkspace.findFirstOrThrow({
Expand Down
Loading

0 comments on commit 0fc4ded

Please sign in to comment.