finished schedule module

This commit is contained in:
faris Aljohari
2024-09-15 18:03:30 +03:00
parent 0df4af175f
commit d962d4a7b3
11 changed files with 559 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
export * from './schedule.controller';
@@ -0,0 +1,116 @@
import { ScheduleService } from '../services/schedule.service';
import {
Body,
Controller,
Get,
Post,
Param,
HttpException,
HttpStatus,
UseGuards,
Put,
Delete,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AddScheduleDto, EnableScheduleDto } from '../dtos/add.schedule.dto';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
@ApiTags('Schedule Module')
@Controller({
version: '1',
path: 'schedule',
})
export class ScheduleController {
constructor(private readonly scheduleService: ScheduleService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post(':deviceUuid')
async addDeviceSchedule(
@Param('deviceUuid') deviceUuid: string,
@Body() addScheduleDto: AddScheduleDto,
) {
try {
const device = await this.scheduleService.addDeviceSchedule(
deviceUuid,
addScheduleDto,
);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'schedule added successfully',
data: device,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':deviceUuid/category/:category')
async getDeviceScheduleByCategory(
@Param('deviceUuid') deviceUuid: string,
@Param('category') category: string,
) {
try {
return await this.scheduleService.getDeviceScheduleByCategory(
deviceUuid,
category,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Delete(':deviceUuid/:scheduleId')
async deleteDeviceSchedule(
@Param('deviceUuid') deviceUuid: string,
@Param('scheduleId') scheduleId: string,
) {
try {
await this.scheduleService.deleteDeviceSchedule(deviceUuid, scheduleId);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'schedule deleted successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put('enable/:deviceUuid')
async enableDeviceSchedule(
@Param('deviceUuid') deviceUuid: string,
@Body() enableScheduleDto: EnableScheduleDto,
) {
try {
await this.scheduleService.enableDeviceSchedule(
deviceUuid,
enableScheduleDto,
);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'schedule updated successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}