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

(BE) Add API for Retrieving Workspace List #59

Merged
merged 4 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
13 changes: 2 additions & 11 deletions backend/src/workspaces/types/create-workspace-response.type.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import { ApiProperty } from "@nestjs/swagger";
import { WorkspaceDomain } from "./workspace-domain.type";

export class CreateWorkspaceResponse {
@ApiProperty({ type: String, description: "ID of new workspace" })
id: string;
@ApiProperty({ type: String, description: "Title of new workspace" })
title: string;
@ApiProperty({ type: Date, description: "Created date of new workspace" })
createdAt: Date;
@ApiProperty({ type: Date, description: "Updated date of new workspace" })
updatedAt: Date;
}
export class CreateWorkspaceResponse extends WorkspaceDomain {}
13 changes: 2 additions & 11 deletions backend/src/workspaces/types/find-workspace-response.type.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import { ApiProperty } from "@nestjs/swagger";
import { WorkspaceDomain } from "./workspace-domain.type";

export class FindWorkspaceResponse {
@ApiProperty({ type: String, description: "ID of found workspace" })
id: string;
@ApiProperty({ type: String, description: "Title of found workspace" })
title: string;
@ApiProperty({ type: Date, description: "Created date of found workspace" })
createdAt: Date;
@ApiProperty({ type: Date, description: "Updated date of found workspace" })
updatedAt: Date;
}
export class FindWorkspaceResponse extends WorkspaceDomain {}
10 changes: 10 additions & 0 deletions backend/src/workspaces/types/find-workspaces-response.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from "@nestjs/swagger";
import { WorkspaceDomain } from "./workspace-domain.type";

export class FindWorkspacesResponse {
@ApiProperty({ type: [WorkspaceDomain], description: "List of found workspaces" })
workspaces: Array<WorkspaceDomain>;

@ApiProperty({ type: String, description: "The ID of last workspace" })
cursor: string | null;
}
12 changes: 12 additions & 0 deletions backend/src/workspaces/types/workspace-domain.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from "@nestjs/swagger";

export class WorkspaceDomain {
@ApiProperty({ type: String, description: "ID of the workspace" })
id: string;
@ApiProperty({ type: String, description: "Title of the workspace" })
title: string;
@ApiProperty({ type: Date, description: "Created date of the workspace" })
createdAt: Date;
@ApiProperty({ type: Date, description: "Updated date of the workspace" })
updatedAt: Date;
}
46 changes: 44 additions & 2 deletions backend/src/workspaces/workspaces.controller.ts
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 { WorkspacesService } from "./workspaces.service";
import { CreateWorkspaceDto } from "./dto/CreateWorkspace.dto";
import {
Expand All @@ -8,12 +18,14 @@ import {
ApiFoundResponse,
ApiNotFoundResponse,
ApiOperation,
ApiQuery,
ApiTags,
} from "@nestjs/swagger";
import { AuthroizedRequest } from "src/utils/types/req.type";
import { CreateWorkspaceResponse } from "./types/create-workspace-response.type";
import { FindWorkspaceResponse } from "./types/find-workspace-response.type";
import { HttpExceptionResponse } from "src/utils/types/http-exception-response.type";
import { FindWorkspacesResponse } from "./types/find-workspaces-response.type";

@ApiTags("Workspaces")
@ApiBearerAuth()
Expand Down Expand Up @@ -45,7 +57,37 @@ export class WorkspacesController {
type: HttpExceptionResponse,
description: "The Workspace does not exist, or the user lacks the appropriate permissions.",
})
async findOne(@Req() req: AuthroizedRequest, @Param("id") workspaceId: string) {
async findOne(
@Req() req: AuthroizedRequest,
@Param("id") workspaceId: string
): Promise<FindWorkspaceResponse> {
return this.workspacesService.findOne(req.user.id, workspaceId);
}

@Get("")
@ApiOperation({
summary: "Retrieve the Workspaces",
description: "Return the user's workspaces. This API supports KeySet pagination.",
})
@ApiFoundResponse({ type: FindWorkspacesResponse })
@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,
@Query("page_size", new DefaultValuePipe(10), ParseIntPipe) pageSize: number,
@Query("cursor", new DefaultValuePipe(undefined)) cursor?: string
): Promise<FindWorkspacesResponse> {
return this.workspacesService.findMany(req.user.id, pageSize, cursor);
}
}
37 changes: 36 additions & 1 deletion backend/src/workspaces/workspaces.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { Workspace } from "@prisma/client";
import { Prisma, Workspace } from "@prisma/client";
import { PrismaService } from "src/db/prisma.service";
import { FindWorkspacesResponse } from "./types/find-workspaces-response.type";

@Injectable()
export class WorkspacesService {
Expand Down Expand Up @@ -42,4 +43,38 @@ export class WorkspacesService {
},
});
}

async findMany(
userId: string,
pageSize: number,
cursor?: string
): Promise<FindWorkspacesResponse> {
const additionalOptions: Prisma.WorkspaceFindManyArgs = {};

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

const workspaceList = await this.prismaService.workspace.findMany({
take: pageSize + 1,
where: {
userWorkspaceList: {
some: {
userId: {
equals: userId,
},
},
},
},
orderBy: {
id: "desc",
},
...additionalOptions,
});

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