This commit is contained in:
Your Name
2025-04-02 11:50:08 -06:00
parent bf7c0e93a8
commit a1f52daa8a
26 changed files with 1109 additions and 42 deletions
+36 -1
View File
@@ -1 +1,36 @@
export class CreatePreguntaDto {}
import { Type } from 'class-transformer';
import { IsArray, IsBoolean, IsNotEmpty, IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator';
export class OpcionDto {
@IsNotEmpty()
@IsString()
valor: string;
}
export class CreatePreguntaDto {
@IsNotEmpty()
@IsString()
titulo: string;
@IsOptional()
@IsBoolean()
obligatoria?: boolean;
@IsNotEmpty()
@IsString()
tipo: string;
@IsOptional()
@IsNumber()
contador_opcion?: number;
@IsOptional()
@IsNumber()
id_opcion_dependiente?: number;
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => OpcionDto)
opciones?: OpcionDto[];
}
+2 -2
View File
@@ -25,8 +25,8 @@ export class Pregunta {
@Column()
id_tipo_pregunta: number;
@Column({ nullable: true })
id_opcion_dependiente: number;
@Column({ type: 'int', nullable: true })
id_opcion_dependiente?: number;
@OneToMany(() => SeccionPregunta, seccionPregunta => seccionPregunta.pregunta)
seccionPreguntas: SeccionPregunta[];
+7
View File
@@ -2,32 +2,39 @@ import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/commo
import { PreguntaService } from './pregunta.service';
import { CreatePreguntaDto } from './dto/create-pregunta.dto';
import { UpdatePreguntaDto } from './dto/update-pregunta.dto';
import { PreguntaApiDocumentation } from './pregunta.documentation';
@Controller('pregunta')
@PreguntaApiDocumentation.ApiController
export class PreguntaController {
constructor(private readonly preguntaService: PreguntaService) {}
@Post()
@PreguntaApiDocumentation.ApiCreate
create(@Body() createPreguntaDto: CreatePreguntaDto) {
return this.preguntaService.create(createPreguntaDto);
}
@Get()
@PreguntaApiDocumentation.ApiGetAll
findAll() {
return this.preguntaService.findAll();
}
@Get(':id')
@PreguntaApiDocumentation.ApiGetOne
findOne(@Param('id') id: string) {
return this.preguntaService.findOne(+id);
}
@Patch(':id')
@PreguntaApiDocumentation.ApiUpdate
update(@Param('id') id: string, @Body() updatePreguntaDto: UpdatePreguntaDto) {
return this.preguntaService.update(+id, updatePreguntaDto);
}
@Delete(':id')
@PreguntaApiDocumentation.ApiRemove
remove(@Param('id') id: string) {
return this.preguntaService.remove(+id);
}
+189
View File
@@ -0,0 +1,189 @@
import { ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { applyDecorators } from '@nestjs/common';
export class PreguntaApiDocumentation {
// Decoradores para toda la clase del controlador
static ApiController = ApiTags('Preguntas');
// Documentación para crear una pregunta
static ApiCreate = applyDecorators(
ApiOperation({
summary: 'Crear una nueva pregunta',
description: 'Crea una nueva pregunta para una sección'
}),
ApiBody({
description: 'Datos de la pregunta a crear',
schema: {
type: 'object',
required: ['titulo', 'tipo'],
properties: {
titulo: { type: 'string', example: '¿Eres parte de la comunidad?' },
obligatoria: { type: 'boolean', example: true },
tipo: { type: 'string', example: 'Multiple' },
contador_opcion: { type: 'number', example: 0 },
id_opcion_dependiente: { type: 'number', example: null },
opciones: {
type: 'array',
items: {
type: 'object',
properties: {
valor: { type: 'string', example: 'Si' }
}
}
}
}
}
}),
ApiResponse({
status: 201,
description: 'Pregunta creada correctamente',
schema: {
type: 'object',
properties: {
id_pregunta: { type: 'number', example: 1 },
pregunta: { type: 'string', example: '¿Eres parte de la comunidad?' },
obligatoria: { type: 'boolean', example: true },
contador_opcion: { type: 'number', example: 0 },
id_tipo_pregunta: { type: 'number', example: 1 },
id_opcion_dependiente: { type: 'number', example: null }
}
}
}),
ApiResponse({ status: 400, description: 'Datos de la pregunta inválidos' }),
ApiResponse({ status: 500, description: 'Error interno del servidor' })
);
// Documentación para obtener todas las preguntas
static ApiGetAll = applyDecorators(
ApiOperation({
summary: 'Obtener todas las preguntas',
description: 'Retorna una lista de todas las preguntas registradas'
}),
ApiResponse({
status: 200,
description: 'Lista de preguntas obtenida correctamente',
schema: {
type: 'array',
items: {
type: 'object',
properties: {
id_pregunta: { type: 'number', example: 1 },
pregunta: { type: 'string', example: '¿Eres parte de la comunidad?' },
obligatoria: { type: 'boolean', example: true },
contador_opcion: { type: 'number', example: 0 },
id_tipo_pregunta: { type: 'number', example: 1 },
id_opcion_dependiente: { type: 'number', example: null }
}
}
}
}),
ApiResponse({ status: 500, description: 'Error interno del servidor' })
);
// Documentación para obtener una pregunta por ID
static ApiGetOne = applyDecorators(
ApiOperation({
summary: 'Obtener una pregunta por ID',
description: 'Retorna una pregunta específica por su ID'
}),
ApiParam({
name: 'id',
description: 'ID de la pregunta',
required: true,
type: 'number',
example: 1
}),
ApiResponse({
status: 200,
description: 'Pregunta obtenida correctamente',
schema: {
type: 'object',
properties: {
id_pregunta: { type: 'number', example: 1 },
pregunta: { type: 'string', example: '¿Eres parte de la comunidad?' },
obligatoria: { type: 'boolean', example: true },
contador_opcion: { type: 'number', example: 0 },
id_tipo_pregunta: { type: 'number', example: 1 },
id_opcion_dependiente: { type: 'number', example: null },
opciones: {
type: 'array',
items: {
type: 'object',
properties: {
id_opcion: { type: 'number', example: 1 },
opcion: { type: 'string', example: 'Si' }
}
}
}
}
}
}),
ApiResponse({ status: 404, description: 'Pregunta no encontrada' }),
ApiResponse({ status: 500, description: 'Error interno del servidor' })
);
// Documentación para actualizar una pregunta
static ApiUpdate = applyDecorators(
ApiOperation({
summary: 'Actualizar una pregunta',
description: 'Actualiza los datos de una pregunta existente'
}),
ApiParam({
name: 'id',
description: 'ID de la pregunta a actualizar',
required: true,
type: 'number',
example: 1
}),
ApiBody({
description: 'Datos a actualizar de la pregunta',
schema: {
type: 'object',
properties: {
pregunta: { type: 'string', example: 'Pregunta actualizada' },
obligatoria: { type: 'boolean', example: false }
}
}
}),
ApiResponse({
status: 200,
description: 'Pregunta actualizada correctamente',
schema: {
type: 'object',
properties: {
affected: { type: 'number', example: 1 }
}
}
}),
ApiResponse({ status: 400, description: 'Datos de actualización inválidos' }),
ApiResponse({ status: 404, description: 'Pregunta no encontrada' }),
ApiResponse({ status: 500, description: 'Error interno del servidor' })
);
// Documentación para eliminar una pregunta
static ApiRemove = applyDecorators(
ApiOperation({
summary: 'Eliminar una pregunta',
description: 'Elimina permanentemente una pregunta por su ID'
}),
ApiParam({
name: 'id',
description: 'ID de la pregunta a eliminar',
required: true,
type: 'number',
example: 1
}),
ApiResponse({
status: 200,
description: 'Pregunta eliminada correctamente',
schema: {
type: 'object',
properties: {
affected: { type: 'number', example: 1 }
}
}
}),
ApiResponse({ status: 404, description: 'Pregunta no encontrada' }),
ApiResponse({ status: 500, description: 'Error interno del servidor' })
);
}