From 9949ac0358036134f0b3bf75c87821e9ad98bf74 Mon Sep 17 00:00:00 2001 From: sedeve Date: Wed, 18 Oct 2023 22:51:13 +0100 Subject: [PATCH] add: list all joind/all rooms --- .../code/src/friends/dto/query-ofsset-dto.ts | 4 +++- backend/code/src/main.ts | 1 + backend/code/src/rooms/dto/list-rooms.dto.ts | 12 ++++++++++ backend/code/src/rooms/dto/room-search.dto.ts | 3 ++- backend/code/src/rooms/rooms.controller.ts | 24 ++++++++++++++++++- backend/code/src/rooms/rooms.service.ts | 22 +++++++++++++++++ 6 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 backend/code/src/rooms/dto/list-rooms.dto.ts diff --git a/backend/code/src/friends/dto/query-ofsset-dto.ts b/backend/code/src/friends/dto/query-ofsset-dto.ts index a15bb1a..f269aa1 100644 --- a/backend/code/src/friends/dto/query-ofsset-dto.ts +++ b/backend/code/src/friends/dto/query-ofsset-dto.ts @@ -1,11 +1,13 @@ +import { ApiProperty } from '@nestjs/swagger'; import { Transform } from 'class-transformer'; import { IsNumber } from 'class-validator'; export class QueryOffsetDto { + @ApiProperty() @Transform(({ value }) => parseInt(value)) @IsNumber() offset: number; - + @ApiProperty() @Transform(({ value }) => parseInt(value)) @IsNumber() limit: number; diff --git a/backend/code/src/main.ts b/backend/code/src/main.ts index 9985380..71a64fc 100644 --- a/backend/code/src/main.ts +++ b/backend/code/src/main.ts @@ -26,6 +26,7 @@ async function bootstrap() { new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, + transform: true, }), ); app.use(passport.initialize()); diff --git a/backend/code/src/rooms/dto/list-rooms.dto.ts b/backend/code/src/rooms/dto/list-rooms.dto.ts new file mode 100644 index 0000000..9bfee18 --- /dev/null +++ b/backend/code/src/rooms/dto/list-rooms.dto.ts @@ -0,0 +1,12 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { Type } from 'class-transformer'; +import { IsBoolean, IsOptional } from 'class-validator'; +import { QueryOffsetDto } from 'src/friends/dto/query-ofsset-dto'; + +export class ListRoomsDto extends QueryOffsetDto { + @ApiProperty({ required: false }) + @IsOptional() + @IsBoolean() + @Type(() => Boolean) + joined: boolean; +} diff --git a/backend/code/src/rooms/dto/room-search.dto.ts b/backend/code/src/rooms/dto/room-search.dto.ts index 9c272b8..e12bcb7 100644 --- a/backend/code/src/rooms/dto/room-search.dto.ts +++ b/backend/code/src/rooms/dto/room-search.dto.ts @@ -1,8 +1,9 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsNotEmpty } from 'class-validator'; +import { IsNotEmpty, IsString } from 'class-validator'; export class RoomSearchDto { @ApiProperty() @IsNotEmpty() + @IsString() q: string; } diff --git a/backend/code/src/rooms/rooms.controller.ts b/backend/code/src/rooms/rooms.controller.ts index be38910..dd11f12 100644 --- a/backend/code/src/rooms/rooms.controller.ts +++ b/backend/code/src/rooms/rooms.controller.ts @@ -20,8 +20,9 @@ import { UpdateRoomDto } from './dto/update-room.dto'; import { ChangeOwnerDto } from './dto/change-owner.dto'; import { QueryOffsetDto } from '../friends/dto/query-ofsset-dto'; import { RoomSearchDto } from './dto/room-search.dto'; -import { ApiCookieAuth, ApiResponse, ApiTags } from '@nestjs/swagger'; +import { ApiCookieAuth, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger'; import { RoomDataDto } from './dto/room-data.dto'; +import { ListRoomsDto } from './dto/list-rooms.dto'; @ApiTags('rooms') @ApiCookieAuth('X-Access-Token') @@ -175,4 +176,25 @@ export class RoomsController { ) { return await this.roomsService.addMember(memberData, userId); } + + @ApiResponse({ + status: HttpStatus.OK, + schema: { + default: { + id: 'string', + name: 'string', + type: 'string', + }, + }, + }) + @ApiQuery({ type: ListRoomsDto }) + @Get('') + @HttpCode(HttpStatus.OK) + @UseGuards(AtGuard) + async listRooms( + @GetCurrentUser('userId') userId: string, + @Query() { offset, limit, joined }: ListRoomsDto, + ) { + return this.roomsService.listRooms(userId, offset, limit, joined); + } } diff --git a/backend/code/src/rooms/rooms.service.ts b/backend/code/src/rooms/rooms.service.ts index 137fb63..7359f58 100644 --- a/backend/code/src/rooms/rooms.service.ts +++ b/backend/code/src/rooms/rooms.service.ts @@ -396,4 +396,26 @@ export class RoomsService { }); return { message: 'User added successfully' }; } + + async listRooms( + userId: string, + offset: number, + limit: number, + joined: boolean, + ) { + const rooms = await this.prisma.room.findMany({ + skip: offset, + take: limit, + where: { + ...(joined && { members: { some: { userId: userId } } }), + ...(!joined && { OR: [{ type: 'public' }, { type: 'protected' }] }), + }, + select: { + id: true, + name: true, + type: true, + }, + }); + return rooms; + } }