mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 17:24:54 +00:00
Add endpoint to rename building by UUID
This commit is contained in:
@ -7,6 +7,7 @@ import {
|
|||||||
HttpStatus,
|
HttpStatus,
|
||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
|
Put,
|
||||||
Query,
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
@ -14,6 +15,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
|||||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
import { AddBuildingDto } from '../dtos/add.building.dto';
|
import { AddBuildingDto } from '../dtos/add.building.dto';
|
||||||
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
||||||
|
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
||||||
|
|
||||||
@ApiTags('Building Module')
|
@ApiTags('Building Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -101,4 +103,28 @@ export class BuildingController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Put('rename/:buildingUuid')
|
||||||
|
async renameBuildingByUuid(
|
||||||
|
@Param('buildingUuid') buildingUuid: string,
|
||||||
|
@Body() updateBuildingDto: UpdateBuildingNameDto,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const building = await this.buildingService.renameBuildingByUuid(
|
||||||
|
buildingUuid,
|
||||||
|
updateBuildingDto,
|
||||||
|
);
|
||||||
|
return building;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.status === 404) {
|
||||||
|
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/building/dtos/update.building.dto.ts
Normal file
16
src/building/dtos/update.building.dto.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class UpdateBuildingNameDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'buildingName',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public buildingName: string;
|
||||||
|
|
||||||
|
constructor(dto: Partial<UpdateBuildingNameDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -19,3 +19,8 @@ export interface BuildingParentInterface {
|
|||||||
type: string;
|
type: string;
|
||||||
parent?: BuildingParentInterface;
|
parent?: BuildingParentInterface;
|
||||||
}
|
}
|
||||||
|
export interface RenameBuildingByUuidInterface {
|
||||||
|
uuid: string;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,14 +1,21 @@
|
|||||||
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
||||||
import { SpaceTypeRepository } from '../../../libs/common/src/modules/space-type/repositories/space.type.repository';
|
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 { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddBuildingDto } from '../dtos';
|
import { AddBuildingDto } from '../dtos';
|
||||||
import {
|
import {
|
||||||
BuildingChildInterface,
|
BuildingChildInterface,
|
||||||
BuildingParentInterface,
|
BuildingParentInterface,
|
||||||
GetBuildingByUuidInterface,
|
GetBuildingByUuidInterface,
|
||||||
|
RenameBuildingByUuidInterface,
|
||||||
} from '../interface/building.interface';
|
} from '../interface/building.interface';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||||
|
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BuildingService {
|
export class BuildingService {
|
||||||
@ -179,4 +186,47 @@ export class BuildingService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async renameBuildingByUuid(
|
||||||
|
buildingUuid: string,
|
||||||
|
updateBuildingNameDto: UpdateBuildingNameDto,
|
||||||
|
): Promise<RenameBuildingByUuidInterface> {
|
||||||
|
try {
|
||||||
|
const building = await this.spaceRepository.findOneOrFail({
|
||||||
|
where: { uuid: buildingUuid },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
!building ||
|
||||||
|
!building.spaceType ||
|
||||||
|
building.spaceType.type !== 'building'
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('Invalid building UUID');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.spaceRepository.update(
|
||||||
|
{ uuid: buildingUuid },
|
||||||
|
{ spaceName: updateBuildingNameDto.buildingName },
|
||||||
|
);
|
||||||
|
|
||||||
|
// Fetch the updated building
|
||||||
|
const updatedBuilding = await this.spaceRepository.findOneOrFail({
|
||||||
|
where: { uuid: buildingUuid },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
uuid: updatedBuilding.uuid,
|
||||||
|
name: updatedBuilding.spaceName,
|
||||||
|
type: updatedBuilding.spaceType.type,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof BadRequestException) {
|
||||||
|
throw err; // Re-throw BadRequestException
|
||||||
|
} else {
|
||||||
|
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user