Refactor code structure for improved readability and maintainability
Deploy NestJS API / build-and-push-deploy (push) Failing after 1m34s Details

This commit is contained in:
joelson 2026-01-08 18:43:22 -03:00
parent 4e926909b8
commit 94f9208ee4
2 changed files with 802 additions and 705 deletions

View File

@ -1,109 +1,148 @@
import { Body, Controller, Get, HttpException, HttpStatus, Param, Post, Query } from '@nestjs/common'; import {
import { CustomerService } from './customer.service'; Body,
import { ResultModel } from '../../domain/models/result.model'; Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
Query,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { error } from 'console'; import { error } from 'console';
import { Customer } from 'src/domain/models/customer.model'; import { Customer } from 'src/domain/models/customer.model';
import { ApiTags } from '@nestjs/swagger'; import { ResultModel } from '../../domain/models/result.model';
import { CustomerService } from './customer.service';
@ApiTags('Customer') @ApiTags('Customer')
@Controller('api/v1/customer') @Controller('api/v1/customer')
export class CustomerController { export class CustomerController {
constructor(private readonly customerService: CustomerService) {}
constructor(private readonly customerService: CustomerService){} @Get(':name')
async getCustomerByName(@Param('name') name: string) {
@Get(':name') try {
async getCustomerByName(@Param('name') name: string){ const customers = await this.customerService.findCustomerByName(name);
try{ return new ResultModel(true, null, customers, null);
const customers = await this.customerService.findCustomerByName(name); } catch (err) {
return new ResultModel(true, null, customers, null); throw new HttpException(
} catch(err){ new ResultModel(
throw new HttpException(new ResultModel(false, 'Não foi possível consultar o cadastro de clientes.', {}, error), false,
HttpStatus.INTERNAL_SERVER_ERROR); 'Não foi possível consultar o cadastro de clientes.',
} {},
error,
),
HttpStatus.INTERNAL_SERVER_ERROR,
);
} }
}
@Get('categories/fechAll') @Get('categories/fechAll')
async getCategories(){ async getCategories() {
try{ try {
const categories = await this.customerService.getCategory(); const categories = await this.customerService.getCategory();
return categories; return categories;
} catch(err){ } catch (err) {
throw new HttpException(new ResultModel(false, err.message, {}, error), throw new HttpException(
HttpStatus.INTERNAL_SERVER_ERROR); new ResultModel(false, err.message, {}, error),
} HttpStatus.INTERNAL_SERVER_ERROR,
);
} }
}
@Get('subcategories/fechAll') @Get('subcategories/fechAll')
async getSubCategories(){ async getSubCategories() {
try{ try {
const subCategories = await this.customerService.getSubCategory(); const subCategories = await this.customerService.getSubCategory();
return subCategories; return subCategories;
} catch(err){ } catch (err) {
throw new HttpException(new ResultModel(false, err.message, {}, error), throw new HttpException(
HttpStatus.INTERNAL_SERVER_ERROR); new ResultModel(false, err.message, {}, error),
} HttpStatus.INTERNAL_SERVER_ERROR,
);
} }
}
@Get()
@Get() async getCustomer(@Query() query) {
async getCustomer(@Query() query){ try {
try{ const field = query['field'];
const field = query['field']; const textSearch = query['textsearch'];
const textSearch = query['textsearch']; const customers = await this.customerService.findCustomerByQuery(
const customers = await this.customerService.findCustomerByQuery(field, textSearch); field,
return new ResultModel(true, null, customers, null); textSearch,
} catch(err){ );
// 'Não foi possível consultar o cadastro de clientes.' return new ResultModel(true, null, customers, null);
throw new HttpException(new ResultModel(false, err.message, {}, error), } catch (err) {
HttpStatus.INTERNAL_SERVER_ERROR); // 'Não foi possível consultar o cadastro de clientes.'
} throw new HttpException(
new ResultModel(false, err.message, {}, error),
HttpStatus.INTERNAL_SERVER_ERROR,
);
} }
}
@Get(':id') @Get(':id')
async getCustomerById(@Param('id') id: number){ async getCustomerById(@Param('id') id: number) {
try{ try {
const customers = await this.customerService.findCustomerById(id); const customers = await this.customerService.findCustomerById(id);
return new ResultModel(true, null, customers, null); return new ResultModel(true, null, customers, null);
} catch(err){ } catch (err) {
throw new HttpException(new ResultModel(false, 'Não foi possível consultar o cadastro de clientes.', {}, error), throw new HttpException(
HttpStatus.INTERNAL_SERVER_ERROR); new ResultModel(
} false,
'Não foi possível consultar o cadastro de clientes.',
{},
error,
),
HttpStatus.INTERNAL_SERVER_ERROR,
);
} }
}
@Get('cpf/:cpf') @Get('cpf/:cpf')
async getCustomerByCpf(@Param('cpf') cpf: string){ async getCustomerByCpf(@Param('cpf') cpf: string) {
try{ try {
console.log("pesquisando por cpf"); console.log('pesquisando por cpf');
const customer = await this.customerService.findCustomerByCpf(cpf); const customer = await this.customerService.findCustomerByCpf(cpf);
if (!customer) return new ResultModel(false, 'Cliente não cadastrado', null, null); if (!customer)
return new ResultModel(true, null, customer, null); return new ResultModel(false, 'Cliente não cadastrado', null, null);
} catch(err){ return new ResultModel(true, null, customer, null);
throw new HttpException(new ResultModel(false, 'Não foi possível consultar o cadastro de clientes.', {}, error), } catch (err) {
HttpStatus.INTERNAL_SERVER_ERROR); throw new HttpException(
} new ResultModel(
false,
'Não foi possível consultar o cadastro de clientes.',
{},
error,
),
HttpStatus.INTERNAL_SERVER_ERROR,
);
} }
@Get('create/proxnumcli') }
async IdCustomer(){ @Get('create/proxnumcli')
try{ async IdCustomer() {
console.log('proxnumcli'); try {
const id = await this.customerService.generateIdCustomer(); console.log('proxnumcli');
return new ResultModel(true, null, id, null); const id = await this.customerService.generateIdCustomer();
} catch(err){ return new ResultModel(true, null, id, null);
throw err; } catch (err) {
} throw err;
} }
}
@Post('create')
@Post('create') async createCustomer(@Body() customer: Customer) {
async createCustomer(@Body() customer: Customer){ try {
try{ console.log(customer);
console.log(customer); const result = await this.customerService.createCustomer(customer);
const result = await this.customerService.createCustomer(customer); return new ResultModel(true, null, result, null);
return new ResultModel(true, null, result, null); //return new ResultModel(true, null, id, null);
//return new ResultModel(true, null, id, null); } catch (err) {
} catch(err){ throw new HttpException(
throw new HttpException(new ResultModel(false, 'Erro ao cadastrar cliente.', {}, err), new ResultModel(false, 'Erro ao cadastrar cliente.', {}, err),
HttpStatus.INTERNAL_SERVER_ERROR); HttpStatus.INTERNAL_SERVER_ERROR,
} );
} }
}
} }

File diff suppressed because it is too large Load Diff