ticket-booking-be/src/event-category/event-category.controller.ts
2025-04-11 13:14:06 +05:30

291 lines
10 KiB
TypeScript

import { Body, Controller, Delete, Get, Param, Post, Put, Res, UsePipes, ValidationPipe } from '@nestjs/common';
import { EventCategoryService } from './event-category.service';
import { Response } from 'express';
import { GenericResponse } from '../common/GenericResponse.model';
import EventCategory from './event-category.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
import { EventCategoryDTO, EventCategoryUpdateDTO } from './event-category.dto';
@ApiTags('eventCategory')
@Controller('eventCategory')
export class EventCategoryController {
constructor(private eventCategoryService: EventCategoryService) {}
@Get('/all')
@ApiOperation({ summary: 'Get all event categories' })
@ApiResponse({ status: 200, description: 'Successfully retrieved all event categories' })
@ApiResponse({
status: 404,
description: 'No event categories found',
example: {
notification: {
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No event categories found'
},
data: null
}
})
async getAll(@Res() res: Response) {
const response = await this.eventCategoryService.findAll();
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No event categories found'
}, null);
return res.status(404).send(errorResponse);
}
res.send(new GenericResponse(null, response));
}
@Get(':id')
@ApiOperation({ summary: 'Get event category by ID' })
@ApiParam({ name: 'id', type: Number, description: 'EventCategory ID' })
@ApiResponse({
status: 200,
description: 'Successfully retrieved event category by ID',
})
@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: 'Event category not found',
example: {
notification: {
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Event category not found'
},
data: null
}
})
async findById(@Param('id') id: number, @Res() res: Response) {
if (!id) {
return res.status(400).send(new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request'
}, null));
}
const response = await this.eventCategoryService.findByPk(id);
if (!response) {
return res.status(404).send(new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Event category with ID ${id} not found`
}, null));
}
res.status(200).send(new GenericResponse(null, response));
}
@Post('/filter')
@ApiOperation({ summary: 'Filter event categories based on criteria' })
@ApiBody({ type: EventCategory, description: 'Filter criteria for event categories' })
@ApiResponse({ status: 200, description: 'Successfully filtered event categories' })
@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 event categories found based on filter criteria',
example: {
notification: {
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No event categories found based on the filter criteria'
},
data: null
}
})
async filter(@Body() category: EventCategory, @Res() res: Response) {
if (!category) {
return res.status(400).send(new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.INVALID_CRITERIA',
stackTrace: 'Request'
}, null));
}
const response = await this.eventCategoryService.filter(category);
if (!response) {
return res.status(404).send(new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No event categories found based on the filter criteria'
}, null));
}
res.status(200).send(new GenericResponse(null, response));
}
@Post()
@ApiOperation({ summary: 'Insert a new event category' })
@ApiBody({ type: EventCategoryDTO, description: 'Event category data to insert' })
@ApiResponse({ status: 201, description: 'Successfully created an event category' })
@ApiResponse({
status: 400,
description: 'Invalid event category data',
example: {
notification: {
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.INVALID_DATA',
stackTrace: 'Request'
},
data: null
}
})
@UsePipes(new ValidationPipe({ whitelist: true}))
async insert(@Body() category: EventCategoryDTO, @Res() res: Response) {
if (!category) {
return res.status(400).send(new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.INVALID_DATA',
stackTrace: 'Request'
}, null));
}
const response = await this.eventCategoryService.upsert(category, true);
res.status(201).send(new GenericResponse(null, response));
}
@Put()
@ApiOperation({ summary: 'Update an existing event category' })
@ApiBody({ type: EventCategoryUpdateDTO, description: 'Event category data to update' })
@ApiResponse({ status: 200, description: 'Successfully updated event category' })
@ApiResponse({
status: 400,
description: 'Invalid data or ID missing',
example: {
notification: {
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request'
},
data: null
}
})
@ApiResponse({
status: 404,
description: 'Event category not found',
example: {
notification: {
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Event category not found'
},
data: null
}
})
@UsePipes(new ValidationPipe({ whitelist: true, skipMissingProperties: true }))
async update(@Body() category: EventCategoryUpdateDTO, @Res() res: Response) {
if (!category || !category.id) {
return res.status(400).send(new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request'
}, null));
}
const response = await this.eventCategoryService.upsert(category, false);
if (!response) {
return res.status(404).send(new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Event category with ID ${category.id} not found`
}, null));
}
res.status(200).send(new GenericResponse(null, response));
}
@Delete(':id')
@ApiOperation({ summary: 'Delete an event category by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Event category ID to delete' })
@ApiResponse({ status: 200, description: 'Successfully deleted event category' })
@ApiResponse({
status: 400,
description: 'ID parameter is required',
example: {
notification: {
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request'
},
data: null
}
})
@ApiResponse({
status: 404,
description: 'Event category not found',
example: {
notification: {
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Event category not found'
},
data: null
}
})
async deleteById(@Param('id') id: number, @Res() res: Response) {
if (!id) {
return res.status(400).send(new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request'
}, null));
}
const response = await this.eventCategoryService.remove(id);
if (!response) {
return res.status(404).send(new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Event category with ID ${id} not found`
}, null));
}
res.status(200).send(new GenericResponse(null, response));
}
}