265 lines
9.2 KiB
TypeScript
265 lines
9.2 KiB
TypeScript
import {
|
|
Body,
|
|
ClassSerializerInterceptor,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Param,
|
|
ParseIntPipe,
|
|
Patch,
|
|
Post,
|
|
Query,
|
|
UseGuards,
|
|
UseInterceptors,
|
|
} from '@nestjs/common';
|
|
import {
|
|
ApiTags,
|
|
ApiSecurity,
|
|
ApiOperation,
|
|
ApiOkResponse,
|
|
ApiBody,
|
|
ApiNotFoundResponse,
|
|
ApiParam,
|
|
ApiNoContentResponse,
|
|
ApiBadRequestResponse,
|
|
} from '@nestjs/swagger';
|
|
import { User } from 'src/objects/user/user.entity';
|
|
import { LoggedInUser } from 'src/shared/decorators/user.decorator';
|
|
import { AuthGuard } from 'src/shared/guards/auth.guard';
|
|
import { AppBuildingService } from './app-building.service';
|
|
import {
|
|
BuildingFloorUpdateRequestDto,
|
|
CreateFloorRequestDto,
|
|
} from './dto/buildings-floor-request.dto';
|
|
import { CreateBuildingRequestDto } from './dto/buildings-create-request.dto';
|
|
import {
|
|
BuildingFloorResponseDto,
|
|
BuildingRoomResponseDto,
|
|
BuildingRoomWithFloorResponseDto,
|
|
BuildingsListResponseDto,
|
|
BuildingsResponseDto,
|
|
} from './dto/buildings-response.dto';
|
|
import { BuildingsUpdateRequestDto } from './dto/buildings-update-request.dto';
|
|
import {
|
|
BuildingsCreateRoomRequestDto,
|
|
BuildingsUpdateRoomRequestDto,
|
|
} from './dto/buildings-create-room-request.dto';
|
|
import { BuildingSearchRequestDto } from './dto/buildings-search-request.dto';
|
|
import { BuildingSearchResponseDto } from './dto/building-search-response.dto';
|
|
|
|
@Controller({
|
|
path: 'buildings',
|
|
})
|
|
@ApiTags('buildings')
|
|
@ApiSecurity('Bearer token')
|
|
@UseInterceptors(ClassSerializerInterceptor)
|
|
@UseGuards(AuthGuard)
|
|
export class AppBuildingController {
|
|
constructor(private readonly service: AppBuildingService) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: 'Get buildings' })
|
|
@ApiOkResponse({ isArray: true, type: BuildingsListResponseDto })
|
|
async getBuildings(
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingsListResponseDto[]> {
|
|
return this.service.getUserBuildings(user);
|
|
}
|
|
|
|
@Post()
|
|
@ApiOperation({ summary: 'Create building' })
|
|
@ApiNotFoundResponse({ description: 'Invalid group ID' })
|
|
@ApiBody({ type: CreateBuildingRequestDto })
|
|
@ApiOkResponse({ type: BuildingsResponseDto })
|
|
async createBuilding(
|
|
@LoggedInUser() user: User,
|
|
@Body() body: CreateBuildingRequestDto,
|
|
): Promise<BuildingsResponseDto> {
|
|
return this.service.createBuilding(user, body);
|
|
}
|
|
|
|
@Get('search')
|
|
@ApiOperation({ summary: 'Search for buildings' })
|
|
@ApiOkResponse({ isArray: true, type: BuildingSearchResponseDto })
|
|
async searchForBuildings(
|
|
@LoggedInUser() user: User,
|
|
@Query() query: BuildingSearchRequestDto,
|
|
): Promise<BuildingSearchResponseDto[]> {
|
|
return this.service.searchForBuilding(query, user);
|
|
}
|
|
|
|
@Get(':id')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiOperation({ summary: 'Get building by ID' })
|
|
@ApiNotFoundResponse({ description: 'Building not found' })
|
|
@ApiOkResponse({ type: BuildingsResponseDto })
|
|
async getBuildingById(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingsResponseDto> {
|
|
return this.service.getBuildingById(user, id);
|
|
}
|
|
|
|
@Patch(':id')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiOperation({ summary: 'Update building by ID' })
|
|
@ApiBody({ type: BuildingsUpdateRequestDto })
|
|
@ApiNotFoundResponse({ description: 'Building not found' })
|
|
@ApiOkResponse({ type: BuildingsListResponseDto })
|
|
async updateBuildingById(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Body() body: BuildingsUpdateRequestDto,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingsListResponseDto> {
|
|
return this.service.updateBuilding(user, id, body);
|
|
}
|
|
|
|
@Get(':id/floors')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiOperation({ summary: 'Get building floors' })
|
|
@ApiOkResponse({ type: BuildingFloorResponseDto, isArray: true })
|
|
async getBuildingFloorsById(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingFloorResponseDto[]> {
|
|
return this.service.getBuildingFloors(user, id);
|
|
}
|
|
|
|
@Post(':id/floors')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiBody({ type: CreateFloorRequestDto })
|
|
@ApiOperation({ summary: 'Create building floors' })
|
|
@ApiOkResponse({ type: BuildingFloorResponseDto, isArray: true })
|
|
async createBuildingFloor(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Body() body: CreateFloorRequestDto,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingFloorResponseDto> {
|
|
return this.service.createBuildingFloor(user, id, body);
|
|
}
|
|
|
|
@Get(':id/floors/:number')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiParam({ name: 'number', description: 'Floor number' })
|
|
@ApiOperation({ summary: 'Get building floor by floor number' })
|
|
@ApiOkResponse({ type: BuildingFloorResponseDto })
|
|
async getBuildingFloorByIdByNumber(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Param('number', ParseIntPipe) floor: number,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingFloorResponseDto> {
|
|
return this.service.getBuildingFloor(user, id, floor);
|
|
}
|
|
|
|
@Patch(':id/floors/:number')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiParam({ name: 'number', description: 'Floor number' })
|
|
@ApiBody({ type: BuildingFloorUpdateRequestDto })
|
|
@ApiOperation({ summary: 'Update building floor by floor number' })
|
|
@ApiOkResponse({ type: BuildingFloorResponseDto })
|
|
async updateBuildingFloorByIdByNumber(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Param('number', ParseIntPipe) floor: number,
|
|
@Body() body: BuildingFloorUpdateRequestDto,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingFloorResponseDto> {
|
|
return this.service.updateBuildingFloor(user, body, id, floor);
|
|
}
|
|
|
|
@Delete(':id/floors/:number')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiParam({ name: 'number', description: 'Floor number' })
|
|
@ApiOperation({ summary: 'Delete building floor by floor number' })
|
|
@ApiBadRequestResponse({
|
|
description: 'A floor cannot be deleted if it has defined rooms',
|
|
})
|
|
@ApiOkResponse({ type: BuildingFloorResponseDto })
|
|
async deleteBuildingFloorByIdByNumber(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Param('number', ParseIntPipe) floor: number,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingFloorResponseDto> {
|
|
return this.service.deleteBuildingFloor(user, id, floor);
|
|
}
|
|
|
|
@Post(':id/floors/:number/rooms')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiParam({ name: 'number', description: 'Floor number' })
|
|
@ApiBody({ type: BuildingsCreateRoomRequestDto })
|
|
@ApiOperation({ summary: 'Create new room on floor' })
|
|
@ApiOkResponse({ type: BuildingRoomResponseDto })
|
|
async createNewRoom(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Param('number', ParseIntPipe) floor: number,
|
|
@Body() body: BuildingsCreateRoomRequestDto,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingRoomResponseDto> {
|
|
return this.service.addNewRoom(user, body, id, floor);
|
|
}
|
|
|
|
@Get(':id/floors/:number/rooms')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiParam({ name: 'number', description: 'Floor number' })
|
|
@ApiOperation({ summary: 'Get rooms on floor' })
|
|
@ApiOkResponse({ type: BuildingRoomResponseDto, isArray: true })
|
|
async getRoomsOnFloor(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Param('number', ParseIntPipe) floor: number,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingRoomResponseDto[]> {
|
|
return this.service.getRoomsOnFloor(user, id, floor);
|
|
}
|
|
|
|
@Get(':id/rooms')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiOperation({ summary: 'Get rooms in building' })
|
|
@ApiOkResponse({ type: BuildingRoomWithFloorResponseDto, isArray: true })
|
|
async getRooms(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingRoomWithFloorResponseDto[]> {
|
|
return this.service.getRoomsInBuilding(user, id);
|
|
}
|
|
|
|
@Get(':id/rooms/:roomId')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiParam({ name: 'roomId', description: 'Room ID' })
|
|
@ApiOperation({ summary: 'Get room by ID' })
|
|
@ApiOkResponse({ type: BuildingRoomResponseDto })
|
|
async getRoomById(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Param('roomId', ParseIntPipe) roomId: number,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingRoomResponseDto> {
|
|
return this.service.getRoom(user, id, roomId);
|
|
}
|
|
|
|
@Patch(':id/rooms/:roomId')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiParam({ name: 'roomId', description: 'Room ID' })
|
|
@ApiBody({ type: BuildingsUpdateRoomRequestDto })
|
|
@ApiOperation({ summary: 'Update room by ID' })
|
|
@ApiOkResponse({ type: BuildingRoomResponseDto })
|
|
async updateRoomById(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Param('roomId', ParseIntPipe) roomId: number,
|
|
@Body() body: BuildingsUpdateRoomRequestDto,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingRoomResponseDto> {
|
|
return this.service.updateRoom(user, body, id, roomId);
|
|
}
|
|
|
|
@Delete(':id/rooms/:roomId')
|
|
@ApiParam({ name: 'id', description: 'Building ID' })
|
|
@ApiParam({ name: 'roomId', description: 'Room ID' })
|
|
@ApiOperation({ summary: 'Delete room by ID' })
|
|
@ApiNoContentResponse()
|
|
async deleteRoomById(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Param('roomId', ParseIntPipe) roomId: number,
|
|
@LoggedInUser() user: User,
|
|
): Promise<BuildingRoomResponseDto> {
|
|
return this.service.deleteRoom(user, id, roomId);
|
|
}
|
|
}
|