Refactor controllers to return UUID of added entities

This commit is contained in:
faris Aljohari
2024-05-01 10:34:07 +03:00
parent 229909f959
commit d146cce1bb
10 changed files with 21 additions and 15 deletions

View File

@ -32,8 +32,8 @@ export class BuildingController {
@Post()
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
try {
await this.buildingService.addBuilding(addBuildingDto);
return { message: 'Building added successfully' };
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',

View File

@ -38,11 +38,12 @@ export class BuildingService {
if (!spaceType) {
throw new BadRequestException('Invalid building UUID');
}
await this.spaceRepository.save({
const building = await this.spaceRepository.save({
spaceName: addBuildingDto.buildingName,
parent: { uuid: addBuildingDto.communityUuid },
spaceType: { uuid: spaceType.uuid },
});
return building;
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException

View File

@ -34,8 +34,9 @@ export class CommunityController {
@Post()
async addCommunity(@Body() addCommunityDto: AddCommunityDto) {
try {
await this.communityService.addCommunity(addCommunityDto);
return { message: 'Community added successfully' };
const community =
await this.communityService.addCommunity(addCommunityDto);
return { message: 'Community added successfully', uuid: community.uuid };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',

View File

@ -34,10 +34,11 @@ export class CommunityService {
},
});
await this.spaceRepository.save({
const community = await this.spaceRepository.save({
spaceName: addCommunityDto.communityName,
spaceType: { uuid: spaceType.uuid },
});
return community;
} catch (err) {
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
}

View File

@ -32,8 +32,8 @@ export class FloorController {
@Post()
async addFloor(@Body() addFloorDto: AddFloorDto) {
try {
await this.floorService.addFloor(addFloorDto);
return { message: 'Floor added successfully' };
const floor = await this.floorService.addFloor(addFloorDto);
return { message: 'Floor added successfully', uuid: floor.uuid };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',

View File

@ -35,11 +35,12 @@ export class FloorService {
},
});
await this.spaceRepository.save({
const floor = await this.spaceRepository.save({
spaceName: addFloorDto.floorName,
parent: { uuid: addFloorDto.buildingUuid },
spaceType: { uuid: spaceType.uuid },
});
return floor;
} catch (err) {
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
}

View File

@ -30,8 +30,8 @@ export class RoomController {
@Post()
async addRoom(@Body() addRoomDto: AddRoomDto) {
try {
await this.roomService.addRoom(addRoomDto);
return { message: 'Room added successfully' };
const room = await this.roomService.addRoom(addRoomDto);
return { message: 'Room added successfully', uuid: room.uuid };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',

View File

@ -32,11 +32,12 @@ export class RoomService {
},
});
await this.spaceRepository.save({
const room = await this.spaceRepository.save({
spaceName: addRoomDto.roomName,
parent: { uuid: addRoomDto.unitUuid },
spaceType: { uuid: spaceType.uuid },
});
return room;
} catch (err) {
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
}

View File

@ -32,8 +32,8 @@ export class UnitController {
@Post()
async addUnit(@Body() addUnitDto: AddUnitDto) {
try {
await this.unitService.addUnit(addUnitDto);
return { message: 'Unit added successfully' };
const unit = await this.unitService.addUnit(addUnitDto);
return { message: 'Unit added successfully', uuid: unit.uuid };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',

View File

@ -35,11 +35,12 @@ export class UnitService {
},
});
await this.spaceRepository.save({
const unit = await this.spaceRepository.save({
spaceName: addUnitDto.unitName,
parent: { uuid: addUnitDto.floorUuid },
spaceType: { uuid: spaceType.uuid },
});
return unit;
} catch (err) {
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
}