diff --git a/backend/prisma/migrations/20231109052515_room/migration.sql b/backend/prisma/migrations/20231109052515_room/migration.sql new file mode 100644 index 00000000..1fb69f84 --- /dev/null +++ b/backend/prisma/migrations/20231109052515_room/migration.sql @@ -0,0 +1,7 @@ +-- CreateTable +CREATE TABLE "Room" ( + "id" SERIAL NOT NULL, + "name" TEXT NOT NULL, + + CONSTRAINT "Room_pkey" PRIMARY KEY ("id") +); diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index 2016f4c3..05562a92 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -16,3 +16,8 @@ model User { name String? password String } + +model Room { + id Int @id @default(autoincrement()) + name String +} \ No newline at end of file diff --git a/backend/prisma/seed.ts b/backend/prisma/seed.ts index 279b4005..843709b0 100644 --- a/backend/prisma/seed.ts +++ b/backend/prisma/seed.ts @@ -56,6 +56,21 @@ async function main() { }); console.log({ user1, user2, user3, user4 }); + + const room1 = await prisma.room.upsert({ + where: { id: 1 }, + update: {}, + create: { + name: 'Room 1', + }, + }); + const room2 = await prisma.room.upsert({ + where: { id: 2 }, + update: {}, + create: { + name: 'Room 2', + }, + }); } main() diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index a44e1275..47c85030 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -4,9 +4,10 @@ import { AppService } from './app.service'; import { UserModule } from './user/user.module'; import { PrismaModule } from './prisma/prisma.module'; import { AuthModule } from './auth/auth.module'; +import { RoomModule } from './room/room.module'; @Module({ - imports: [UserModule, PrismaModule, AuthModule], + imports: [UserModule, PrismaModule, AuthModule, RoomModule], controllers: [AppController], providers: [AppService], }) diff --git a/backend/src/room/dto/create-room.dto.ts b/backend/src/room/dto/create-room.dto.ts new file mode 100644 index 00000000..854c2236 --- /dev/null +++ b/backend/src/room/dto/create-room.dto.ts @@ -0,0 +1,11 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString } from 'class-validator'; + +export class CreateRoomDto { + @ApiProperty() + id: number; + + @IsString() + @ApiProperty() + name: string; +} diff --git a/backend/src/room/dto/update-room.dto.ts b/backend/src/room/dto/update-room.dto.ts new file mode 100644 index 00000000..294bd61a --- /dev/null +++ b/backend/src/room/dto/update-room.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/swagger'; +import { CreateRoomDto } from './create-room.dto'; + +export class UpdateRoomDto extends PartialType(CreateRoomDto) {} diff --git a/backend/src/room/entities/room.entity.ts b/backend/src/room/entities/room.entity.ts new file mode 100644 index 00000000..34a76c45 --- /dev/null +++ b/backend/src/room/entities/room.entity.ts @@ -0,0 +1,10 @@ +import { Room } from '@prisma/client'; +import { ApiProperty } from '@nestjs/swagger'; + +export class RoomEntity implements Room { + @ApiProperty() + id: number; + + @ApiProperty() + name: string; +} diff --git a/backend/src/room/room.controller.spec.ts b/backend/src/room/room.controller.spec.ts new file mode 100644 index 00000000..bc26cadd --- /dev/null +++ b/backend/src/room/room.controller.spec.ts @@ -0,0 +1,21 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { RoomController } from './room.controller'; +import { RoomService } from './room.service'; +import { PrismaService } from 'src/prisma/prisma.service'; + +describe('RoomController', () => { + let controller: RoomController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [RoomController], + providers: [RoomService, PrismaService], + }).compile(); + + controller = module.get(RoomController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/backend/src/room/room.controller.ts b/backend/src/room/room.controller.ts new file mode 100644 index 00000000..99f15a97 --- /dev/null +++ b/backend/src/room/room.controller.ts @@ -0,0 +1,50 @@ +import { + Controller, + Get, + Post, + Body, + Patch, + Param, + Delete, +} from '@nestjs/common'; +import { RoomService } from './room.service'; +import { CreateRoomDto } from './dto/create-room.dto'; +import { UpdateRoomDto } from './dto/update-room.dto'; +import { ApiTags, ApiCreatedResponse, ApiOkResponse } from '@nestjs/swagger'; +import { RoomEntity } from './entities/room.entity'; + +@Controller('room') +@ApiTags('room') +export class RoomController { + constructor(private readonly roomService: RoomService) {} + + @Post() + @ApiCreatedResponse({ type: CreateRoomDto }) + create(@Body() createRoomDto: CreateRoomDto) { + return this.roomService.create(createRoomDto); + } + + @Get() + @ApiOkResponse({ type: RoomEntity, isArray: true }) + findAll() { + return this.roomService.findAll(); + } + + @Get(':id') + @ApiOkResponse({ type: RoomEntity }) + findOne(@Param('id') id: string) { + return this.roomService.findOne(+id); + } + + @Patch(':id') + @ApiOkResponse({ type: RoomEntity }) + update(@Param('id') id: string, @Body() updateRoomDto: UpdateRoomDto) { + return this.roomService.update(+id, updateRoomDto); + } + + @Delete(':id') + @ApiOkResponse({ type: RoomEntity }) + remove(@Param('id') id: string) { + return this.roomService.remove(+id); + } +} diff --git a/backend/src/room/room.module.ts b/backend/src/room/room.module.ts new file mode 100644 index 00000000..c8f36bfb --- /dev/null +++ b/backend/src/room/room.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { RoomService } from './room.service'; +import { RoomController } from './room.controller'; +import { PrismaModule } from 'src/prisma/prisma.module'; + +@Module({ + controllers: [RoomController], + providers: [RoomService], + imports: [PrismaModule], +}) +export class RoomModule {} diff --git a/backend/src/room/room.service.spec.ts b/backend/src/room/room.service.spec.ts new file mode 100644 index 00000000..02a69671 --- /dev/null +++ b/backend/src/room/room.service.spec.ts @@ -0,0 +1,19 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { RoomService } from './room.service'; +import { PrismaService } from 'src/prisma/prisma.service'; + +describe('RoomService', () => { + let service: RoomService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [RoomService, PrismaService], + }).compile(); + + service = module.get(RoomService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/backend/src/room/room.service.ts b/backend/src/room/room.service.ts new file mode 100644 index 00000000..fe371282 --- /dev/null +++ b/backend/src/room/room.service.ts @@ -0,0 +1,32 @@ +import { Injectable } from '@nestjs/common'; +import { CreateRoomDto } from './dto/create-room.dto'; +import { UpdateRoomDto } from './dto/update-room.dto'; +import { PrismaService } from 'src/prisma/prisma.service'; + +@Injectable() +export class RoomService { + constructor(private prisma: PrismaService) {} + + create(createRoomDto: CreateRoomDto) { + return this.prisma.room.create({ data: createRoomDto }); + } + + findAll() { + return this.prisma.room.findMany(); + } + + findOne(id: number) { + return this.prisma.room.findUniqueOrThrow({ where: { id: id } }); + } + + update(id: number, updateRoomDto: UpdateRoomDto) { + return this.prisma.room.update({ + where: { id }, + data: updateRoomDto, + }); + } + + remove(id: number) { + return this.prisma.room.delete({ where: { id } }); + } +}