Skip to content

Commit

Permalink
(FE) Add retrieve a Document (#83)
Browse files Browse the repository at this point in the history
* Add retrieve a Document

* Add theme button to workspace

* Remove unused code

* Update yorkie
  • Loading branch information
devleejb authored Jan 22, 2024
1 parent 2cf0078 commit 5a30d6e
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 277 deletions.
23 changes: 22 additions & 1 deletion backend/src/documents/documents.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Controller, Get, Query } from "@nestjs/common";
import { Controller, Get, Param, Query, Req } from "@nestjs/common";
import { DocumentsService } from "./documents.service";
import { Public } from "src/utils/decorators/auth.decorator";
import {
ApiFoundResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
Expand All @@ -11,6 +12,8 @@ import {
} from "@nestjs/swagger";
import { FindDocumentFromSharingTokenResponse } from "./types/find-document-from-sharing-token-response.type";
import { HttpExceptionResponse } from "src/utils/types/http-exception-response.type";
import { FindDocumentResponse } from "./types/find-document-response.type";
import { AuthroizedRequest } from "src/utils/types/req.type";

@ApiTags("Documents")
@Controller("documents")
Expand Down Expand Up @@ -38,4 +41,22 @@ export class DocumentsController {
): Promise<FindDocumentFromSharingTokenResponse> {
return this.documentsService.findDocumentFromSharingToken(token);
}

@Get(":document_slug")
@ApiOperation({
summary: "Retrieve a Document in the Workspace",
description: "If the user has the access permissions, return a document.",
})
@ApiFoundResponse({ type: FindDocumentResponse })
@ApiNotFoundResponse({
type: HttpExceptionResponse,
description:
"The workspace or document does not exist, or the user lacks the appropriate permissions.",
})
async findOne(
@Req() req: AuthroizedRequest,
@Param("document_slug") documentSlug: string
): Promise<FindDocumentResponse> {
return this.documentsService.findOneBySlug(req.user.id, documentSlug);
}
}
21 changes: 21 additions & 0 deletions backend/src/documents/documents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,25 @@ export class DocumentsService {
role,
};
}

async findOneBySlug(userId: string, documentSlug: string) {
try {
const document = await this.prismaService.document.findFirstOrThrow({
where: {
slug: documentSlug,
},
});

await this.prismaService.userWorkspace.findFirstOrThrow({
where: {
userId,
workspaceId: document.workspaceId,
},
});

return document;
} catch (e) {
throw new NotFoundException();
}
}
}
3 changes: 3 additions & 0 deletions backend/src/documents/types/find-document-response.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { WorkspaceDocumentDomain } from "../../workspace-documents/types/workspace-document-domain.type";

export class FindDocumentResponse extends WorkspaceDocumentDomain {}

This file was deleted.

20 changes: 0 additions & 20 deletions backend/src/workspace-documents/workspace-documents.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { AuthroizedRequest } from "src/utils/types/req.type";
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";
import { CreateWorkspaceDocumentShareTokenResponse } from "./types/create-workspace-document-share-token-response.type";
import { CreateWorkspaceDocumentShareTokenDto } from "./dto/create-workspace-document-share-token.dto";
Expand All @@ -36,25 +35,6 @@ import { CreateWorkspaceDocumentShareTokenDto } from "./dto/create-workspace-doc
export class WorkspaceDocumentsController {
constructor(private workspaceDocumentsService: WorkspaceDocumentsService) {}

@Get(":document_slug")
@ApiOperation({
summary: "Retrieve a Document in the Workspace",
description: "If the user has the access permissions, return a document.",
})
@ApiFoundResponse({ type: FindWorkspaceDocumentResponse })
@ApiNotFoundResponse({
type: HttpExceptionResponse,
description:
"The workspace or document does not exist, or the user lacks the appropriate permissions.",
})
async findOne(
@Req() req: AuthroizedRequest,
@Param("workspace_id") workspaceId: string,
@Param("document_slug") documentSlug: string
): Promise<FindWorkspaceDocumentResponse> {
return this.workspaceDocumentsService.findOneBySlug(req.user.id, workspaceId, documentSlug);
}

@Get("")
@ApiOperation({
summary: "Retrieve the Documents in Workspace",
Expand Down
19 changes: 0 additions & 19 deletions backend/src/workspace-documents/workspace-documents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,6 @@ export class WorkspaceDocumentsService {
});
}

async findOneBySlug(userId: string, workspaceId: string, documentSlug: string) {
try {
await this.prismaService.userWorkspace.findFirstOrThrow({
where: {
userId,
workspaceId,
},
});

return this.prismaService.document.findFirstOrThrow({
where: {
slug: documentSlug,
},
});
} catch (e) {
throw new NotFoundException();
}
}

async findMany(
userId: string,
workspaceId: string,
Expand Down
Loading

0 comments on commit 5a30d6e

Please sign in to comment.