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/backend/room crud #36

Merged
merged 12 commits into from
Nov 9, 2023
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
7 changes: 7 additions & 0 deletions backend/prisma/migrations/20231109052515_room/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- CreateTable
CREATE TABLE "Room" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,

CONSTRAINT "Room_pkey" PRIMARY KEY ("id")
);
5 changes: 5 additions & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ model User {
name String?
password String
}

model Room {
id Int @id @default(autoincrement())
name String
}
15 changes: 15 additions & 0 deletions backend/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
}
kotto5 marked this conversation as resolved.
Show resolved Hide resolved

main()
kotto5 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
3 changes: 2 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
Expand Down
11 changes: 11 additions & 0 deletions backend/src/room/dto/create-room.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class CreateRoomDto {
@ApiProperty()
id: number;
kotto5 marked this conversation as resolved.
Show resolved Hide resolved

@IsString()
@ApiProperty()
name: 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) {}
10 changes: 10 additions & 0 deletions backend/src/room/entities/room.entity.ts
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions backend/src/room/room.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(RoomController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
50 changes: 50 additions & 0 deletions backend/src/room/room.controller.ts
Original file line number Diff line number Diff line change
@@ -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);
}
kotto5 marked this conversation as resolved.
Show resolved Hide resolved

@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);
}
}
11 changes: 11 additions & 0 deletions backend/src/room/room.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
19 changes: 19 additions & 0 deletions backend/src/room/room.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(RoomService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
32 changes: 32 additions & 0 deletions backend/src/room/room.service.ts
Original file line number Diff line number Diff line change
@@ -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 } });
}
}