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

331 lines
11 KiB
TypeScript

import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common';
import { UserService } from './user.service';
import { Response } from 'express';
import { GenericResponse } from '../common/GenericResponse.model';
import { User } from './user.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
@ApiTags('users')
@Controller('users')
export class UserController {
constructor(private userService: UserService) {}
@Get("/all")
@ApiOperation({ summary: 'Get all users' })
@ApiResponse({
status: 200,
description: 'Successfully retrieved all users',
})
@ApiResponse({
status: 404,
description: 'No users found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No users found"
},
"data": null
}
})
async getAllUsers(@Res() res: Response) {
const response = await this.userService.findAll();
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No users found'
}, null);
return res.status(404).send(errorResponse);
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
}
@Get(':id')
@ApiOperation({ summary: 'Get a user by ID' })
@ApiParam({ name: 'id', type: Number, description: 'User ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'User not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "User not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved user 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.userService.findByPk(id);
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `User 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 users based on criteria' })
@ApiBody({ type: User, description: 'User filter criteria' })
@ApiResponse({
status: 400,
description: 'Failed to insert user',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_CREATED",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'No users matching criteria found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No users found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered users',
})
async filter(@Body() user: User, @Res() res: Response) {
if (!user) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request'
}, null);
return res.status(400).send(response);
}
const response = await this.userService.filter(user);
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No users found matching 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 user' })
@ApiBody({ type: User, description: 'User data to insert' })
@ApiResponse({
status: 400,
description: 'Failed to insert user',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_CREATED",
"stackTrace": "User not created"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'User successfully created',
})
async insert(@Body() user: User, @Res() res: Response) {
if (!user) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request'
}, null);
return res.status(400).send(response);
}
delete user.id;
if (user.password) {
user.password = this.encryptPassword(user.password);
}
const response = await this.userService.upsert(user, true);
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_CREATED',
stackTrace: 'User not created'
}, null);
return res.status(404).send(errorResponse);
}
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
}
@Put()
@ApiOperation({ summary: 'Update an existing user' })
@ApiBody({ type: User, description: 'User data to update' })
@ApiResponse({
status: 400,
description: 'Failed to update user',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_UPDATED",
"stackTrace": "User not updated"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'User not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "User not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'User successfully updated',
})
async update(@Body() user: User, @Res() res: Response) {
if (!user || !user.id) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request'
}, null);
return res.status(400).send(response);
}
if (user.password) {
user.password = this.encryptPassword(user.password);
}
const response = await this.userService.upsert(user, false);
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'User 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 user by ID' })
@ApiParam({ name: 'id', type: Number, description: 'User ID to delete' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'User not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "User not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'User successfully deleted',
})
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.userService.remove(id);
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `User with ID ${id} not found`
}, null);
return res.status(404).send(errorResponse);
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
}
encryptPassword(password: string) {
return Buffer.from(password).toString('base64');
}
}