From e5c16219cd88c6ebd776162b195960102cfd82e0 Mon Sep 17 00:00:00 2001 From: devleejb Date: Thu, 18 Jan 2024 15:11:34 +0900 Subject: [PATCH] Add controller for retrieving the document list in the workspace --- .../find-workspace-documents-response.type.ts | 2 +- .../workspace-documents.controller.ts | 46 ++++++++++++++++++- .../workspace-documents.service.ts | 4 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/backend/src/workspace-documents/types/find-workspace-documents-response.type.ts b/backend/src/workspace-documents/types/find-workspace-documents-response.type.ts index 4910dbb5..9bce2eb7 100644 --- a/backend/src/workspace-documents/types/find-workspace-documents-response.type.ts +++ b/backend/src/workspace-documents/types/find-workspace-documents-response.type.ts @@ -1,7 +1,7 @@ import { ApiProperty } from "@nestjs/swagger"; import { WorkspaceDocumentDomain } from "./workspace-document-domain.type"; -export class FindWorkspaceUsersResponse { +export class FindWorkspaceDocumentsResponse { @ApiProperty({ type: [WorkspaceDocumentDomain], description: "List of found workspace documents", diff --git a/backend/src/workspace-documents/workspace-documents.controller.ts b/backend/src/workspace-documents/workspace-documents.controller.ts index a88b0995..1905953d 100644 --- a/backend/src/workspace-documents/workspace-documents.controller.ts +++ b/backend/src/workspace-documents/workspace-documents.controller.ts @@ -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, @@ -7,6 +17,7 @@ import { ApiFoundResponse, ApiNotFoundResponse, ApiOperation, + ApiQuery, ApiTags, } from "@nestjs/swagger"; import { AuthroizedRequest } from "src/utils/types/req.type"; @@ -14,6 +25,7 @@ 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() @@ -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 { + return this.workspaceDocumentsService.findMany(req.user.id, workspaceId, pageSize, cursor); + } + @Post() @ApiOperation({ summary: "Create a Document in a Workspace", diff --git a/backend/src/workspace-documents/workspace-documents.service.ts b/backend/src/workspace-documents/workspace-documents.service.ts index 01a41ff2..af11231c 100644 --- a/backend/src/workspace-documents/workspace-documents.service.ts +++ b/backend/src/workspace-documents/workspace-documents.service.ts @@ -1,7 +1,7 @@ import { Injectable, NotFoundException } from "@nestjs/common"; import { Prisma } from "@prisma/client"; import { PrismaService } from "src/db/prisma.service"; -import { FindWorkspaceUsersResponse } from "./types/find-workspace-documents-response.type"; +import { FindWorkspaceDocumentsResponse } from "./types/find-workspace-documents-response.type"; @Injectable() export class WorkspaceDocumentsService { @@ -52,7 +52,7 @@ export class WorkspaceDocumentsService { workspaceId: string, pageSize: number, cursor?: string - ): Promise { + ): Promise { try { await this.prismaService.userWorkspace.findFirstOrThrow({ where: {