mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 17:54:54 +00:00
Add endpoint to get rooms by user ID
This commit is contained in:
@ -8,13 +8,16 @@ import {
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
UseGuards,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddRoomDto } from '../dtos/add.room.dto';
|
||||
import { UpdateRoomNameDto } from '../dtos/update.room.dto';
|
||||
import { CheckUnitTypeGuard } from 'src/guards/unit.type.guard';
|
||||
import { GetRoomByUserIdDto } from '../dtos/get.room.dto';
|
||||
|
||||
@ApiTags('Room Module')
|
||||
@Controller({
|
||||
@ -69,6 +72,20 @@ export class RoomController {
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get()
|
||||
async getRoomsByUserId(@Query(ValidationPipe) dto: GetRoomByUserIdDto) {
|
||||
try {
|
||||
return await this.roomService.getRoomsByUserId(dto.userUuid);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('rename/:roomUuid')
|
||||
|
||||
12
src/room/dtos/get.room.dto.ts
Normal file
12
src/room/dtos/get.room.dto.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class GetRoomByUserIdDto {
|
||||
@ApiProperty({
|
||||
description: 'userUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public userUuid: string;
|
||||
}
|
||||
@ -17,3 +17,8 @@ export interface RenameRoomByUuidInterface {
|
||||
name: string;
|
||||
type: string;
|
||||
}
|
||||
export interface GetRoomByUserUuidInterface {
|
||||
uuid: string;
|
||||
name: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
@ -6,11 +6,23 @@ import { SpaceRepositoryModule } from '@app/common/modules/space/space.repositor
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
||||
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
||||
import { UserSpaceRepositoryModule } from '@app/common/modules/user-space/user.space.repository.module';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
||||
imports: [
|
||||
ConfigModule,
|
||||
SpaceRepositoryModule,
|
||||
SpaceTypeRepositoryModule,
|
||||
UserSpaceRepositoryModule,
|
||||
],
|
||||
controllers: [RoomController],
|
||||
providers: [RoomService, SpaceRepository, SpaceTypeRepository],
|
||||
providers: [
|
||||
RoomService,
|
||||
SpaceRepository,
|
||||
SpaceTypeRepository,
|
||||
UserSpaceRepository,
|
||||
],
|
||||
exports: [RoomService],
|
||||
})
|
||||
export class RoomModule {}
|
||||
|
||||
@ -11,14 +11,17 @@ import {
|
||||
RoomParentInterface,
|
||||
GetRoomByUuidInterface,
|
||||
RenameRoomByUuidInterface,
|
||||
GetRoomByUserUuidInterface,
|
||||
} from '../interface/room.interface';
|
||||
import { UpdateRoomNameDto } from '../dtos/update.room.dto';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class RoomService {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||
private readonly userSpaceRepository: UserSpaceRepository,
|
||||
) {}
|
||||
|
||||
async addRoom(addRoomDto: AddRoomDto) {
|
||||
@ -103,6 +106,38 @@ export class RoomService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getRoomsByUserId(
|
||||
userUuid: string,
|
||||
): Promise<GetRoomByUserUuidInterface[]> {
|
||||
try {
|
||||
const rooms = await this.userSpaceRepository.find({
|
||||
relations: ['space', 'space.spaceType'],
|
||||
where: {
|
||||
user: { uuid: userUuid },
|
||||
space: { spaceType: { type: 'room' } },
|
||||
},
|
||||
});
|
||||
|
||||
if (rooms.length === 0) {
|
||||
throw new HttpException('this user has no rooms', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
const spaces = rooms.map((room) => ({
|
||||
uuid: room.space.uuid,
|
||||
name: room.space.spaceName,
|
||||
type: room.space.spaceType.type,
|
||||
}));
|
||||
|
||||
return spaces;
|
||||
} catch (err) {
|
||||
if (err instanceof HttpException) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new HttpException('user not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async renameRoomByUuid(
|
||||
roomUuid: string,
|
||||
updateRoomNameDto: UpdateRoomNameDto,
|
||||
|
||||
Reference in New Issue
Block a user