feat: Add success response with data in controllers

This commit is contained in:
faris Aljohari
2024-05-19 14:25:19 +03:00
parent 25c3765d9c
commit 835fde8304
6 changed files with 64 additions and 11 deletions

View File

@ -34,7 +34,12 @@ export class BuildingController {
async addBuilding(@Body() addBuildingDto: AddBuildingDto) { async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
try { try {
const building = await this.buildingService.addBuilding(addBuildingDto); const building = await this.buildingService.addBuilding(addBuildingDto);
return { message: 'Building added successfully', uuid: building.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Building added successfully',
data: building,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -100,7 +105,11 @@ export class BuildingController {
async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) { async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) {
try { try {
await this.buildingService.addUserBuilding(addUserBuildingDto); await this.buildingService.addUserBuilding(addUserBuildingDto);
return { message: 'user building added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user building added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -37,7 +37,12 @@ export class CommunityController {
try { try {
const community = const community =
await this.communityService.addCommunity(addCommunityDto); await this.communityService.addCommunity(addCommunityDto);
return { message: 'Community added successfully', uuid: community.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Community added successfully',
data: community,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -102,7 +107,11 @@ export class CommunityController {
async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) { async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) {
try { try {
await this.communityService.addUserCommunity(addUserCommunityDto); await this.communityService.addUserCommunity(addUserCommunityDto);
return { message: 'user community added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user community added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -61,7 +61,15 @@ export class DeviceController {
@Post('room') @Post('room')
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) { async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
try { try {
return await this.deviceService.addDeviceInRoom(addDeviceInRoomDto); const device =
await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'device added in room successfully',
data: device,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -34,7 +34,12 @@ export class FloorController {
async addFloor(@Body() addFloorDto: AddFloorDto) { async addFloor(@Body() addFloorDto: AddFloorDto) {
try { try {
const floor = await this.floorService.addFloor(addFloorDto); const floor = await this.floorService.addFloor(addFloorDto);
return { message: 'Floor added successfully', uuid: floor.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Floor added successfully',
data: floor,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -99,7 +104,11 @@ export class FloorController {
async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) { async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) {
try { try {
await this.floorService.addUserFloor(addUserFloorDto); await this.floorService.addUserFloor(addUserFloorDto);
return { message: 'user floor added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user floor added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -32,7 +32,12 @@ export class RoomController {
async addRoom(@Body() addRoomDto: AddRoomDto) { async addRoom(@Body() addRoomDto: AddRoomDto) {
try { try {
const room = await this.roomService.addRoom(addRoomDto); const room = await this.roomService.addRoom(addRoomDto);
return { message: 'Room added successfully', uuid: room.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Room added successfully',
data: room,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -76,7 +81,11 @@ export class RoomController {
async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) { async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) {
try { try {
await this.roomService.addUserRoom(addUserRoomDto); await this.roomService.addUserRoom(addUserRoomDto);
return { message: 'user room added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user room added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -34,7 +34,12 @@ export class UnitController {
async addUnit(@Body() addUnitDto: AddUnitDto) { async addUnit(@Body() addUnitDto: AddUnitDto) {
try { try {
const unit = await this.unitService.addUnit(addUnitDto); const unit = await this.unitService.addUnit(addUnitDto);
return { message: 'Unit added successfully', uuid: unit.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Unit added successfully',
data: unit,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -95,7 +100,11 @@ export class UnitController {
async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) { async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) {
try { try {
await this.unitService.addUserUnit(addUserUnitDto); await this.unitService.addUserUnit(addUserUnitDto);
return { message: 'user unit added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user unit added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',