You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tryFullStack/backend/src/sensors/sensors.controller.ts

112 lines
4.0 KiB

1 month ago
import {
Body,
Controller,
Request,
Post,
UseGuards,
Get,
ParseIntPipe,
Param,
Query,
1 month ago
HttpCode,
1 month ago
} from '@nestjs/common';
1 month ago
import { SensorsService } from './sensors.service';
1 month ago
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
import { SensorGroupResponseDto } from './dto/sensor-group-response.dto';
import { AuthRequest } from 'src/common/interfaces/auth-request.interface';
import { CreateSensorGroupDto } from './dto/create-sensor-group.dto';
import { SensorResponseDto } from './dto/sensor-response.dto';
import { CreateSensorDto } from './dto/create-sensor.dto';
import { SensorDataResponseDto } from './dto/sensor-data-response.dto';
import { CreateSensorDataDto } from './dto/create-sensor-data.dto';
1 month ago
@ApiTags('센서')
1 month ago
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
1 month ago
@Controller('sensors')
export class SensorsController {
constructor(private readonly sensorService: SensorsService) {}
1 month ago
//#region Group
@Post('sensor-groups')
1 month ago
@HttpCode(200)
1 month ago
@ApiOperation({ summary: '센서 그룹 생성' })
@ApiOkResponse({ description: '성공', type: SensorGroupResponseDto })
async createGroup(
@Request() req: AuthRequest,
@Body() dto: CreateSensorGroupDto,
): Promise<SensorGroupResponseDto> {
return this.sensorService.createGroup(req.user.userId, dto);
1 month ago
}
1 month ago
@Get('sensor-groups')
@ApiOperation({ summary: '센서 그룹 목록' })
@ApiOkResponse({ description: '성공', type: [SensorGroupResponseDto] })
async myGroup(@Request() req: AuthRequest): Promise<SensorGroupResponseDto[]> {
return this.sensorService.findMyGroups(req.user.userId);
1 month ago
}
1 month ago
//#endregion
//#region Sensor
@Post('sensors')
1 month ago
@HttpCode(200)
1 month ago
@ApiOperation({ summary: '센서 생성' })
@ApiOkResponse({ description: '성공', type: SensorResponseDto })
async createSensor(
@Request() req: AuthRequest,
@Body() dto: CreateSensorDto,
): Promise<SensorResponseDto> {
return this.sensorService.createSensor(req.user.userId, dto);
}
@Get('sensor-groups/:groupId/sensors')
@ApiOperation({ summary: '센서 목록' })
@ApiOkResponse({ description: '성공', type: [SensorResponseDto] })
async sensorsInGroup(
@Request() req: AuthRequest,
@Param('groupId', ParseIntPipe) groupId: number,
): Promise<SensorResponseDto[]> {
return this.sensorService.findSensorsInGroup(req.user.userId, groupId);
}
//#endregion
//#region Data
@Post('sensors/:sensorId/data')
1 month ago
@HttpCode(200)
1 month ago
@ApiOperation({ summary: '센서 데이터 생성' })
@ApiOkResponse({ description: '성공', type: SensorDataResponseDto })
async createData(
@Request() req: AuthRequest,
@Param('sensorId', ParseIntPipe) sensorId: number,
@Body() dto: CreateSensorDataDto,
): Promise<SensorDataResponseDto> {
return this.sensorService.createSensorData(req.user.userId, sensorId, dto);
}
@Get('sensors/:sensorId/data')
@ApiOperation({ summary: '센서 데이터 조회' })
@ApiQuery({ name: 'from', required: false, description: 'ISO날짜' })
@ApiQuery({ name: 'to', required: false, description: 'ISO날짜' })
@ApiQuery({ name: 'limit', required: false, description: '조회 개수 (기본50, 최대 500)' })
@ApiOkResponse({ description: '성공', type: [SensorDataResponseDto] })
async getData(
@Request() req: AuthRequest,
@Param('sensorId', ParseIntPipe) sensorId: number,
@Query('from') from?: string,
@Query('to') to?: string,
@Query('limit') limit?: string,
): Promise<SensorDataResponseDto[]> {
const parsedFrom = from ? new Date(from) : undefined;
const parsedTo = to ? new Date(to) : undefined;
const parsedLimit = limit ? Number(limit) : undefined;
return this.sensorService.findSensorData(req.user.userId, sensorId, {
from: parsedFrom,
to: parsedTo,
limit: parsedLimit,
});
}
//#endregion
1 month ago
}