import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common'; import { Response } from 'express'; import { ConfigLogsService } from './config-logs.service'; import ConfigLog from './config-logs.entity'; import { GenericResponse } from 'src/common/GenericResponse.model'; @Controller('config/logs') export class ConfigLogsController { constructor(private configService: ConfigLogsService) {} @Get("/all") async getAll(@Res() res: Response) { const response = await this.configService.findAll() || []; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Get(':id') async findById(@Param('id') id: number, @Res() res: Response) { if(!id) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_ID_REQ', stackTrace: 'Request' }, null); res.send(response); return; } const response = await this.configService.findByPk(id) || {}; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Post('/filter') async filter(@Body() config: ConfigLog, @Res() res: Response) { if(!config) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_ID_REQ', stackTrace: 'Request' }, null); res.send(response); return; } const response = await this.configService.filter(config) || {}; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Post() async insert(@Body() config: ConfigLog, @Res() res: Response) { if(!config) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_ID_REQ', stackTrace: 'Request' }, null); res.send(response); return; } delete config.id; const response = await this.configService.upsert(config, true); const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Put() async update(@Body() config: ConfigLog, @Res() res: Response) { if(!config || !config.id) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_ID_REQ', stackTrace: 'Request' }, null); res.send(response); return; } const response = await this.configService.upsert(config, false); const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Delete(':id') async deleteById(@Param('id') id: number, @Res() res: Response) { if(!id) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_ID_REQ', stackTrace: 'Request' }, null); res.send(response); return; } const response = await this.configService.remove(id) || {}; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } }