Skip to content

Commit

Permalink
feat: get all projects and relavent files
Browse files Browse the repository at this point in the history
- remove GET /files
  • Loading branch information
rahuldahal committed Jun 8, 2024
1 parent a2cc919 commit dd6879f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ model Project {
owner User @relation(name: "Owner", fields: [ownerId], references: [id])
ownerId Int
collaborators User[] @relation(name: "Collaborators")
file File[] @relation(name: "Project")
files File[] @relation(name: "Project")
}

model File {
Expand Down
15 changes: 1 addition & 14 deletions src/file/file.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CreateFileDto } from './dto';
import { User } from '@prisma/client';
import { AuthGuard } from '@nestjs/passport';
import { FileService } from './file.service';
import { GetParam, GetUser } from 'src/auth/Decorator';
import { GetParam } from 'src/auth/Decorator';
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';

import {
Expand Down Expand Up @@ -31,18 +30,6 @@ 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' })
@ApiOperation({ summary: 'Find the file matching the id' })
Expand Down
27 changes: 0 additions & 27 deletions src/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,6 @@ 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 {
Expand Down
14 changes: 13 additions & 1 deletion src/project/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CreateProjectDto } from './dto';
import { GetUser } from 'src/auth/Decorator';
import { AuthGuard } from '@nestjs/passport';
import { ProjectService } from './project.service';
import { Body, Controller, Post, UseGuards } from '@nestjs/common';
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';

import {
ApiBearerAuth,
Expand Down Expand Up @@ -32,4 +32,16 @@ export class ProjectController {
createProject(@Body() dto: CreateProjectDto, @GetUser() user: User) {
return this.projectService.createProject(user, dto);
}

@ApiBearerAuth()
@ApiForbiddenResponse({ description: 'Forbidden' })
@ApiOperation({ summary: 'Get all projects' })
@ApiResponse({
status: 200,
description: 'Projects found successfully',
})
@Get('/')
findAllProjects(@GetUser() user: User ){
return this.projectService.getAllProjects(user.id);
}
}
20 changes: 20 additions & 0 deletions src/project/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,24 @@ export class ProjectService {
throw InternalServerErrorException;
}
}

async getAllProjects(userId: number) {
try {
const projectsWithFiles = await this.prisma.project.findMany({
where: {
ownerId: userId,
},
include: {
files: true,
},
});


// Return the found projects along with relevant files
return projectsWithFiles;
} catch (error) {
console.log(error);
throw InternalServerErrorException;
}
}
}

0 comments on commit dd6879f

Please sign in to comment.