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 user relation #41

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Warnings:

- A unique constraint covering the columns `[ownerId]` on the table `Room` will be added. If there are existing duplicate values, this will fail.
- Added the required column `ownerId` to the `Room` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "Room" ADD COLUMN "ownerId" INTEGER NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "Room_ownerId_key" ON "Room"("ownerId");

-- AddForeignKey
ALTER TABLE "Room" ADD CONSTRAINT "Room_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
5 changes: 4 additions & 1 deletion backend/prisma/schema.prisma
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

口頭でも言いましたが、一つの機能(PR)の中でmigrationファイルが3つに分かれているのはわかりづらいので、(経過としてはいいですが)、一回migrationファイルを消してからprisma migrate devしなおすとかしてからgit rebaseし直すなどして欲しかったですね!

やり方はわかりますかね?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

分からない! todo

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ model User {
email String @unique
name String?
password String
room Room?
}

model Room {
id Int @id @default(autoincrement())
name String
}
owner User @relation(fields: [ownerId], references: [id])
ownerId Int @unique
}
4 changes: 3 additions & 1 deletion backend/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ async function main() {
update: {},
create: {
name: 'Room 1',
ownerId: user1.id,
},
});
const room2 = await prisma.room.upsert({
where: { id: 2 },
update: {},
create: {
name: 'Room 2',
name: 'Room 2',
ownerId: user2.id,
},
});
}
Expand Down
13 changes: 13 additions & 0 deletions backend/src/room/dto/create-room-request.dto.ts
kotto5 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// import { CreateRoomDto } from './create-room.dto';
import { ApiProperty } from '@nestjs/swagger';
import { Room } from '@prisma/client';

Check failure on line 3 in backend/src/room/dto/create-room-request.dto.ts

View workflow job for this annotation

GitHub Actions / Lint Backend

'Room' is defined but never used
import { CreateRoomDto } from './create-room.dto';

Check failure on line 4 in backend/src/room/dto/create-room-request.dto.ts

View workflow job for this annotation

GitHub Actions / Lint Backend

'CreateRoomDto' is defined but never used
import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator';

Check failure on line 5 in backend/src/room/dto/create-room-request.dto.ts

View workflow job for this annotation

GitHub Actions / Lint Backend

'IsEmail' is defined but never used

Check failure on line 5 in backend/src/room/dto/create-room-request.dto.ts

View workflow job for this annotation

GitHub Actions / Lint Backend

'IsNotEmpty' is defined but never used

Check failure on line 5 in backend/src/room/dto/create-room-request.dto.ts

View workflow job for this annotation

GitHub Actions / Lint Backend

'MinLength' is defined but never used
kotto5 marked this conversation as resolved.
Show resolved Hide resolved

// export type CreateRoomRequestDto = Omit<CreateRoomDto, 'ownerId'>;
kotto5 marked this conversation as resolved.
Show resolved Hide resolved

export class CreateRoomRequestDto {
@IsString()
@ApiProperty()
name: string;
}
9 changes: 5 additions & 4 deletions backend/src/room/dto/create-room.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty } from 'class-validator';
import { IsString, IsNotEmpty, IsNumber } from 'class-validator';

export class CreateRoomDto {
@ApiProperty()
id: number;

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

@IsNumber()
@ApiProperty()
ownerId: number;
}
7 changes: 6 additions & 1 deletion backend/src/room/entities/room.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { Room } from '@prisma/client';
import { ApiProperty } from '@nestjs/swagger';

export class RoomEntity implements Room {
@ApiProperty()
constructor(partial: Partial<RoomEntity>) {
Object.assign(this, partial);
}
id: number;
kotto5 marked this conversation as resolved.
Show resolved Hide resolved

@ApiProperty()
name: string;

@ApiProperty()
ownerId: number;
}
12 changes: 9 additions & 3 deletions backend/src/room/room.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ import {
Param,
Delete,
ParseIntPipe,
UseGuards,
Req,
} 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 { CreateRoomRequestDto } from './dto/create-room-request.dto';
import { ApiTags, ApiCreatedResponse, ApiOkResponse, ApiBearerAuth } from '@nestjs/swagger';
import { RoomEntity } from './entities/room.entity';
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';

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

@Post()
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiCreatedResponse({ type: CreateRoomDto })
create(@Body() createRoomDto: CreateRoomDto) {
return this.roomService.create(createRoomDto);
create(@Body() createRoomRequestDto: CreateRoomRequestDto, @Req() request: Request) {
return this.roomService.create({...createRoomRequestDto, ownerId: request['user']['id']});
kotto5 marked this conversation as resolved.
Show resolved Hide resolved
}

@Get()
Expand Down
2 changes: 1 addition & 1 deletion backend/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class RoomService {
constructor(private prisma: PrismaService) {}

create(createRoomDto: CreateRoomDto) {
return this.prisma.room.create({ data: createRoomDto });
return this.prisma.room.create({data: createRoomDto});
}

findAll() {
Expand Down
Loading