From 499603f3c0bc7f8e7e71fa33e8cbff2a02960701 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 3 Apr 2024 14:51:40 +0300 Subject: [PATCH] Add BuildingController with CRUD operations --- .../controllers/building.controller.ts | 104 ++++++++++++++++++ src/building/controllers/index.ts | 1 + 2 files changed, 105 insertions(+) create mode 100644 src/building/controllers/building.controller.ts create mode 100644 src/building/controllers/index.ts diff --git a/src/building/controllers/building.controller.ts b/src/building/controllers/building.controller.ts new file mode 100644 index 0000000..17e01d4 --- /dev/null +++ b/src/building/controllers/building.controller.ts @@ -0,0 +1,104 @@ +import { BuildingService } from '../services/building.service'; +import { + Body, + Controller, + Get, + HttpException, + HttpStatus, + Param, + Post, + Query, + UseGuards, +} from '@nestjs/common'; +import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; +import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; +import { AddBuildingDto } from '../dtos/add.building.dto'; +import { GetBuildingChildDto } from '../dtos/get.building.dto'; + +@ApiTags('Building Module') +@Controller({ + version: '1', + path: 'building', +}) +export class BuildingController { + constructor(private readonly buildingService: BuildingService) {} + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Post() + async addBuilding(@Body() addBuildingDto: AddBuildingDto) { + try { + await this.buildingService.addBuilding(addBuildingDto); + return { message: 'Building added successfully' }; + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get(':buildingUuid') + async getBuildingByUuid(@Param('buildingUuid') buildingUuid: string) { + try { + const building = + await this.buildingService.getBuildingByUuid(buildingUuid); + 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', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + } + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @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) { + if (error.status === 404) { + throw new HttpException('Building not found', HttpStatus.NOT_FOUND); + } else { + throw new HttpException( + error.message || 'Internal server error', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get('parent/:buildingUuid') + async getBuildingParentByUuid(@Param('buildingUuid') buildingUuid: string) { + try { + const building = + await this.buildingService.getBuildingParentByUuid(buildingUuid); + 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', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + } +} diff --git a/src/building/controllers/index.ts b/src/building/controllers/index.ts new file mode 100644 index 0000000..b5ec3c2 --- /dev/null +++ b/src/building/controllers/index.ts @@ -0,0 +1 @@ +export * from './building.controller';