Skip to content

Commit

Permalink
(BE) Add API for Retrieving the Document List in the Workspace (#63)
Browse files Browse the repository at this point in the history
* Add service for retrieving document list in the workspace

* Add controller for retrieving the document list in the workspace
  • Loading branch information
devleejb authored Jan 18, 2024
1 parent 03caebd commit 57f4acc
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApiProperty } from "@nestjs/swagger";
import { WorkspaceDocumentDomain } from "./workspace-document-domain.type";

export class FindWorkspaceDocumentsResponse {
@ApiProperty({
type: [WorkspaceDocumentDomain],
description: "List of found workspace documents",
})
documents: Array<WorkspaceDocumentDomain>;

@ApiProperty({ type: String, description: "The ID of last document" })
cursor: string | null;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Body, Controller, Get, Param, Post, Req } from "@nestjs/common";
import {
Body,
Controller,
DefaultValuePipe,
Get,
Param,
ParseIntPipe,
Post,
Query,
Req,
} from "@nestjs/common";
import { WorkspaceDocumentsService } from "./workspace-documents.service";
import {
ApiBearerAuth,
Expand All @@ -7,13 +17,15 @@ import {
ApiFoundResponse,
ApiNotFoundResponse,
ApiOperation,
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 { HttpExceptionResponse } from "src/utils/types/http-exception-response.type";
import { FindWorkspaceDocumentResponse } from "./types/find-workspace-document-response.type";
import { FindWorkspaceDocumentsResponse } from "./types/find-workspace-documents-response.type";

@ApiTags("Workspace.Documents")
@ApiBearerAuth()
Expand All @@ -40,6 +52,38 @@ export class WorkspaceDocumentsController {
return this.workspaceDocumentsService.findOne(req.user.id, workspaceId, documentId);
}

@Get("")
@ApiOperation({
summary: "Retrieve the Documents in Workspace",
description: "Return the documents in the workspace. This API supports KeySet pagination.",
})
@ApiFoundResponse({ type: FindWorkspaceDocumentsResponse })
@ApiNotFoundResponse({
type: HttpExceptionResponse,
description: "The workspace does not exist, or the user lacks the appropriate permissions.",
})
@ApiQuery({
name: "page_size",
type: Number,
description: "Page size to fetch (Default to 10)",
required: false,
})
@ApiQuery({
name: "cursor",
type: String,
description:
"API returns a limited set of results after a given cursor. If no value is provided, it returns the first page.",
required: false,
})
async findMany(
@Req() req: AuthroizedRequest,
@Param("workspace_id") workspaceId: string,
@Query("page_size", new DefaultValuePipe(10), ParseIntPipe) pageSize: number,
@Query("cursor", new DefaultValuePipe(undefined)) cursor?: string
): Promise<FindWorkspaceDocumentsResponse> {
return this.workspaceDocumentsService.findMany(req.user.id, workspaceId, pageSize, cursor);
}

@Post()
@ApiOperation({
summary: "Create a Document in a Workspace",
Expand Down
42 changes: 42 additions & 0 deletions backend/src/workspace-documents/workspace-documents.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { PrismaService } from "src/db/prisma.service";
import { FindWorkspaceDocumentsResponse } from "./types/find-workspace-documents-response.type";

@Injectable()
export class WorkspaceDocumentsService {
Expand Down Expand Up @@ -44,4 +46,44 @@ export class WorkspaceDocumentsService {
throw new NotFoundException();
}
}

async findMany(
userId: string,
workspaceId: string,
pageSize: number,
cursor?: string
): Promise<FindWorkspaceDocumentsResponse> {
try {
await this.prismaService.userWorkspace.findFirstOrThrow({
where: {
userId,
workspaceId,
},
});
} catch (e) {
throw new NotFoundException();
}

const additionalOptions: Prisma.DocumentFindManyArgs = {};

if (cursor) {
additionalOptions.cursor = { id: cursor };
}

const documentList = await this.prismaService.document.findMany({
take: pageSize + 1,
where: {
workspaceId,
},
orderBy: {
id: "desc",
},
...additionalOptions,
});

return {
documents: documentList.slice(0, pageSize),
cursor: documentList.length > pageSize ? documentList[pageSize].id : null,
};
}
}

0 comments on commit 57f4acc

Please sign in to comment.