feat(device): add endpoint to add device to user

This commit is contained in:
faris Aljohari
2024-05-29 23:29:53 +03:00
parent 93bab79d31
commit 423533df04
9 changed files with 223 additions and 65 deletions

View File

@ -10,11 +10,13 @@ import {
HttpStatus,
UseGuards,
Req,
Put,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import {
AddDeviceDto,
AddDeviceInGroupDto,
AddDeviceInRoomDto,
UpdateDeviceInRoomDto,
} from '../dtos/add.device.dto';
import {
GetDeviceByGroupIdDto,
@ -26,6 +28,7 @@ import { CheckGroupGuard } from 'src/guards/group.guard';
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { CheckDeviceGuard } from 'src/guards/device.guard';
@ApiTags('Device Module')
@Controller({
@ -34,7 +37,26 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
})
export class DeviceController {
constructor(private readonly deviceService: DeviceService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckDeviceGuard)
@Post()
async addDevice(@Body() addDeviceDto: AddDeviceDto) {
try {
const device = await this.deviceService.addDevice(addDeviceDto);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'device added successfully',
data: device,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard)
@Get('room')
@ -58,16 +80,19 @@ export class DeviceController {
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard)
@Post('room')
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
@Put('room')
async updateDeviceInRoom(
@Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto,
) {
try {
const device =
await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
const device = await this.deviceService.updateDeviceInRoom(
updateDeviceInRoomDto,
);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'device added in room successfully',
message: 'device updated in room successfully',
data: device,
};
} catch (error) {