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 4 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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Warnings:

- You are about to drop the column `ownerId` on the `Room` table. All the data in the column will be lost.

*/
-- DropForeignKey
ALTER TABLE "Room" DROP CONSTRAINT "Room_ownerId_fkey";

-- DropIndex
DROP INDEX "Room_ownerId_key";

-- AlterTable
ALTER TABLE "Room" DROP COLUMN "ownerId";

-- CreateTable
CREATE TABLE "useronroom" (
"id" SERIAL NOT NULL,
"userid" INTEGER NOT NULL,
"roomid" INTEGER NOT NULL,

CONSTRAINT "useronroom_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "useronroom" ADD CONSTRAINT "useronroom_userid_fkey" FOREIGN KEY ("userid") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "useronroom" ADD CONSTRAINT "useronroom_roomid_fkey" FOREIGN KEY ("roomid") REFERENCES "Room"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- Added the required column `role` to the `useronroom` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "useronroom" ADD COLUMN "role" TEXT NOT NULL;
kotto5 marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 12 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,20 @@ model User {
email String @unique
name String?
password String
rooms useronroom[]
}

model Room {
id Int @id @default(autoincrement())
name String
}
users useronroom[]
}

kotto5 marked this conversation as resolved.
Show resolved Hide resolved
model useronroom {
id Int @id @default(autoincrement())
user User @relation(fields: [userid], references: [id])
userid Int
role String
room Room @relation(fields: [roomid], references: [id])
roomid Int
}
18 changes: 16 additions & 2 deletions backend/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,29 @@ async function main() {
update: {},
create: {
name: 'Room 1',
users: {
connect: [
{ id: user1.id },
{ id: user2.id },
{ id: user3.id },
{ id: user4.id },
],
}
},
});
const room2 = await prisma.room.upsert({
where: { id: 2 },
update: {},
create: {
name: 'Room 2',
name: 'Room 2',
users: {
connect: [
{ id: user1.id },
{ id: user2.id },
{ id: user4.id },
],
},
});
}});
}

main()
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()
userId: 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, userId: request['user']['id']});
kotto5 marked this conversation as resolved.
Show resolved Hide resolved
}

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

create(createRoomDto: CreateRoomDto) {
return this.prisma.room.create({ data: createRoomDto });
}
return this.prisma.room.create({
data: {
name: createRoomDto.name,
users: {
create: [
{
userid: createRoomDto.userId,
role: 'owner',
},
],
},
},
})
};
kotto5 marked this conversation as resolved.
Show resolved Hide resolved

findAll() {
return this.prisma.room.findMany();
Expand Down
Loading