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; + } }