diff --git a/src/app-building/app-building.controller.ts b/src/app-building/app-building.controller.ts index 5024849..4465c5a 100644 --- a/src/app-building/app-building.controller.ts +++ b/src/app-building/app-building.controller.ts @@ -2,6 +2,7 @@ import { Body, ClassSerializerInterceptor, Controller, + Delete, Get, Param, ParseIntPipe, @@ -18,6 +19,7 @@ import { ApiBody, ApiNotFoundResponse, ApiParam, + ApiNoContentResponse, } from '@nestjs/swagger'; import { User } from 'src/objects/user/user.entity'; import { LoggedInUser } from 'src/shared/decorators/user.decorator'; @@ -200,4 +202,17 @@ export class AppBuildingController { ): Promise { 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 { + return this.service.deleteRoom(user, id, roomId); + } } diff --git a/src/app-building/app-building.service.ts b/src/app-building/app-building.service.ts index 8cef9e5..737154d 100644 --- a/src/app-building/app-building.service.ts +++ b/src/app-building/app-building.service.ts @@ -220,4 +220,20 @@ export class AppBuildingService { return room; } + + async deleteRoom(user: User, buildingId: number, roomId: number) { + const room = await this.buildingService.getRoomByBuildingAndUserSub( + buildingId, + roomId, + user.sub, + ); + + if (!room) { + throw new NotFoundException('Room not found'); + } + + await this.buildingService.deleteRoom(room); + + return room; + } } diff --git a/src/main.ts b/src/main.ts index f8d7c45..9e602fd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -40,6 +40,6 @@ async function bootstrap() { whitelist: true, }), ); - await app.listen(+configService.get('PORT')); + await app.listen(+configService.get('PORT'), configService.get('HOST')); } bootstrap(); diff --git a/src/objects/building/building.service.ts b/src/objects/building/building.service.ts index a0fc139..079e6d4 100644 --- a/src/objects/building/building.service.ts +++ b/src/objects/building/building.service.ts @@ -200,4 +200,8 @@ export class BuildingService { Object.assign(newRoom, data); return this.roomRepository.save(newRoom); } + + async deleteRoom(room: Room) { + return this.roomRepository.remove(room); + } }