Add BuildingController with CRUD operations

This commit is contained in:
faris Aljohari
2024-04-03 14:51:40 +03:00
parent 7cf26f3146
commit 499603f3c0
2 changed files with 105 additions and 0 deletions
@@ -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,
);
}
}
}
}
+1
View File
@@ -0,0 +1 @@
export * from './building.controller';