Add endpoint to rename room by UUID

This commit is contained in:
faris Aljohari
2024-04-14 10:57:13 +03:00
parent bf60303ddc
commit 53628236a6
4 changed files with 94 additions and 1 deletions

View File

@ -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,
);
}
}
}
}

View File

@ -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<UpdateRoomNameDto>) {
Object.assign(this, dto);
}
}

View File

@ -12,3 +12,8 @@ export interface RoomParentInterface {
type: string;
parent?: RoomParentInterface;
}
export interface RenameRoomByUuidInterface {
uuid: string;
name: string;
type: string;
}

View File

@ -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<RenameRoomByUuidInterface> {
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);
}
}
}
}