321 lines
10 KiB
TypeScript
321 lines
10 KiB
TypeScript
import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common';
|
|
import { QuestionService } from './question.service';
|
|
import { Response } from 'express';
|
|
import { GenericResponse } from '../common/GenericResponse.model';
|
|
import Question from './question.entity';
|
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
|
|
|
|
@ApiTags('question')
|
|
@Controller('question')
|
|
export class QuestionController {
|
|
constructor(private questionService: QuestionService) {}
|
|
|
|
@Get("/all")
|
|
@ApiOperation({ summary: 'Get all questions' })
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully retrieved all questions',
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'No questions found',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "Questions not found"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
async getAllQuestions(@Res() res: Response) {
|
|
const response = await this.questionService.findAll() || [];
|
|
if (!response) {
|
|
const httpResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: 'Questions not found'
|
|
}, null);
|
|
res.status(404).send(httpResponse);
|
|
return;
|
|
}
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(200).send(httpResponse);
|
|
}
|
|
|
|
@Get(':id')
|
|
@ApiOperation({ summary: 'Get a question by ID' })
|
|
@ApiParam({ name: 'id', type: Number, description: 'ID of the question' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'ID is required',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NO_ID_REQ",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'Question not found',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "Question not found"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully retrieved question',
|
|
})
|
|
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);
|
|
return res.status(400).send(response);
|
|
}
|
|
|
|
const response = await this.questionService.findByPk(id) || {};
|
|
if (!response) {
|
|
const errorResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: `Question with ID ${id} not found`
|
|
}, null);
|
|
return res.status(404).send(errorResponse);
|
|
}
|
|
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(200).send(httpResponse);
|
|
}
|
|
|
|
@Post('/filter')
|
|
@ApiOperation({ summary: 'Filter questions based on criteria' })
|
|
@ApiBody({ type: Question, description: 'Criteria to filter questions' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'Invalid filter criteria',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.INVALID_CRITERIA",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'No questions found matching the filter criteria',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "No questions found based on the filter criteria"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully filtered questions',
|
|
})
|
|
async filter(@Body() question: Question, @Res() res: Response) {
|
|
if (!question) {
|
|
const response = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.INVALID_CRITERIA',
|
|
stackTrace: 'Request'
|
|
}, null);
|
|
return res.status(400).send(response);
|
|
}
|
|
|
|
const response = await this.questionService.filter(question) || [];
|
|
if (!response) {
|
|
const errorResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: 'No questions found matching the filter criteria'
|
|
}, null);
|
|
return res.status(404).send(errorResponse);
|
|
}
|
|
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(200).send(httpResponse);
|
|
}
|
|
|
|
@Post()
|
|
@ApiOperation({ summary: 'Create a new question' })
|
|
@ApiBody({ type: Question, description: 'Question data to create' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'Invalid question data',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.INVALID_DATA",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 201,
|
|
description: 'Successfully created question',
|
|
})
|
|
async insert(@Body() question: Question, @Res() res: Response) {
|
|
if (!question) {
|
|
const response = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.INVALID_DATA',
|
|
stackTrace: 'Request'
|
|
}, null);
|
|
return res.status(400).send(response);
|
|
}
|
|
|
|
delete question.id;
|
|
const response = await this.questionService.upsert(question, true);
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(201).send(httpResponse);
|
|
}
|
|
|
|
@Put()
|
|
@ApiOperation({ summary: 'Update an existing question' })
|
|
@ApiBody({ type: Question, description: 'Question data to update' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'Invalid data or missing ID',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.INVALID_DATA",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'Question not found',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "Question not found"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully updated question',
|
|
})
|
|
async update(@Body() question: Question, @Res() res: Response) {
|
|
if (!question || !question.id) {
|
|
const response = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NO_ID_REQ',
|
|
stackTrace: 'Request'
|
|
}, null);
|
|
return res.status(400).send(response);
|
|
}
|
|
|
|
const response = await this.questionService.upsert(question, false);
|
|
if (!response) {
|
|
const errorResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: `Question with ID ${question.id} not found`
|
|
}, null);
|
|
return res.status(404).send(errorResponse);
|
|
}
|
|
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(200).send(httpResponse);
|
|
}
|
|
|
|
@Delete(':id')
|
|
@ApiOperation({ summary: 'Delete a question by ID' })
|
|
@ApiParam({ name: 'id', type: Number, description: 'ID of the question to delete' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'ID is required',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NO_ID_REQ",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'Question not found',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "Question not found"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully deleted question',
|
|
})
|
|
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);
|
|
return res.status(400).send(response);
|
|
}
|
|
|
|
const response = await this.questionService.remove(id) || {};
|
|
if (!response) {
|
|
const errorResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: `Question with ID ${id} not found`
|
|
}, null);
|
|
return res.status(404).send(errorResponse);
|
|
}
|
|
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(200).send(httpResponse);
|
|
}
|
|
}
|