ticket-booking-be/src/event-episodes/event-episodes.controller.ts

311 lines
11 KiB
TypeScript

import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common';
import { EventEpisodeService } from './event-episodes.service';
import { Response } from 'express';
import { GenericResponse } from '../common/GenericResponse.model';
import EventEpisode from './event-episodes.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
@ApiTags('event-episode')
@Controller('event-episode')
export class EventEpisodeController {
constructor(private eventEpisodeService: EventEpisodeService) {}
@Get("/all")
@ApiOperation({ summary: 'Get all event episodes' })
@ApiResponse({
status: 200,
description: 'Successfully retrieved all event episodes',
})
@ApiResponse({
status: 404,
description: 'No event episodes found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No event episodes found"
},
"data": null
}
})
async getAllEventEpisodes(@Res() res: Response) {
const response = await this.eventEpisodeService.findAll() || [];
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No event episodes found'
}, null);
return res.status(404).send(errorResponse);
}
const httpResponse = new GenericResponse(null, response);
res.send(httpResponse);
}
@Get(':id')
@ApiOperation({ summary: 'Get event episode by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Event Episode 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 episode not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Event episode not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved event episode by 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);
return res.status(400).send(response);
}
const response = await this.eventEpisodeService.findByPk(id) || {};
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Event episode 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 event episodes based on criteria' })
@ApiBody({ type: EventEpisode, description: 'Filter criteria for event episodes' })
@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 episodes found based on filter criteria',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No event episodes found based on the filter criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered event episodes',
})
async filter(@Body() eventEpisode: EventEpisode, @Res() res: Response) {
if (!eventEpisode) {
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.eventEpisodeService.filter(eventEpisode) || [];
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No event episodes found based on the filter criteria'
}, null);
return res.status(404).send(errorResponse);
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
}
@Post()
@ApiOperation({ summary: 'Insert a new event episode' })
@ApiBody({ type: EventEpisode, description: 'Event episode data to insert' })
@ApiResponse({
status: 400,
description: 'Invalid event episode data',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.INVALID_DATA",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'Successfully created an event episode',
})
async insert(@Body() eventEpisode: EventEpisode, @Res() res: Response) {
if (!eventEpisode) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.INVALID_DATA',
stackTrace: 'Request'
}, null);
return res.status(400).send(response);
}
delete eventEpisode.id; // Ensure no ID is passed in the creation
const response = await this.eventEpisodeService.upsert(eventEpisode, true);
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
}
@Put()
@ApiOperation({ summary: 'Update an existing event episode' })
@ApiBody({ type: EventEpisode, description: 'Event episode data to update' })
@ApiResponse({
status: 400,
description: 'Invalid event episode data or ID missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.INVALID_DATA",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Event episode not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Event episode not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully updated event episode',
})
async update(@Body() eventEpisode: EventEpisode, @Res() res: Response) {
if (!eventEpisode || !eventEpisode.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.eventEpisodeService.upsert(eventEpisode, false);
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Event episode with ID ${eventEpisode.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 an event episode by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Event episode ID to delete' })
@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 episode not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Event episode not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully deleted event episode',
})
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.eventEpisodeService.remove(id) || {};
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Event episode with ID ${id} not found`
}, null);
return res.status(404).send(errorResponse);
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
}
}