remedify-assessment-be/src/config/config.controller.ts
2025-02-22 17:08:32 +05:30

105 lines
3.5 KiB
TypeScript

import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common';
import { Response } from 'express';
import { ConfigService } from './config.service';
import Config from './config.entity';
import { GenericResponse } from 'src/common/GenericResponse.model';
@Controller('config')
export class ConfigController {
constructor(private configService: ConfigService) {}
@Get("/all")
async getAll(@Res() res: Response) {
const response = await this.configService.findAll() || [];
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Get(':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);
res.send(response);
return;
}
const response = await this.configService.findByPk(id) || {};
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Post('/filter')
async filter(@Body() config: Config, @Res() res: Response) {
if(!config) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request'
}, null);
res.send(response);
return;
}
const response = await this.configService.filter(config) || {};
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Post()
async insert(@Body() config: Config, @Res() res: Response) {
if(!config) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request'
}, null);
res.send(response);
return;
}
delete config.id;
const response = await this.configService.upsert(config, true);
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Put()
async update(@Body() config: Config, @Res() res: Response) {
if(!config || !config.id) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request'
}, null);
res.send(response);
return;
}
const response = await this.configService.upsert(config, false);
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Delete(':id')
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);
res.send(response);
return;
}
const response = await this.configService.remove(id) || {};
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
}