From 505ad2c2af7bb4fdd1eae3e1a726ca27bded286f Mon Sep 17 00:00:00 2001 From: LeeJongBeom <52884648+devleejb@users.noreply.github.com> Date: Thu, 18 Jan 2024 11:26:16 +0900 Subject: [PATCH] (BE) Add API for Retrieving a Workspace (#58) * Add service for retrieving a workspace * Add controller for retrieving a workspace --- .../types/http-exception-response.type.ts | 9 ++++++ .../types/find-workspace-response.type.ts | 12 ++++++++ .../src/workspaces/workspaces.controller.ts | 28 +++++++++++++++++-- backend/src/workspaces/workspaces.service.ts | 21 +++++++++++++- 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 backend/src/utils/types/http-exception-response.type.ts create mode 100644 backend/src/workspaces/types/find-workspace-response.type.ts 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, + }, + }); + } }