Skip to content

Commit

Permalink
feat: get all files along w/ relavent project
Browse files Browse the repository at this point in the history
  • Loading branch information
rahuldahal committed Jun 6, 2024
1 parent 786eeb9 commit a2cc919
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/file/file.controller.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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' })
Expand Down
30 changes: 28 additions & 2 deletions src/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a2cc919

Please sign in to comment.