From a2cc9191af16b1b4d8635decfaeb537cef86d36f Mon Sep 17 00:00:00 2001 From: rahuldahal Date: Thu, 6 Jun 2024 20:56:40 +0545 Subject: [PATCH] feat: get all files along w/ relavent project --- src/file/file.controller.ts | 14 +++++++++++++- src/file/file.service.ts | 30 ++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/file/file.controller.ts b/src/file/file.controller.ts index 932e733..c23a9af 100644 --- a/src/file/file.controller.ts +++ b/src/file/file.controller.ts @@ -1,7 +1,8 @@ import { CreateFileDto } from './dto'; +import { User } from '@prisma/client'; import { AuthGuard } from '@nestjs/passport'; import { FileService } from './file.service'; -import { GetParam } from 'src/auth/Decorator'; +import { GetParam, GetUser } from 'src/auth/Decorator'; import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common'; import { @@ -30,6 +31,17 @@ export class FileController { return this.fileService.createFile(dto); } + @ApiBearerAuth() + @ApiForbiddenResponse({ description: 'Forbidden' }) + @ApiOperation({ summary: 'Get all files' }) + @ApiResponse({ + status: 200, + description: 'Files found successfully', + }) + @Get('/') + findAllFiles(@GetUser() user: User ){ + return this.fileService.getAllFiles(user.id); + } @ApiBearerAuth() @ApiForbiddenResponse({ description: 'Forbidden' }) diff --git a/src/file/file.service.ts b/src/file/file.service.ts index 2401c1b..c0fb95d 100644 --- a/src/file/file.service.ts +++ b/src/file/file.service.ts @@ -25,17 +25,43 @@ export class FileService { } } + // Get all files + async getAllFiles(userId: number) { + try { + const files = await this.prisma.file.findMany({ + where: { + project: { + ownerId: userId, + }, + }, + include: { + project: { + select: { + id: true, + name: true, + }, + }, + }, + }); + + // Return the found files along with relevant project + return files; + } catch (error) { + console.log(error); + throw InternalServerErrorException; + } + } + // Get a file async getFile(id: number) { try { - // save file in the database const file = await this.prisma.file.findFirstOrThrow({ where: { id } }); - // Return the created file + // Return the found file return file; } catch (error) { console.log(error);