Skip to content

Commit

Permalink
add: list all joind/all rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
sedeve committed Oct 18, 2023
1 parent bf69b24 commit 9949ac0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
4 changes: 3 additions & 1 deletion backend/code/src/friends/dto/query-ofsset-dto.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions backend/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async function bootstrap() {
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
);
app.use(passport.initialize());
Expand Down
12 changes: 12 additions & 0 deletions backend/code/src/rooms/dto/list-rooms.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion backend/code/src/rooms/dto/room-search.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 23 additions & 1 deletion backend/code/src/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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);
}
}
22 changes: 22 additions & 0 deletions backend/code/src/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 9949ac0

Please sign in to comment.