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

Add ability to modify document titles #342

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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")
hugosandsjo marked this conversation as resolved.
Show resolved Hide resolved
@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