Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/material #73

Merged
merged 26 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/server/workspace/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { WorkspaceModule } from './workspace/workspace.module';
import { OrganisationModule } from './organisation/organisation.module';
import { MaterialModule } from './material/material.module';
import { ScheduleModule } from './schedule/schedule.module';

@Module({
imports: [
AuthModule,
UserModule,
WorkspaceModule,
MaterialModule,
ScheduleModule,
OrganisationModule,
ConfigModule.forRoot({ envFilePath: ['.env'] }),
MongooseModule.forRoot('mongodb://localhost/cc'),
Expand Down
4 changes: 3 additions & 1 deletion src/server/workspace/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ export class AuthService {
const user = {
sub: findUser._id,
role: findUser.role,
workspaces: findUser.workspaces,
organisation: findUser.organisation,
username: findUser.username,
accessToken: this.jwtService.sign({
sub: findUser._id,
role: findUser.role,
username: findUser.username,
}),
};

return user;
} else {
return null;
Expand Down
35 changes: 35 additions & 0 deletions src/server/workspace/src/material/dto/create-material.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IsNotEmpty, IsString } from 'class-validator';

//this class just encapsulates data and does validation
export class CreateMaterialDto {
//this is the type
//either 3d model orpdf
@IsNotEmpty()
//@isBoolean(true)
type: boolean;

//this is the workspace id
@IsNotEmpty()
@IsString()
workspace_id: string;

//this is the lecturer id
@IsNotEmpty()
@IsString()
lecturer_id: string;

//this is the title of the material
@IsNotEmpty()
@IsString()
title: string;

//this is the description of the material
@IsNotEmpty()
@IsString()
description: string;

//this is the filepath of the material
@IsNotEmpty()
@IsString()
file_path: string;
}
15 changes: 15 additions & 0 deletions src/server/workspace/src/material/dto/update-material.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IsOptional, IsString, IsUrl } from 'class-validator';

export class UpdateMaterialDto {
@IsString()
@IsOptional()
file_path?: string;

@IsUrl()
@IsOptional()
title?: string;

@IsUrl()
@IsOptional()
description?: string;
}
93 changes: 93 additions & 0 deletions src/server/workspace/src/material/material.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MaterialController } from './material.controller';
import { MaterialService } from './material.service';
import { CreateMaterialDto } from './dto/create-material.dto';
import { UpdateMaterialDto } from './dto/update-material.dto';

describe('MaterialController', () => {
let controller: MaterialController;
let service: MaterialService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [MaterialController],
providers: [
{
provide: MaterialService,
useValue: {
create: jest.fn(),
findOne: jest.fn(),
findAllByWorkspaceId: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
},
},
],
}).compile();

controller = module.get<MaterialController>(MaterialController);
service = module.get<MaterialService>(MaterialService);
});

describe('create', () => {
it('should call the create method of the service', async () => {
const createMaterialDto: CreateMaterialDto = {
type: true,
workspace_id: 'workspace-id-1',
lecturer_id: 'lecturer-id-1',
title: 'Test Material',
description: 'Test Description',
file_path: '/path/to/file.pdf',
};

await controller.create(createMaterialDto);

expect(service.create).toHaveBeenCalledWith(createMaterialDto);
});
});

describe('findOne', () => {
it('should call the findOne method of the service', async () => {
const id = 'test-id';

await controller.findOne(id);

expect(service.findOne).toHaveBeenCalledWith(id);
});
});

describe('findAllByWorkspaceId', () => {
it('should call the findAllByWorkspaceId method of the service', async () => {
const workspaceId = 'workspace-id-1';

await controller.findAllByWorkspaceId(workspaceId);

expect(service.findAllByWorkspaceId).toHaveBeenCalledWith(workspaceId);
});
});

describe('update', () => {
it('should call the update method of the service', async () => {
const id = 'test-id';
const updateMaterialDto: UpdateMaterialDto = {
title: 'Updated Material',
description: 'Updated Description',
file_path: '/path/to/updated-file.pdf',
};

await controller.update(id, updateMaterialDto);

expect(service.update).toHaveBeenCalledWith(id, updateMaterialDto);
});
});

describe('delete', () => {
it('should call the delete method of the service', async () => {
const id = 'test-id';

await controller.delete(id);

expect(service.delete).toHaveBeenCalledWith(id);
});
});
});
51 changes: 51 additions & 0 deletions src/server/workspace/src/material/material.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//this will handle all the http reqs and the responses

import {
Get,
Put,
Body,
Post,
Param,
Delete,
Controller,
} from '@nestjs/common';

import { MaterialService } from './material.service';
import { CreateMaterialDto } from './dto/create-material.dto';
import { UpdateMaterialDto } from './dto/update-material.dto';
import { Material } from '../schemas/material.schema';

@Controller('materials')
export class MaterialController {
constructor(private readonly materialService: MaterialService) {}

@Post()
async create(@Body() createMaterialDto: CreateMaterialDto) {
return this.materialService.create(createMaterialDto);
}

@Get(':id')
async findOne(@Param('id') id: string) {
return this.materialService.findOne(id);
}

@Get('workspace/:workspaceId')
async findAllByWorkspaceId(
@Param('workspaceId') workspaceId: string,
): Promise<Material[]> {
return this.materialService.findAllByWorkspaceId(workspaceId);
}

@Put(':id')
async update(
@Param('id') id: string,
@Body() updateMaterialDto: UpdateMaterialDto,
) {
return this.materialService.update(id, updateMaterialDto);
}

@Delete(':id')
async delete(@Param('id') id: string) {
return this.materialService.delete(id);
}
}
20 changes: 20 additions & 0 deletions src/server/workspace/src/material/material.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//this will encapsulate the functionality and components

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

import { Material, MaterialSchema } from '../schemas/material.schema';
import { MaterialService } from './material.service';
import { MaterialController } from './material.controller';

@Module({
imports: [
MongooseModule.forFeature([
{ name: Material.name, schema: MaterialSchema },
]),
],
providers: [MaterialService],
controllers: [MaterialController],
exports: [MaterialService],
})
export class MaterialModule {}
Loading
Loading