mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
156 lines
4.6 KiB
TypeScript
156 lines
4.6 KiB
TypeScript
import { BuildingService } from '../services/building.service';
|
|
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpException,
|
|
HttpStatus,
|
|
Param,
|
|
Post,
|
|
Put,
|
|
Query,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
|
import { AddBuildingDto, AddUserBuildingDto } from '../dtos/add.building.dto';
|
|
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
|
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
|
import { CheckCommunityTypeGuard } from 'src/guards/community.type.guard';
|
|
import { CheckUserBuildingGuard } from 'src/guards/user.building.guard';
|
|
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
|
import { BuildingPermissionGuard } from 'src/guards/building.permission.guard';
|
|
|
|
@ApiTags('Building Module')
|
|
@Controller({
|
|
version: '1',
|
|
path: 'building',
|
|
})
|
|
export class BuildingController {
|
|
constructor(private readonly buildingService: BuildingService) {}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(AdminRoleGuard, CheckCommunityTypeGuard)
|
|
@Post()
|
|
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
|
|
try {
|
|
const building = await this.buildingService.addBuilding(addBuildingDto);
|
|
return {
|
|
statusCode: HttpStatus.CREATED,
|
|
success: true,
|
|
message: 'Building added successfully',
|
|
data: building,
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
|
@Get(':buildingUuid')
|
|
async getBuildingByUuid(@Param('buildingUuid') buildingUuid: string) {
|
|
try {
|
|
const building =
|
|
await this.buildingService.getBuildingByUuid(buildingUuid);
|
|
return building;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
|
@Get('child/:buildingUuid')
|
|
async getBuildingChildByUuid(
|
|
@Param('buildingUuid') buildingUuid: string,
|
|
@Query() query: GetBuildingChildDto,
|
|
) {
|
|
try {
|
|
const building = await this.buildingService.getBuildingChildByUuid(
|
|
buildingUuid,
|
|
query,
|
|
);
|
|
return building;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
|
@Get('parent/:buildingUuid')
|
|
async getBuildingParentByUuid(@Param('buildingUuid') buildingUuid: string) {
|
|
try {
|
|
const building =
|
|
await this.buildingService.getBuildingParentByUuid(buildingUuid);
|
|
return building;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(AdminRoleGuard, CheckUserBuildingGuard)
|
|
@Post('user')
|
|
async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) {
|
|
try {
|
|
await this.buildingService.addUserBuilding(addUserBuildingDto);
|
|
return {
|
|
statusCode: HttpStatus.CREATED,
|
|
success: true,
|
|
message: 'user building added successfully',
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('user/:userUuid')
|
|
async getBuildingsByUserId(@Param('userUuid') userUuid: string) {
|
|
try {
|
|
return await this.buildingService.getBuildingsByUserId(userUuid);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
|
@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) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
}
|