Skip to content

Commit

Permalink
finalizando
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunobento1990 committed Jan 24, 2025
1 parent e9fec26 commit 43a88cc
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 15 deletions.
28 changes: 28 additions & 0 deletions OpenAdm.Api/Controllers/CnpjConsultaController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Mvc;
using OpenAdm.Application.Dtos.Response;
using OpenAdm.Application.Interfaces;
using OpenAdm.Application.Models.Fretes;
using OpenAdm.Infra.Model;

namespace OpenAdm.Api.Controllers;

[ApiController]
[Route("cnpj")]
public class CnpjConsultaController : ControllerBase
{
private readonly ICnpjConsultaService _cnpjConsultaService;

public CnpjConsultaController(ICnpjConsultaService cnpjConsultaService)
{
_cnpjConsultaService = cnpjConsultaService;
}

[HttpGet("consulta")]
[ProducesResponseType<ConsultaCnpjResponse>(200)]
[ProducesResponseType<ErrorResponse>(400)]
public async Task<IActionResult> Consulta([FromQuery] string cnpj)
{
var response = await _cnpjConsultaService.ConsultaCnpjAsync(cnpj);
return Ok(response);
}
}
5 changes: 3 additions & 2 deletions OpenAdm.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
var email = VariaveisDeAmbiente.GetVariavel("EMAIL");
var servidor = VariaveisDeAmbiente.GetVariavel("SERVER");
var senha = VariaveisDeAmbiente.GetVariavel("SENHA");
var urlConsultaCnpj = VariaveisDeAmbiente.GetVariavel("ULR_CONSULTA_CNPJ");
var porta = int.Parse(VariaveisDeAmbiente.GetVariavel("PORT"));

ConfiguracaoDeToken.Configure(keyJwt, issue, audience, expirate);
Expand All @@ -45,13 +46,13 @@
builder.Services.InjectJwt(keyJwt, issue, audience);
builder.Services.InjectContext(pgString);
builder.Services.InjectRepositories(redisString);
builder.Services.InjectHttpClient(urlDiscord, urlApiCep, urlApiMercadoPago);
builder.Services.InjectHttpClient(urlDiscord, urlApiCep, urlApiMercadoPago, urlConsultaCnpj);

QuestPDF.Settings.License = LicenseType.Community;

var app = builder.Build();

var basePath = "/api/v1";
var basePath = "/api";
app.UsePathBase(new PathString(basePath));

app.UseRouting();
Expand Down
29 changes: 17 additions & 12 deletions OpenAdm.Application/Dtos/Usuarios/CreateUsuarioDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ public class CreateUsuarioDto : BaseModel
public string Telefone { get; set; } = string.Empty;
public string? Cnpj { get; set; } = string.Empty;
public string? Cpf { get; set; } = string.Empty;
public TipoPessoa TipoPessoa { get; set; }

public void Validar()
{
if ((string.IsNullOrWhiteSpace(Cpf) && string.IsNullOrWhiteSpace(Cnpj)) ||
!string.IsNullOrWhiteSpace(Cpf) && !string.IsNullOrWhiteSpace(Cnpj))
if (TipoPessoa == TipoPessoa.Juridica && string.IsNullOrWhiteSpace(Cnpj))
{
throw new ExceptionApi("Informe o CPF ou o CNPJ");
throw new ExceptionApi("Informe o CNPJ");
}

if (TipoPessoa == TipoPessoa.Fisica && string.IsNullOrWhiteSpace(Cpf))
{
throw new ExceptionApi("Informe o CPF");
}

if (string.IsNullOrWhiteSpace(Nome))
Expand All @@ -45,7 +50,7 @@ public void Validar()
throw new ExceptionApi("As senha não conferem!");
}

if (!string.IsNullOrWhiteSpace(Cpf) && !ValidarCnpjECpf.IsCpf(Cpf))
if (!string.IsNullOrWhiteSpace(Cpf) && !ValidarCnpjECpf.IsCpf(Cpf) && TipoPessoa == TipoPessoa.Fisica)
{
throw new ExceptionApi("CPF inválido!");
}
Expand All @@ -56,12 +61,6 @@ public void Validar()

public Usuario ToEntity()
{
if ((string.IsNullOrWhiteSpace(Cnpj) && string.IsNullOrWhiteSpace(Cpf))
|| (!string.IsNullOrWhiteSpace(Cnpj) && !string.IsNullOrWhiteSpace(Cpf)))
{
throw new ExceptionApi("Informe o CNPJ ou o CPF");
}

var senha = PasswordAdapter.GenerateHash(Senha);

var date = DateTime.Now;
Expand All @@ -74,8 +73,14 @@ public Usuario ToEntity()
senha,
Nome,
Telefone,
string.IsNullOrWhiteSpace(Cnpj) ? null : Cnpj,
string.IsNullOrWhiteSpace(Cpf) ? null : Cpf,
TipoPessoa == TipoPessoa.Juridica ? Cnpj : null,
TipoPessoa == TipoPessoa.Fisica ? Cpf : null,
true);
}
}

public enum TipoPessoa
{
Juridica = 1,
Fisica = 2
}
8 changes: 8 additions & 0 deletions OpenAdm.Application/Interfaces/ICnpjConsultaService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OpenAdm.Infra.Model;

namespace OpenAdm.Application.Interfaces;

public interface ICnpjConsultaService
{
Task<ConsultaCnpjResponse> ConsultaCnpjAsync(string cnpj);
}
26 changes: 26 additions & 0 deletions OpenAdm.Application/Services/CnpjConsultaService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using OpenAdm.Application.Interfaces;
using OpenAdm.Domain.Exceptions;
using OpenAdm.Infra.HttpService.Interfaces;
using OpenAdm.Infra.Model;

namespace OpenAdm.Application.Services;

public class CnpjConsultaService : ICnpjConsultaService
{
private readonly ICnpjHttpService _cnpjHttpService;

public CnpjConsultaService(ICnpjHttpService cnpjHttpService)
{
_cnpjHttpService = cnpjHttpService;
}

public async Task<ConsultaCnpjResponse> ConsultaCnpjAsync(string cnpj)
{
if (string.IsNullOrWhiteSpace(cnpj))
{
throw new ExceptionApi("CNPJ inválido");
}

return await _cnpjHttpService.ConsultarCnpjAsync(cnpj);
}
}
6 changes: 6 additions & 0 deletions OpenAdm.Infra/Enums/HttpServiceEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace OpenAdm.Infra.Enums;

public enum HttpServiceEnum
{
ConsultaCnpj
}
8 changes: 8 additions & 0 deletions OpenAdm.Infra/HttpService/Interfaces/ICnpjHttpService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OpenAdm.Infra.Model;

namespace OpenAdm.Infra.HttpService.Interfaces;

public interface ICnpjHttpService
{
Task<ConsultaCnpjResponse> ConsultarCnpjAsync(string cnpj);
}
45 changes: 45 additions & 0 deletions OpenAdm.Infra/HttpService/Services/CnpjHttpService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Text.Json;
using OpenAdm.Domain.Exceptions;
using OpenAdm.Infra.Enums;
using OpenAdm.Infra.HttpService.Interfaces;
using OpenAdm.Infra.Model;

namespace OpenAdm.Infra.HttpService.Services;

public class CnpjHttpService : ICnpjHttpService
{
private readonly IHttpClientFactory _httpClientFactory;

public CnpjHttpService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}

public async Task<ConsultaCnpjResponse> ConsultarCnpjAsync(string cnpj)
{
var client = _httpClientFactory.CreateClient(HttpServiceEnum.ConsultaCnpj.ToString());

var response = await client.GetAsync(cnpj);
var body = await response.Content.ReadAsStreamAsync();

if (!response.IsSuccessStatusCode)
{
if (body == null || body.Length == 0)
{
throw new ExceptionApi("Não foi possível obter a resposta da consulta do seu CNPJ");
}

var erro = JsonSerializer.Deserialize<ConsultaCnpjErroResponse>(body, JsonSerializerOptionsApi.Options());

if (!string.IsNullOrWhiteSpace(erro?.Message))
{
throw new ExceptionApi(erro.Message);
}

throw new ExceptionApi("Não foi possível efetuar a consulta do seu CNPJ");
}

return JsonSerializer.Deserialize<ConsultaCnpjResponse>(body, JsonSerializerOptionsApi.Options())
?? throw new ExceptionApi("Não foi possível efetuar a consulta do seu CNPJ!"); ;
}
}
6 changes: 6 additions & 0 deletions OpenAdm.Infra/Model/ConsultaCnpjErroResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace OpenAdm.Infra.Model;

public class ConsultaCnpjErroResponse
{
public string Message { get; set; } = string.Empty;
}
Loading

0 comments on commit 43a88cc

Please sign in to comment.