ticket-booking-be/src/user/user-additional-details/user-additional-details.controller.ts
2025-03-18 23:02:21 +05:30

341 lines
12 KiB
TypeScript

import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common';
import { Response } from 'express';
import { UserAdditionalDetailsService } from './user-additional-details.service';
import { GenericResponse } from 'src/common/GenericResponse.model';
import UserAdditionalDetail from './user-additional-details.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
@ApiTags('user-additional-details')
@Controller('users/addl/')
export class UserAdditionalDetailsController {
constructor(private userAdditionalDetailsService: UserAdditionalDetailsService) { }
@Get("/all")
@ApiOperation({ summary: 'Get all user additional details' })
@ApiResponse({
status: 200,
description: 'Successfully retrieved all user additional details',
})
@ApiResponse({
status: 404,
description: 'No user additional details found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No user additional details found"
},
"data": null
}
})
async userAdditionalDetailsgetAll(@Res() res: Response) {
const response = await this.userAdditionalDetailsService.findAll() || [];
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No user additional details found'
}, null);
return res.status(404).send(errorResponse);
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
}
@Get(':id')
@ApiOperation({ summary: 'Get user additional details by ID' })
@ApiParam({ name: 'id', type: Number, description: 'User Additional Details ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_IN_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'User additional details not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "User additional details not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved user additional details by ID',
})
async userAdditionalDetailsFindById(@Param('id') id: number, @Res() res: Response) {
if (!id) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_IN_REQ',
stackTrace: 'Request'
}, null);
res.status(400).send(response);
return;
}
const response = await this.userAdditionalDetailsService.findByPk(id) || {};
if(!response) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'User additional details not found'
}, null);
res.status(404).send(response);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
}
@Post('/filter')
@ApiOperation({ summary: 'Filter user additional details based on criteria' })
@ApiBody({ type: UserAdditionalDetail, description: 'User additional detail filter criteria' })
@ApiResponse({
status: 400,
description: 'Request body is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'No user additional details found matching criteria',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No user additional details found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered user additional details',
})
async userAdditionalDetailsFilter(@Body() user: UserAdditionalDetail, @Res() res: Response) {
if (!user) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request'
}, null);
res.status(400).send(response);
return;
}
const response = await this.userAdditionalDetailsService.filter(user) || [];
if (!response) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No user additional details found matching criteria'
}, null);
res.status(404).send(response);
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
}
@Post()
@ApiOperation({ summary: 'Insert a new user additional detail' })
@ApiBody({ type: UserAdditionalDetail, description: 'User additional detail data to insert' })
@ApiResponse({
status: 400,
description: 'Failed to insert user additional detail',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_CREATED",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'User additional detail not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "User additional detail not found"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'User additional detail successfully created',
})
async userAdditionalDetailsInsert(@Body() user: UserAdditionalDetail, @Res() res: Response) {
if (!user) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request'
}, null);
res.status(400).send(response);
return;
}
delete user.id;
const response = await this.userAdditionalDetailsService.upsert(user, true);
if (!response) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_CREATED',
stackTrace: 'User additional detail not created'
}, null);
res.status(404).send(response);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
}
@Put()
@ApiOperation({ summary: 'Update an existing user additional detail' })
@ApiBody({ type: UserAdditionalDetail, description: 'User additional detail data to update' })
@ApiResponse({
status: 400,
description: 'Failed to update user additional detail',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_UPDATED",
"stackTrace": "User additional detail not updated"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'User additional detail not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "User additional detail not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'User additional detail successfully updated',
})
async userAdditionalDetailsUpdate(@Body() user: UserAdditionalDetail, @Res() res: Response) {
if (!user || !user.id) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request'
}, null);
res.status(400).send(response);
return;
}
const response = await this.userAdditionalDetailsService.upsert(user, false);
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'User additional detail 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 user additional detail by ID' })
@ApiParam({ name: 'id', type: Number, description: 'User additional detail ID to delete' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_IN_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'User additional detail not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "User additional detail not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'User additional detail successfully deleted',
})
async userAdditionalDetailsDeleteById(@Param('id') id: number, @Res() res: Response) {
if (!id) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_IN_REQ',
stackTrace: 'Request'
}, null);
res.status(400).send(response);
return;
}
const response = await this.userAdditionalDetailsService.remove(id) || {};
if (!response) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'User additional detail not found'
}, null);
res.status(404).send(response);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
}
}