diff --git a/backend/src/utils/types/http-exception-response.type.ts b/backend/src/utils/types/http-exception-response.type.ts new file mode 100644 index 00000000..bd7988e0 --- /dev/null +++ b/backend/src/utils/types/http-exception-response.type.ts @@ -0,0 +1,9 @@ +import { HttpStatus } from "@nestjs/common"; +import { ApiProperty } from "@nestjs/swagger"; + +export class HttpExceptionResponse { + @ApiProperty({ enum: HttpStatus, description: "Status Code of HTTP Response" }) + statusCode: HttpStatus; + @ApiProperty({ type: String, description: "Description about the error" }) + message: string; +} diff --git a/backend/src/workspaces/types/find-workspace-response.type.ts b/backend/src/workspaces/types/find-workspace-response.type.ts new file mode 100644 index 00000000..8a073d24 --- /dev/null +++ b/backend/src/workspaces/types/find-workspace-response.type.ts @@ -0,0 +1,12 @@ +import { ApiProperty } from "@nestjs/swagger"; + +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; +} diff --git a/backend/src/workspaces/workspaces.controller.ts b/backend/src/workspaces/workspaces.controller.ts index 8cc43d94..1c27aa61 100644 --- a/backend/src/workspaces/workspaces.controller.ts +++ b/backend/src/workspaces/workspaces.controller.ts @@ -1,9 +1,19 @@ -import { Body, Controller, Post, Req } from "@nestjs/common"; +import { Body, Controller, Get, Param, Post, Req } from "@nestjs/common"; import { WorkspacesService } from "./workspaces.service"; import { CreateWorkspaceDto } from "./dto/CreateWorkspace.dto"; -import { ApiBearerAuth, ApiBody, ApiCreatedResponse, ApiOperation, ApiTags } from "@nestjs/swagger"; +import { + ApiBearerAuth, + ApiBody, + ApiCreatedResponse, + ApiFoundResponse, + ApiNotFoundResponse, + ApiOperation, + 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"; @ApiTags("Workspaces") @ApiBearerAuth() @@ -24,4 +34,18 @@ export class WorkspacesController { ): Promise { return this.workspacesService.create(req.user.id, createWorkspaceDto.title); } + + @Get(":id") + @ApiOperation({ + summary: "Retrieve a Workspace", + description: "If the user has the access permissions, return a workspace.", + }) + @ApiFoundResponse({ type: FindWorkspaceResponse }) + @ApiNotFoundResponse({ + type: HttpExceptionResponse, + description: "The Workspace does not exist, or the user lacks the appropriate permissions.", + }) + async findOne(@Req() req: AuthroizedRequest, @Param("id") workspaceId: string) { + return this.workspacesService.findOne(req.user.id, workspaceId); + } } diff --git a/backend/src/workspaces/workspaces.service.ts b/backend/src/workspaces/workspaces.service.ts index 913f2851..ed5a285a 100644 --- a/backend/src/workspaces/workspaces.service.ts +++ b/backend/src/workspaces/workspaces.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from "@nestjs/common"; +import { Injectable, NotFoundException } from "@nestjs/common"; import { Workspace } from "@prisma/client"; import { PrismaService } from "src/db/prisma.service"; @@ -23,4 +23,23 @@ export class WorkspacesService { return workspace; } + + async findOne(userId: string, workspaceId: string) { + try { + await this.prismaService.userWorkspace.findFirstOrThrow({ + where: { + userId, + workspaceId, + }, + }); + } catch (e) { + throw new NotFoundException(); + } + + return this.prismaService.workspace.findUnique({ + where: { + id: workspaceId, + }, + }); + } }