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 API for Retrieving a Document in the Workspace #62

Merged
merged 2 commits into from
Jan 18, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { WorkspaceDocumentDomain } from "./workspace-document-domain.type";

export class FindWorkspaceDocumentResponse extends WorkspaceDocumentDomain {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Body, Controller, Param, Post, Req } from "@nestjs/common";
import { Body, Controller, Get, Param, Post, Req } from "@nestjs/common";
import { WorkspaceDocumentsService } from "./workspace-documents.service";
import {
ApiBearerAuth,
ApiBody,
ApiCreatedResponse,
ApiFoundResponse,
ApiNotFoundResponse,
ApiOperation,
ApiTags,
Expand All @@ -12,13 +13,33 @@ 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";
import { FindWorkspaceDocumentResponse } from "./types/find-workspace-document-response.type";

@ApiTags("Workspace.Documents")
@ApiBearerAuth()
@Controller("workspaces/:workspace_id/documents")
export class WorkspaceDocumentsController {
constructor(private workspaceDocumentsService: WorkspaceDocumentsService) {}

@Get(":document_id")
@ApiOperation({
summary: "Retrieve a Document in the Workspace",
description: "If the user has the access permissions, return a document.",
})
@ApiFoundResponse({ type: FindWorkspaceDocumentResponse })
@ApiNotFoundResponse({
type: HttpExceptionResponse,
description:
"The workspace or document does not exist, or the user lacks the appropriate permissions.",
})
async findOne(
@Req() req: AuthroizedRequest,
@Param("workspace_id") workspaceId: string,
@Param("document_id") documentId: string
): Promise<FindWorkspaceDocumentResponse> {
return this.workspaceDocumentsService.findOne(req.user.id, workspaceId, documentId);
}

@Post()
@ApiOperation({
summary: "Create a Document in a Workspace",
Expand Down
19 changes: 19 additions & 0 deletions backend/src/workspace-documents/workspace-documents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,23 @@ export class WorkspaceDocumentsService {
},
});
}

async findOne(userId: string, workspaceId: string, documentId: string) {
try {
await this.prismaService.userWorkspace.findFirstOrThrow({
where: {
userId,
workspaceId,
},
});

return this.prismaService.document.findUniqueOrThrow({
where: {
id: documentId,
},
});
} catch (e) {
throw new NotFoundException();
}
}
}
12 changes: 6 additions & 6 deletions backend/src/workspaces/workspaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ export class WorkspacesService {
workspaceId,
},
});

return this.prismaService.workspace.findUniqueOrThrow({
where: {
id: workspaceId,
},
});
} catch (e) {
throw new NotFoundException();
}

return this.prismaService.workspace.findUnique({
where: {
id: workspaceId,
},
});
}

async findMany(
Expand Down
Loading