mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
refactor: update PowerClampController and service to use PowerClampParamsDto for parameter handling
This commit is contained in:
@ -6,6 +6,7 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { PowerClampService } from '../services/power-clamp.service';
|
||||
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { PowerClampParamsDto } from '../dto/power-clamp-params.dto';
|
||||
|
||||
@ApiTags('Power Clamp Module')
|
||||
@Controller({
|
||||
@ -23,12 +24,9 @@ export class PowerClampController {
|
||||
description: ControllerRoute.PowerClamp.ACTIONS.GET_ENERGY_DESCRIPTION,
|
||||
})
|
||||
async getPowerClampData(
|
||||
@Param('powerClampUuid') powerClampUuid: string,
|
||||
@Query() params: GetPowerClampDto,
|
||||
@Param() params: PowerClampParamsDto,
|
||||
@Query() query: GetPowerClampDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.powerClampService.getPowerClampData(
|
||||
powerClampUuid,
|
||||
params,
|
||||
);
|
||||
return await this.powerClampService.getPowerClampData(params, query);
|
||||
}
|
||||
}
|
||||
|
6
src/power-clamp/dto/power-clamp-params.dto.ts
Normal file
6
src/power-clamp/dto/power-clamp-params.dto.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class PowerClampParamsDto {
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
powerClampUuid: string;
|
||||
}
|
@ -7,6 +7,7 @@ import {
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [PowerClampController],
|
||||
@ -15,6 +16,7 @@ import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
DeviceRepository,
|
||||
],
|
||||
exports: [PowerClampService],
|
||||
})
|
||||
|
@ -6,6 +6,8 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { PowerClampParamsDto } from '../dto/power-clamp-params.dto';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class PowerClampService {
|
||||
@ -13,12 +15,24 @@ export class PowerClampService {
|
||||
private readonly powerClampDailyRepository: PowerClampDailyRepository,
|
||||
private readonly powerClampHourlyRepository: PowerClampHourlyRepository,
|
||||
private readonly powerClampMonthlyRepository: PowerClampMonthlyRepository,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
) {}
|
||||
|
||||
async getPowerClampData(powerClampUuid: string, params: GetPowerClampDto) {
|
||||
const { dayDate, monthDate, year } = params;
|
||||
|
||||
async getPowerClampData(
|
||||
params: PowerClampParamsDto,
|
||||
query: GetPowerClampDto,
|
||||
) {
|
||||
const { dayDate, monthDate, year } = query;
|
||||
const { powerClampUuid } = params;
|
||||
try {
|
||||
const device = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: powerClampUuid,
|
||||
},
|
||||
});
|
||||
if (!device) {
|
||||
throw new HttpException('Power clamp not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
if (dayDate) {
|
||||
const data = await this.powerClampHourlyRepository
|
||||
.createQueryBuilder('hourly')
|
||||
@ -72,8 +86,8 @@ export class PowerClampService {
|
||||
return this.buildResponse(`Power clamp data fetched successfully`, []);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching power clamp data',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
error.message || 'Error fetching power clamp data',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user