diff --git a/backend/prisma/migrations/20231111115551_add_oweer_id/migration.sql b/backend/prisma/migrations/20231111115551_add_oweer_id/migration.sql new file mode 100644 index 00000000..22ba93f4 --- /dev/null +++ b/backend/prisma/migrations/20231111115551_add_oweer_id/migration.sql @@ -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; diff --git a/backend/prisma/migrations/20231113020147_many_to_many_userroom/migration.sql b/backend/prisma/migrations/20231113020147_many_to_many_userroom/migration.sql new file mode 100644 index 00000000..34443dd2 --- /dev/null +++ b/backend/prisma/migrations/20231113020147_many_to_many_userroom/migration.sql @@ -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; diff --git a/backend/prisma/migrations/20231113023444_add_role_column_user/migration.sql b/backend/prisma/migrations/20231113023444_add_role_column_user/migration.sql new file mode 100644 index 00000000..63ee3696 --- /dev/null +++ b/backend/prisma/migrations/20231113023444_add_role_column_user/migration.sql @@ -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; diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index 05562a92..3649b95a 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -15,9 +15,20 @@ model User { email String @unique name String? password String + rooms useronroom[] } model Room { id Int @id @default(autoincrement()) name String -} \ No newline at end of file + users useronroom[] +} + +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 +} diff --git a/backend/prisma/seed.ts b/backend/prisma/seed.ts index 843709b0..f18e65c5 100644 --- a/backend/prisma/seed.ts +++ b/backend/prisma/seed.ts @@ -62,6 +62,14 @@ 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({ @@ -69,6 +77,9 @@ async function main() { update: {}, create: { name: 'Room 2', + users: { + connect: [{ id: user1.id }, { id: user2.id }, { id: user4.id }], + }, }, }); } diff --git a/backend/src/room/dto/create-room-request.dto.ts b/backend/src/room/dto/create-room-request.dto.ts new file mode 100644 index 00000000..edac03e3 --- /dev/null +++ b/backend/src/room/dto/create-room-request.dto.ts @@ -0,0 +1,9 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class CreateRoomRequestDto { + @IsNotEmpty() + @IsString() + @ApiProperty() + name: string; +} diff --git a/backend/src/room/dto/create-room.dto.ts b/backend/src/room/dto/create-room.dto.ts index 846c2794..065c983b 100644 --- a/backend/src/room/dto/create-room.dto.ts +++ b/backend/src/room/dto/create-room.dto.ts @@ -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; } diff --git a/backend/src/room/entities/room.entity.ts b/backend/src/room/entities/room.entity.ts index 34a76c45..2d6b31dd 100644 --- a/backend/src/room/entities/room.entity.ts +++ b/backend/src/room/entities/room.entity.ts @@ -2,9 +2,14 @@ import { Room } from '@prisma/client'; import { ApiProperty } from '@nestjs/swagger'; export class RoomEntity implements Room { - @ApiProperty() + constructor(partial: Partial) { + Object.assign(this, partial); + } id: number; @ApiProperty() name: string; + + @ApiProperty() + ownerId: number; } diff --git a/backend/src/room/room.controller.ts b/backend/src/room/room.controller.ts index f2fcca58..56fbb51c 100644 --- a/backend/src/room/room.controller.ts +++ b/backend/src/room/room.controller.ts @@ -7,12 +7,21 @@ 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') @@ -20,9 +29,17 @@ 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'], + }); } @Get() diff --git a/backend/src/room/room.service.ts b/backend/src/room/room.service.ts index fe371282..317c62ed 100644 --- a/backend/src/room/room.service.ts +++ b/backend/src/room/room.service.ts @@ -8,7 +8,19 @@ 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', + }, + ], + }, + }, + }); } findAll() {