Skip to content

Commit

Permalink
(BE) Add API for Retrieving a Workspace (#58)
Browse files Browse the repository at this point in the history
* Add service for retrieving a workspace

* Add controller for retrieving a workspace
  • Loading branch information
devleejb authored Jan 18, 2024
1 parent 1c80bbf commit 505ad2c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
9 changes: 9 additions & 0 deletions backend/src/utils/types/http-exception-response.type.ts
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions backend/src/workspaces/types/find-workspace-response.type.ts
Original file line number Diff line number Diff line change
@@ -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;
}
28 changes: 26 additions & 2 deletions backend/src/workspaces/workspaces.controller.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -24,4 +34,18 @@ export class WorkspacesController {
): Promise<CreateWorkspaceResponse> {
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);
}
}
21 changes: 20 additions & 1 deletion backend/src/workspaces/workspaces.service.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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,
},
});
}
}

0 comments on commit 505ad2c

Please sign in to comment.