diff --git a/src/room/controllers/room.controller.ts b/src/room/controllers/room.controller.ts index a998077..b5ffb15 100644 --- a/src/room/controllers/room.controller.ts +++ b/src/room/controllers/room.controller.ts @@ -7,11 +7,13 @@ import { HttpStatus, Param, Post, + Put, UseGuards, } 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'; @ApiTags('Room Module') @Controller({ @@ -73,4 +75,29 @@ export class RoomController { } } } + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Put('rename/:roomUuid') + async renameRoomByUuid( + @Param('roomUuid') roomUuid: string, + @Body() updateRoomNameDto: UpdateRoomNameDto, + ) { + try { + const room = await this.roomService.renameRoomByUuid( + roomUuid, + updateRoomNameDto, + ); + return room; + } catch (error) { + if (error.status === 404) { + throw new HttpException('Room not found', HttpStatus.NOT_FOUND); + } else { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + } } diff --git a/src/room/dtos/update.room.dto.ts b/src/room/dtos/update.room.dto.ts new file mode 100644 index 0000000..8f54092 --- /dev/null +++ b/src/room/dtos/update.room.dto.ts @@ -0,0 +1,16 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class UpdateRoomNameDto { + @ApiProperty({ + description: 'roomName', + required: true, + }) + @IsString() + @IsNotEmpty() + public roomName: string; + + constructor(dto: Partial) { + Object.assign(this, dto); + } +} diff --git a/src/room/interface/room.interface.ts b/src/room/interface/room.interface.ts index f98a8c9..3f1d817 100644 --- a/src/room/interface/room.interface.ts +++ b/src/room/interface/room.interface.ts @@ -12,3 +12,8 @@ export interface RoomParentInterface { type: string; parent?: RoomParentInterface; } +export interface RenameRoomByUuidInterface { + uuid: string; + name: string; + type: string; +} diff --git a/src/room/services/room.service.ts b/src/room/services/room.service.ts index ae0d403..24d5301 100644 --- a/src/room/services/room.service.ts +++ b/src/room/services/room.service.ts @@ -1,11 +1,18 @@ import { SpaceTypeRepository } from '../../../libs/common/src/modules/space-type/repositories/space.type.repository'; -import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { + Injectable, + HttpException, + HttpStatus, + BadRequestException, +} from '@nestjs/common'; import { SpaceRepository } from '@app/common/modules/space/repositories'; import { AddRoomDto } from '../dtos'; import { RoomParentInterface, GetRoomByUuidInterface, + RenameRoomByUuidInterface, } from '../interface/room.interface'; +import { UpdateRoomNameDto } from '../dtos/update.room.dto'; @Injectable() export class RoomService { @@ -93,4 +100,42 @@ export class RoomService { ); } } + async renameRoomByUuid( + roomUuid: string, + updateRoomNameDto: UpdateRoomNameDto, + ): Promise { + try { + const room = await this.spaceRepository.findOneOrFail({ + where: { uuid: roomUuid }, + relations: ['spaceType'], + }); + + if (!room || !room.spaceType || room.spaceType.type !== 'room') { + throw new BadRequestException('Invalid room UUID'); + } + + await this.spaceRepository.update( + { uuid: roomUuid }, + { spaceName: updateRoomNameDto.roomName }, + ); + + // Fetch the updated room + const updateRoom = await this.spaceRepository.findOneOrFail({ + where: { uuid: roomUuid }, + relations: ['spaceType'], + }); + + return { + uuid: updateRoom.uuid, + name: updateRoom.spaceName, + type: updateRoom.spaceType.type, + }; + } catch (err) { + if (err instanceof BadRequestException) { + throw err; // Re-throw BadRequestException + } else { + throw new HttpException('Room not found', HttpStatus.NOT_FOUND); + } + } + } }