mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 09:57:28 +00:00
Add endpoint to rename floor by UUID
This commit is contained in:
@ -7,6 +7,7 @@ import {
|
||||
HttpStatus,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
@ -14,6 +15,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddFloorDto } from '../dtos/add.floor.dto';
|
||||
import { GetFloorChildDto } from '../dtos/get.floor.dto';
|
||||
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
||||
|
||||
@ApiTags('Floor Module')
|
||||
@Controller({
|
||||
@ -99,4 +101,29 @@ export class FloorController {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('rename/:floorUuid')
|
||||
async renameFloorByUuid(
|
||||
@Param('floorUuid') floorUuid: string,
|
||||
@Body() updateFloorNameDto: UpdateFloorNameDto,
|
||||
) {
|
||||
try {
|
||||
const floor = await this.floorService.renameFloorByUuid(
|
||||
floorUuid,
|
||||
updateFloorNameDto,
|
||||
);
|
||||
return floor;
|
||||
} catch (error) {
|
||||
if (error.status === 404) {
|
||||
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
|
||||
} else {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
16
src/floor/dtos/update.floor.dto.ts
Normal file
16
src/floor/dtos/update.floor.dto.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class UpdateFloorNameDto {
|
||||
@ApiProperty({
|
||||
description: 'floorName',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public floorName: string;
|
||||
|
||||
constructor(dto: Partial<UpdateFloorNameDto>) {
|
||||
Object.assign(this, dto);
|
||||
}
|
||||
}
|
@ -19,3 +19,8 @@ export interface FloorParentInterface {
|
||||
type: string;
|
||||
parent?: FloorParentInterface;
|
||||
}
|
||||
export interface RenameFloorByUuidInterface {
|
||||
uuid: string;
|
||||
name: string;
|
||||
type: string;
|
||||
}
|
||||
|
@ -1,14 +1,21 @@
|
||||
import { GetFloorChildDto } from '../dtos/get.floor.dto';
|
||||
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 { AddFloorDto } from '../dtos';
|
||||
import {
|
||||
FloorChildInterface,
|
||||
FloorParentInterface,
|
||||
GetFloorByUuidInterface,
|
||||
RenameFloorByUuidInterface,
|
||||
} from '../interface/floor.interface';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
||||
|
||||
@Injectable()
|
||||
export class FloorService {
|
||||
@ -177,4 +184,43 @@ export class FloorService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async renameFloorByUuid(
|
||||
floorUuid: string,
|
||||
updateFloorDto: UpdateFloorNameDto,
|
||||
): Promise<RenameFloorByUuidInterface> {
|
||||
try {
|
||||
const floor = await this.spaceRepository.findOneOrFail({
|
||||
where: { uuid: floorUuid },
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
|
||||
if (!floor || !floor.spaceType || floor.spaceType.type !== 'floor') {
|
||||
throw new BadRequestException('Invalid floor UUID');
|
||||
}
|
||||
|
||||
await this.spaceRepository.update(
|
||||
{ uuid: floorUuid },
|
||||
{ spaceName: updateFloorDto.floorName },
|
||||
);
|
||||
|
||||
// Fetch the updated floor
|
||||
const updatedFloor = await this.spaceRepository.findOneOrFail({
|
||||
where: { uuid: floorUuid },
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
|
||||
return {
|
||||
uuid: updatedFloor.uuid,
|
||||
name: updatedFloor.spaceName,
|
||||
type: updatedFloor.spaceType.type,
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user