import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common'; import { Response } from 'express'; import { GenericResponse } from 'src/common/GenericResponse.model'; import Plan from './plans.entity'; import { PlansService } from './plans.service'; @Controller('plans') export class PlansController { constructor(private planService?: PlansService) { if(!planService) { planService = new PlansService(); } } @Get("/all") async plangetAll(@Res() res: Response) { const response = await this.planService.findAll() || []; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Get(':id') async planFindById(@Param('id') id: number, @Res() res: Response) { if(!id) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_ID_IN_REQ', stackTrace: 'Request' }, null); res.send(response); return; } const response = await this.planService.findByPk(id) || {}; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Post('/filter') async planFilter(@Body() user: Plan, @Res() res: Response) { if(!user) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_REQ', stackTrace: 'Request' }, null); res.send(response); return; } const response = await this.planService.filter(user) || []; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Post() async planInsert(@Body() user: Plan, @Res() res: Response) { if(!user) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_REQ', stackTrace: 'Request' }, null); res.send(response); return; } delete user.id; const response = await this.planService.upsert(user, true); const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Put() async planUpdate(@Body() user: Plan, @Res() res: Response) { if(!user || !user.id) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_REQ', stackTrace: 'Request' }, null); res.send(response); return; } const response = await this.planService.upsert(user, false); const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Delete(':id') async planDeleteById(@Param('id') id: number, @Res() res: Response) { if(!id) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_REQ', stackTrace: 'Request' }, null); res.send(response); return; } const response = await this.planService.remove(id) || {}; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } }