import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common'; import { QuestionTagService } from './question-tag.service'; import { Response } from 'express'; import { GenericResponse } from '../common/GenericResponse.model'; import QuestionTag from './question-tag.entity'; @Controller('question-tag') export class QuestionTagController { constructor(private questionTagService: QuestionTagService) {} @Get("/all") async getAllQuestionTags(@Res() res: Response) { const response = await this.questionTagService.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.questionTagService.findByPk(id) || {}; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Post('/filter') async filter(@Body() questionTag: QuestionTag, @Res() res: Response) { if(!questionTag) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_ID_REQ', stackTrace: 'Request' }, null); res.send(response); return; } const response = await this.questionTagService.filter(questionTag) || []; const httpResponse = new GenericResponse(null, response) res.status(200).send(httpResponse); } @Post() async insert(@Body() questionTag: QuestionTag, @Res() res: Response) { if(!questionTag) { const response = new GenericResponse({ exception: true, exceptionSeverity: 'HIGH', exceptionMessage: 'ERR.NO_ID_REQ', stackTrace: 'Request' }, null); res.send(response); return; } delete questionTag.id; const response = await this.questionTagService.upsert(questionTag, true); const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } @Put() async update(@Body() questionTag: QuestionTag, @Res() res: Response) { if(!questionTag || !questionTag.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.questionTagService.upsert(questionTag, 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.questionTagService.remove(id) || {}; const httpResponse = new GenericResponse(null, response) res.send(httpResponse); } }