be
This commit is contained in:
@@ -1,24 +1,108 @@
|
||||
import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Request,
|
||||
Post,
|
||||
UseGuards,
|
||||
Get,
|
||||
ParseIntPipe,
|
||||
Param,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { SensorsService } from './sensors.service';
|
||||
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
|
||||
import { Sensor } from './entities/sensor.entity';
|
||||
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';
|
||||
|
||||
@ApiTags('센서')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('sensors')
|
||||
export class SensorsController {
|
||||
constructor(private readonly sensorService: SensorsService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: '센서 목록 조회' })
|
||||
@ApiOkResponse({ description: '센서 목록', type: [Sensor] })
|
||||
async findAll(): Promise<Sensor[]> {
|
||||
return this.findAll();
|
||||
//#region Group
|
||||
@Post('sensor-groups')
|
||||
@ApiOperation({ summary: '센서 그룹 생성' })
|
||||
@ApiOkResponse({ description: '성공', type: SensorGroupResponseDto })
|
||||
async createGroup(
|
||||
@Request() req: AuthRequest,
|
||||
@Body() dto: CreateSensorGroupDto,
|
||||
): Promise<SensorGroupResponseDto> {
|
||||
return this.sensorService.createGroup(req.user.userId, dto);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: '특정 센서 조회' })
|
||||
@ApiOkResponse({ description: '센서', type: [Sensor] })
|
||||
async findOne(@Param('id', ParseIntPipe) id: number): Promise<Sensor | null> {
|
||||
return this.findOne(id);
|
||||
@Get('sensor-groups')
|
||||
@ApiOperation({ summary: '센서 그룹 목록' })
|
||||
@ApiOkResponse({ description: '성공', type: [SensorGroupResponseDto] })
|
||||
async myGroup(@Request() req: AuthRequest): Promise<SensorGroupResponseDto[]> {
|
||||
return this.sensorService.findMyGroups(req.user.userId);
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//#region Sensor
|
||||
@Post('sensors')
|
||||
@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')
|
||||
@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
|
||||
}
|
||||
|
Reference in New Issue
Block a user