Refactor Device Module

This commit is contained in:
faris Aljohari
2024-04-27 23:27:40 +03:00
parent 165dfb575d
commit 8c790b0d96
14 changed files with 527 additions and 478 deletions

View File

@ -1,5 +1,15 @@
import { DeviceService } from '../services/device.service';
import { Body, Controller, Get, Post, Query, Param } from '@nestjs/common';
import {
Body,
Controller,
Get,
Post,
Query,
Param,
HttpException,
HttpStatus,
UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import {
AddDeviceInGroupDto,
@ -7,11 +17,12 @@ import {
} from '../dtos/add.device.dto';
import {
GetDeviceByGroupIdDto,
GetDeviceByRoomIdDto,
GetDeviceByRoomUuidDto,
} from '../dtos/get.device.dto';
import { ControlDeviceDto } from '../dtos/control.device.dto';
import { AuthGuardWithRoles } from 'src/guards/device.permission.guard';
import { PermissionType } from '@app/common/constants/permission-type.enum';
import { CheckRoomGuard } from 'src/guards/room.guard';
import { CheckGroupGuard } from 'src/guards/group.guard';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
@ApiTags('Device Module')
@Controller({
@ -22,19 +33,38 @@ export class DeviceController {
constructor(private readonly deviceService: DeviceService) {}
@ApiBearerAuth()
@AuthGuardWithRoles(PermissionType.READ)
@UseGuards(JwtAuthGuard, CheckRoomGuard)
@Get('room')
async getDevicesByRoomId(
@Query() getDeviceByRoomIdDto: GetDeviceByRoomIdDto,
@Query() getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
) {
try {
return await this.deviceService.getDevicesByRoomId(getDeviceByRoomIdDto);
} catch (err) {
throw new Error(err);
return await this.deviceService.getDevicesByRoomId(
getDeviceByRoomUuidDto,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard)
@Post('room')
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
try {
return await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@AuthGuardWithRoles(PermissionType.READ)
@UseGuards(JwtAuthGuard, CheckGroupGuard)
@Get('group')
async getDevicesByGroupId(
@Query() getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
@ -43,68 +73,81 @@ export class DeviceController {
return await this.deviceService.getDevicesByGroupId(
getDeviceByGroupIdDto,
);
} catch (err) {
throw new Error(err);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@AuthGuardWithRoles(PermissionType.READ)
@Get(':deviceId')
async getDeviceDetailsByDeviceId(@Param('deviceId') deviceId: string) {
try {
return await this.deviceService.getDeviceDetailsByDeviceId(deviceId);
} catch (err) {
throw new Error(err);
}
}
@ApiBearerAuth()
@AuthGuardWithRoles(PermissionType.READ)
@Get(':deviceId/functions')
async getDeviceInstructionByDeviceId(@Param('deviceId') deviceId: string) {
try {
return await this.deviceService.getDeviceInstructionByDeviceId(deviceId);
} catch (err) {
throw new Error(err);
}
}
@ApiBearerAuth()
@AuthGuardWithRoles(PermissionType.READ)
@Get(':deviceId/functions/status')
async getDevicesInstructionStatus(@Param('deviceId') deviceId: string) {
try {
return await this.deviceService.getDevicesInstructionStatus(deviceId);
} catch (err) {
throw new Error(err);
}
}
@ApiBearerAuth()
@AuthGuardWithRoles(PermissionType.CONTROLLABLE)
@Post('room')
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
try {
return await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
} catch (err) {
throw new Error(err);
}
}
@ApiBearerAuth()
@AuthGuardWithRoles(PermissionType.CONTROLLABLE)
@UseGuards(JwtAuthGuard, CheckGroupGuard)
@Post('group')
async addDeviceInGroup(@Body() addDeviceInGroupDto: AddDeviceInGroupDto) {
try {
return await this.deviceService.addDeviceInGroup(addDeviceInGroupDto);
} catch (err) {
throw new Error(err);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@AuthGuardWithRoles(PermissionType.CONTROLLABLE)
@UseGuards(JwtAuthGuard)
@Get(':deviceUuid')
async getDeviceDetailsByDeviceId(@Param('deviceUuid') deviceUuid: string) {
try {
return await this.deviceService.getDeviceDetailsByDeviceId(deviceUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':deviceUuid/functions')
async getDeviceInstructionByDeviceId(
@Param('deviceUuid') deviceUuid: string,
) {
try {
return await this.deviceService.getDeviceInstructionByDeviceId(
deviceUuid,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':deviceUuid/functions/status')
async getDevicesInstructionStatus(@Param('deviceUuid') deviceUuid: string) {
try {
return await this.deviceService.getDevicesInstructionStatus(deviceUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post('control')
async controlDevice(@Body() controlDeviceDto: ControlDeviceDto) {
try {
return await this.deviceService.controlDevice(controlDeviceDto);
} catch (err) {
throw new Error(err);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}