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 { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; 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'; @ApiTags('Building Module') @Controller({ version: '1', path: 'building', }) export class BuildingController { constructor(private readonly buildingService: BuildingService) {} @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckCommunityTypeGuard) @Post() async addBuilding(@Body() addBuildingDto: AddBuildingDto) { try { const building = await this.buildingService.addBuilding(addBuildingDto); return { message: 'Building added successfully', uuid: building.uuid }; } catch (error) { throw new HttpException( error.message || 'Internal server error', error.status || 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) { throw new HttpException( error.message || 'Internal server error', error.status || 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) { throw new HttpException( error.message || 'Internal server error', error.status || 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) { throw new HttpException( error.message || 'Internal server error', error.status || HttpStatus.INTERNAL_SERVER_ERROR, ); } } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckUserBuildingGuard) @Post('user') async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) { try { await this.buildingService.addUserBuilding(addUserBuildingDto); return { 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) @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, ); } } }