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

feat: added room service with code generation and tests #224

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions backend/src/room/dto/create-room.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class CreateRoomDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
name: string;

@ApiProperty()
@IsNotEmpty()
@IsString()
description: string;

// @ApiProperty()
// @IsNotEmpty()
// @IsString()
// code: string;
}
4 changes: 4 additions & 0 deletions backend/src/room/dto/update-room.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateRoomDto } from './create-room.dto';

export class UpdateRoomDto extends PartialType(CreateRoomDto) {}
22 changes: 22 additions & 0 deletions backend/src/room/entities/room.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';

@Entity('rooms')
export class Room {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column()
name: string;

@Column('text')
description: string;

@Column()
code: string;

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;
}
128 changes: 128 additions & 0 deletions backend/src/room/room.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RoomController } from './room.controller';
import { RoomService } from './room.service';
import { CreateRoomDto } from './dto/create-room.dto';
import { UpdateRoomDto } from './dto/update-room.dto';
import { NotFoundException } from '@nestjs/common';

describe('RoomController', () => {
let controller: RoomController;
let service: RoomService;

const mockRoomService = {
create: jest.fn().mockResolvedValue({
id: '1',
name: 'Test Room Name',
code: 'RANDOM',
description: 'Test Room Description',
}),
// create: jest.fn().mockImplementation((dto) => ({ id: '1', ...dto })),
findAll: jest.fn().mockResolvedValue([]),
findOne: jest.fn().mockImplementation((id) => {
if (id === '1')
return Promise.resolve({
id: '1',
name: 'Test Room Name',
description: 'Test Room Description',
code: 'RANDOM',
});
throw new NotFoundException('Room not found');
}),
update: jest.fn().mockImplementation((id, dto) => {
if (id === '1') return Promise.resolve({ id: '1', ...dto });
throw new NotFoundException('Room not found');
}),
remove: jest.fn().mockImplementation((id) => {
if (id === '1') return Promise.resolve(true);
throw new NotFoundException('Room not found');
}),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [RoomController],
providers: [{ provide: RoomService, useValue: mockRoomService }],
}).compile();

controller = module.get<RoomController>(RoomController);
service = module.get<RoomService>(RoomService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

it('should create a room', async () => {
const dto: CreateRoomDto = {
name: 'Test Room Name',
description: 'Test Room Description',
};
await expect(controller.create(dto)).resolves.toEqual({
id: '1',
name: 'Test Room Name',
code: 'RANDOM',
description: 'Test Room Description',
});
expect(service.create).toHaveBeenCalledWith(dto);
});

it('should return all rooms', async () => {
await expect(controller.findAll()).resolves.toEqual([]);
expect(service.findAll).toHaveBeenCalled();
});

it('should return a room by id', async () => {
await expect(controller.findOne('1')).resolves.toEqual({
id: '1',
name: 'Test Room Name',
description: 'Test Room Description',
code: 'RANDOM',
});
expect(service.findOne).toHaveBeenCalledWith('1');
});

it('should throw an error if room not found', async () => {
try {
await controller.findOne('2');
} catch (error) {
expect(error).toBeInstanceOf(NotFoundException);
}
});

it('should update a room', async () => {
const updateDto: UpdateRoomDto = {
name: 'Updated Room',
description: 'Test Room Description'
};
await expect(controller.update('1', updateDto)).resolves.toEqual({
id: '1',
...updateDto,
});
expect(service.update).toHaveBeenCalledWith('1', updateDto);
});

it('should throw an error if updating a non-existent room', async () => {
const updateDto: UpdateRoomDto = {
name: 'Updated Room',
description: 'Test Room Description'
};
try {
await expect(controller.update('2', updateDto));
} catch (error) {
expect(error).toBeInstanceOf(NotFoundException);
}
});

it('should delete a room', async () => {
await expect(controller.remove('1')).resolves.toBeTruthy();
expect(service.remove).toHaveBeenCalledWith('1');
});

it('should throw an error if deleting a non-existent room', async () => {
try {
await expect(controller.remove('2'));
} catch (error) {
expect(error).toBeInstanceOf(NotFoundException);
}
});
});
103 changes: 103 additions & 0 deletions backend/src/room/room.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// src/room/room.controller.ts
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
Query,
} from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiResponse,
ApiParam,
ApiBody,
} from '@nestjs/swagger';
import { RoomService } from './room.service';
import { CreateRoomDto } from './dto/create-room.dto';
import { UpdateRoomDto } from './dto/update-room.dto';

@ApiTags('room')
@Controller('room')
export class RoomController {
constructor(private readonly roomService: RoomService) {}

@Post()
@ApiOperation({ summary: 'Create a new room' })
@ApiBody({ type: CreateRoomDto })
@ApiResponse({
status: 201,
description: 'Room created successfully',
})
@ApiResponse({
status: 400,
description: 'Invalid input',
})
create(@Body() createRoomDto: CreateRoomDto) {
return this.roomService.create(createRoomDto);
}

@Get()
@ApiOperation({ summary: 'Get all room' })
@ApiResponse({
status: 200,
description: 'List of all room successfully retrieved',
})
findAll() {
return this.roomService.findAll();
}

@Get(':id')
@ApiOperation({ summary: 'Get a room by id' })
findOne(@Param('id') id: string) {
return this.roomService.findOne(id);
}

@Patch(':id')
@ApiOperation({
summary: 'Update a room',
description: 'Update an existing room by its ID',
})
@ApiParam({
name: 'id',
description: 'Unique identifier of the room to be updated',
type: 'string',
})
@ApiBody({ type: UpdateRoomDto })
@ApiResponse({
status: 200,
description: 'Room successfully updated',
})
@ApiResponse({
status: 404,
description: 'Room not found',
})
update(@Param('id') id: string, @Body() updateRoomDto: UpdateRoomDto) {
return this.roomService.update(id, updateRoomDto);
}

@Delete(':id')
@ApiOperation({
summary: 'Delete a room',
description: 'Delete a room from the collection by its ID',
})
@ApiParam({
name: 'id',
description: 'Unique identifier of the room to be deleted',
type: 'string',
})
@ApiResponse({
status: 200,
description: 'Room successfully deleted',
})
@ApiResponse({
status: 404,
description: 'Room not found',
})
remove(@Param('id') id: string) {
return this.roomService.remove(id);
}
}
14 changes: 14 additions & 0 deletions backend/src/room/room.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// src/room/room.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { RoomService } from './room.service';
import { RoomController } from './room.controller';
import { Room } from './entities/room.entity';

@Module({
imports: [TypeOrmModule.forFeature([Room])],
controllers: [RoomController],
providers: [RoomService],
exports: [RoomService],
})
export class RoomModule {}
Loading