From cbb7379a1b416bc82f20e89c56397a6c3f28217c Mon Sep 17 00:00:00 2001 From: Edinei Cavalcanti Date: Fri, 19 Sep 2025 18:25:24 -0300 Subject: [PATCH 1/3] add CooperaSharp.Service --- .../CooperaSharp.Service/.dockerignore | 25 ++ CooperaSharp/CooperaSharp.Service/.gitignore | 4 + .../CooperaSharp.Service.Tests.csproj | 22 ++ .../PedidoServiceTests.cs | 231 ++++++++++++++++++ .../CooperaSharp.Service.sln | 27 ++ .../CooperaSharp.Service/Client.cs | 6 + .../CooperaSharp.Service.csproj | 17 ++ .../CooperaSharp.Service/Dockerfile | 21 ++ .../CooperaSharp.Service/IAuditoriaService.cs | 6 + .../IClienteRepository.cs | 6 + .../CooperaSharp.Service/IEmailService.cs | 6 + .../CooperaSharp.Service/IEstoqueService.cs | 6 + .../CooperaSharp.Service/IFreteService.cs | 6 + .../CooperaSharp.Service/ILogger.cs | 6 + .../CooperaSharp.Service/IMapper.cs | 6 + .../INotificacaoService.cs | 6 + .../CooperaSharp.Service/IPagamentoService.cs | 6 + .../CooperaSharp.Service/IPedidoRepository.cs | 6 + .../CooperaSharp.Service/Pedido.cs | 15 ++ .../CooperaSharp.Service/PedidoDto.cs | 7 + .../CooperaSharp.Service/PedidoService.cs | 70 ++++++ .../CooperaSharp.Service/Program.cs | 7 + .../CooperaSharp.Service/compose.yaml | 7 + CooperaSharp/CooperaSharp.Service/global.json | 7 + 24 files changed, 526 insertions(+) create mode 100644 CooperaSharp/CooperaSharp.Service/.dockerignore create mode 100644 CooperaSharp/CooperaSharp.Service/.gitignore create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/CooperaSharp.Service.Tests.csproj create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/PedidoServiceTests.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.sln create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Client.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/CooperaSharp.Service.csproj create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Dockerfile create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IAuditoriaService.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IClienteRepository.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEmailService.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEstoqueService.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IFreteService.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/ILogger.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IMapper.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/INotificacaoService.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPagamentoService.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPedidoRepository.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Pedido.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoDto.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoService.cs create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Program.cs create mode 100644 CooperaSharp/CooperaSharp.Service/compose.yaml create mode 100644 CooperaSharp/CooperaSharp.Service/global.json diff --git a/CooperaSharp/CooperaSharp.Service/.dockerignore b/CooperaSharp/CooperaSharp.Service/.dockerignore new file mode 100644 index 0000000..38bece4 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/.gitignore b/CooperaSharp/CooperaSharp.Service/.gitignore new file mode 100644 index 0000000..c738267 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/.gitignore @@ -0,0 +1,4 @@ +.idea/ +bin/ +obj/ +*.user diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/CooperaSharp.Service.Tests.csproj b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/CooperaSharp.Service.Tests.csproj new file mode 100644 index 0000000..371b614 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/CooperaSharp.Service.Tests.csproj @@ -0,0 +1,22 @@ + + + + net9.0 + false + true + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/PedidoServiceTests.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/PedidoServiceTests.cs new file mode 100644 index 0000000..58d90f3 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/PedidoServiceTests.cs @@ -0,0 +1,231 @@ +using System; +using Xunit; + +namespace CooperaSharp.Service.Tests; + +public class PedidoServiceTests +{ + private class FakeClienteRepository : IClienteRepository + { + public Client? ClienteToReturn; + public int LastRequestedId; + public Client ObterPorId(int dtoClienteId) + { + LastRequestedId = dtoClienteId; + return ClienteToReturn!; + } + } + + private class FakePedidoRepository : IPedidoRepository + { + public Pedido? SavedPedido; + public void Salvar(Pedido pedido) + { + SavedPedido = pedido; + } + } + + private class FakeEstoqueService : IEstoqueService + { + public object ReservedItens; + public void ReservarItens(object itens) + { + ReservedItens = itens; + } + } + + private class FakePagamentoService : IPagamentoService + { + public bool Result = true; + public object? LastPedido; + public bool Processar(object pedido) + { + LastPedido = pedido; + return Result; + } + } + + private class FakeEmailService : IEmailService + { + public object? LastEmail; + public void EnviarConfirmacao(object email) + { + LastEmail = email; + } + } + + private class FakeNotificacaoService : INotificacaoService + { + public string? LastMessage; + public void Enviar(string falhaNoPagamento) + { + LastMessage = falhaNoPagamento; + } + } + + private class FakeLogger : ILogger + { + public string? LastLog; + public void Log(string clienteNaoEncontrado) + { + LastLog = clienteNaoEncontrado; + } + } + + private class FakeFreteService : IFreteService + { + public object? LastEndereco; + public object ValorToReturn = 10m; + public object CalcularFrete(string dtoEnderecoEntrega) + { + LastEndereco = dtoEnderecoEntrega; + return ValorToReturn; + } + } + + private class FakeMapper : IMapper + { + public Pedido? LastPedidoCreated; + public T Map(PedidoDto dto) + { + var pedido = new Pedido + { + Id = 123, + Itens = new object() + }; + LastPedidoCreated = pedido; + return (T)(object)pedido; + } + } + + private class FakeAuditoria : IAuditoriaService + { + public (string? Acao, object? Id)? LastRegistro; + public void Registrar(string pedidoCriado, object toString) + { + LastRegistro = (pedidoCriado, toString); + } + } + + private static PedidoService CreateService( + FakeClienteRepository clienteRepo, + FakePedidoRepository pedidoRepo, + FakeEstoqueService estoque, + FakePagamentoService pagamento, + FakeEmailService email, + FakeNotificacaoService notificacao, + FakeLogger logger, + FakeFreteService frete, + FakeMapper mapper, + FakeAuditoria auditoria) + { + return new PedidoService( + clienteRepo, + pedidoRepo, + estoque, + pagamento, + email, + notificacao, + logger, + frete, + mapper, + auditoria); + } + + [Fact] + public void ProcessarPedido_ClienteNaoEncontrado_DeveLogarENaoProsseguir() + { + var clienteRepo = new FakeClienteRepository { ClienteToReturn = null }; + var pedidoRepo = new FakePedidoRepository(); + var estoque = new FakeEstoqueService(); + var pagamento = new FakePagamentoService(); + var email = new FakeEmailService(); + var notificacao = new FakeNotificacaoService(); + var logger = new FakeLogger(); + var frete = new FakeFreteService(); + var mapper = new FakeMapper(); + var auditoria = new FakeAuditoria(); + var service = CreateService(clienteRepo, pedidoRepo, estoque, pagamento, email, notificacao, logger, frete, mapper, auditoria); + + var dto = new PedidoDto { ClienteId = 1, EnderecoEntrega = "Rua 1" }; + + service.ProcessarPedido(dto); + + Assert.Equal("Cliente não encontrado", logger.LastLog); + Assert.Null(mapper.LastPedidoCreated); + Assert.Null(estoque.ReservedItens); + Assert.Null(pagamento.LastPedido); + Assert.Null(pedidoRepo.SavedPedido); + Assert.Null(email.LastEmail); + Assert.Null(auditoria.LastRegistro); + Assert.Null(notificacao.LastMessage); + } + + [Fact] + public void ProcessarPedido_PagamentoFalha_DeveNotificarENaoSalvarNemEnviarEmail() + { + var clienteRepo = new FakeClienteRepository { ClienteToReturn = new Client { Email = "x@x.com" } }; + var pedidoRepo = new FakePedidoRepository(); + var estoque = new FakeEstoqueService(); + var pagamento = new FakePagamentoService { Result = false }; + var email = new FakeEmailService(); + var notificacao = new FakeNotificacaoService(); + var logger = new FakeLogger(); + var frete = new FakeFreteService { ValorToReturn = 42m }; + var mapper = new FakeMapper(); + var auditoria = new FakeAuditoria(); + var service = CreateService(clienteRepo, pedidoRepo, estoque, pagamento, email, notificacao, logger, frete, mapper, auditoria); + + var dto = new PedidoDto { ClienteId = 1, EnderecoEntrega = "Rua 2" }; + + service.ProcessarPedido(dto); + + // Fez etapas até pagamento + Assert.NotNull(mapper.LastPedidoCreated); + Assert.Equal(mapper.LastPedidoCreated!.Itens, estoque.ReservedItens); + Assert.Equal("Rua 2", frete.LastEndereco); + // Notificou falha + Assert.Equal("Falha no pagamento", notificacao.LastMessage); + // Não salvou nem enviou email nem auditou + Assert.Null(pedidoRepo.SavedPedido); + Assert.Null(email.LastEmail); + Assert.Null(auditoria.LastRegistro); + } + + [Fact] + public void ProcessarPedido_Sucesso_DeveExecutarTodoFluxo() + { + var clienteRepo = new FakeClienteRepository { ClienteToReturn = new Client { Email = "ok@ok.com" } }; + var pedidoRepo = new FakePedidoRepository(); + var estoque = new FakeEstoqueService(); + var pagamento = new FakePagamentoService { Result = true }; + var email = new FakeEmailService(); + var notificacao = new FakeNotificacaoService(); + var logger = new FakeLogger(); + var frete = new FakeFreteService { ValorToReturn = 99m }; + var mapper = new FakeMapper(); + var auditoria = new FakeAuditoria(); + var service = CreateService(clienteRepo, pedidoRepo, estoque, pagamento, email, notificacao, logger, frete, mapper, auditoria); + + var dto = new PedidoDto { ClienteId = 77, EnderecoEntrega = "Rua 3" }; + + service.ProcessarPedido(dto); + + // Reservou itens + Assert.Equal(mapper.LastPedidoCreated!.Itens, estoque.ReservedItens); + // Calculou frete e adicionou + Assert.Equal("Rua 3", frete.LastEndereco); + Assert.Equal(99m, mapper.LastPedidoCreated!.Frete); + // Processou pagamento com o pedido + Assert.Same(mapper.LastPedidoCreated, pagamento.LastPedido); + // Salvou + Assert.Same(mapper.LastPedidoCreated, pedidoRepo.SavedPedido); + // Enviou email + Assert.Equal("ok@ok.com", email.LastEmail); + // Auditou + Assert.Equal(("Pedido criado", "123"), auditoria.LastRegistro); + // Não deve ter registrado erro/notificação extra + Assert.Null(logger.LastLog); + Assert.Null(notificacao.LastMessage); + } +} diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.sln b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.sln new file mode 100644 index 0000000..4fb002f --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.sln @@ -0,0 +1,27 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CooperaSharp.Service", "CooperaSharp.Service\CooperaSharp.Service.csproj", "{29265864-0214-49D9-8B5A-FDB9C9999133}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{00FA2EFE-BDE3-4E49-BE2B-232693C19A72}" + ProjectSection(SolutionItems) = preProject + compose.yaml = compose.yaml + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CooperaSharp.Service.Tests", "CooperaSharp.Service.Tests\CooperaSharp.Service.Tests.csproj", "{3254B12C-1F64-49D3-94B5-7287789CB4F5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {29265864-0214-49D9-8B5A-FDB9C9999133}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {29265864-0214-49D9-8B5A-FDB9C9999133}.Debug|Any CPU.Build.0 = Debug|Any CPU + {29265864-0214-49D9-8B5A-FDB9C9999133}.Release|Any CPU.ActiveCfg = Release|Any CPU + {29265864-0214-49D9-8B5A-FDB9C9999133}.Release|Any CPU.Build.0 = Release|Any CPU + {3254B12C-1F64-49D3-94B5-7287789CB4F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3254B12C-1F64-49D3-94B5-7287789CB4F5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3254B12C-1F64-49D3-94B5-7287789CB4F5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3254B12C-1F64-49D3-94B5-7287789CB4F5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Client.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Client.cs new file mode 100644 index 0000000..3e18a88 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Client.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public class Client +{ + public string Email { get; set; } +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/CooperaSharp.Service.csproj b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/CooperaSharp.Service.csproj new file mode 100644 index 0000000..b67a87c --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/CooperaSharp.Service.csproj @@ -0,0 +1,17 @@ + + + + Exe + net9.0 + enable + enable + Linux + + + + + .dockerignore + + + + diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Dockerfile b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Dockerfile new file mode 100644 index 0000000..b355592 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Dockerfile @@ -0,0 +1,21 @@ +FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base +USER $APP_UID +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["CooperaSharp.Service/CooperaSharp.Service.csproj", "CooperaSharp.Service/"] +RUN dotnet restore "CooperaSharp.Service/CooperaSharp.Service.csproj" +COPY . . +WORKDIR "/src/CooperaSharp.Service" +RUN dotnet build "./CooperaSharp.Service.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./CooperaSharp.Service.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "CooperaSharp.Service.dll"] diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IAuditoriaService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IAuditoriaService.cs new file mode 100644 index 0000000..6367b2b --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IAuditoriaService.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface IAuditoriaService +{ + void Registrar(string pedidoCriado, object toString); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IClienteRepository.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IClienteRepository.cs new file mode 100644 index 0000000..31e66a8 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IClienteRepository.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface IClienteRepository +{ + Client ObterPorId(int dtoClienteId); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEmailService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEmailService.cs new file mode 100644 index 0000000..3548de8 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEmailService.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface IEmailService +{ + void EnviarConfirmacao(object email); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEstoqueService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEstoqueService.cs new file mode 100644 index 0000000..500d3a2 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEstoqueService.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface IEstoqueService +{ + void ReservarItens(object itens); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IFreteService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IFreteService.cs new file mode 100644 index 0000000..2c242d6 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IFreteService.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface IFreteService +{ + object CalcularFrete(string dtoEnderecoEntrega); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/ILogger.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/ILogger.cs new file mode 100644 index 0000000..cb68e2c --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/ILogger.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface ILogger +{ + void Log(string clienteNãoEncontrado); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IMapper.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IMapper.cs new file mode 100644 index 0000000..9333722 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IMapper.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface IMapper +{ + T Map(PedidoDto dto); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/INotificacaoService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/INotificacaoService.cs new file mode 100644 index 0000000..6aa3958 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/INotificacaoService.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface INotificacaoService +{ + void Enviar(string falhaNoPagamento); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPagamentoService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPagamentoService.cs new file mode 100644 index 0000000..124d216 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPagamentoService.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface IPagamentoService +{ + bool Processar(object pedido); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPedidoRepository.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPedidoRepository.cs new file mode 100644 index 0000000..866a08b --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPedidoRepository.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public interface IPedidoRepository +{ + void Salvar(Pedido pedido); +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Pedido.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Pedido.cs new file mode 100644 index 0000000..3762d02 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Pedido.cs @@ -0,0 +1,15 @@ +namespace CooperaSharp.Service; + +public class Pedido +{ + public object? Frete { get; private set; } + + public void AdicionarFrete(object valorFrete) + { + // Armazena o valor do frete sem lógica adicional para permitir testes + Frete = valorFrete; + } + + public object Itens { get; set; } + public int Id { get; set; } +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoDto.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoDto.cs new file mode 100644 index 0000000..6b8c21a --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoDto.cs @@ -0,0 +1,7 @@ +namespace CooperaSharp.Service; + +public class PedidoDto +{ + public int ClienteId { get; set; } + public string EnderecoEntrega { get; set; } +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoService.cs new file mode 100644 index 0000000..0a8d5f0 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoService.cs @@ -0,0 +1,70 @@ +namespace CooperaSharp.Service; + +public class PedidoService +{ + private readonly IClienteRepository _clienteRepository; + private readonly IPedidoRepository _pedidoRepository; + private readonly IEstoqueService _estoqueService; + private readonly IPagamentoService _pagamentoService; + private readonly IEmailService _emailService; + private readonly INotificacaoService _notificacaoService; + private readonly ILogger _logger; + private readonly IFreteService _freteService; + private readonly IMapper _mapper; + private readonly IAuditoriaService _auditoriaService; + + public PedidoService( + IClienteRepository clienteRepository, + IPedidoRepository pedidoRepository, + IEstoqueService estoqueService, + IPagamentoService pagamentoService, + IEmailService emailService, + INotificacaoService notificacaoService, + ILogger logger, + IFreteService freteService, + IMapper mapper, + IAuditoriaService auditoriaService) + { + _clienteRepository = clienteRepository; + _pedidoRepository = pedidoRepository; + _estoqueService = estoqueService; + _pagamentoService = pagamentoService; + _emailService = emailService; + _notificacaoService = notificacaoService; + _logger = logger; + _freteService = freteService; + _mapper = mapper; + _auditoriaService = auditoriaService; + } + + public void ProcessarPedido(PedidoDto dto) + { + var cliente = _clienteRepository.ObterPorId(dto.ClienteId); + + if (cliente == null) + { + _logger.Log("Cliente não encontrado"); + return; + } + + var pedido = _mapper.Map(dto); + + _estoqueService.ReservarItens(pedido.Itens); + + var valorFrete = _freteService.CalcularFrete(dto.EnderecoEntrega); + pedido.AdicionarFrete(valorFrete); + + var pagamentoOk = _pagamentoService.Processar(pedido); + + if (!pagamentoOk) + { + _notificacaoService.Enviar("Falha no pagamento"); + return; + } + + _pedidoRepository.Salvar(pedido); + + _emailService.EnviarConfirmacao(cliente.Email); + _auditoriaService.Registrar("Pedido criado", pedido.Id.ToString()); + } +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Program.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Program.cs new file mode 100644 index 0000000..a790029 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Program.cs @@ -0,0 +1,7 @@ +// See https://aka.ms/new-console-template for more information + +using CooperaSharp.Service; + +Console.WriteLine("Hello, World!"); + +//new PedidoService().ProcessarPedido(new PedidoDto()); \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/compose.yaml b/CooperaSharp/CooperaSharp.Service/compose.yaml new file mode 100644 index 0000000..9c150f4 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/compose.yaml @@ -0,0 +1,7 @@ +services: + cooperasharp.service: + image: cooperasharp.service + build: + context: . + dockerfile: CooperaSharp.Service/Dockerfile + diff --git a/CooperaSharp/CooperaSharp.Service/global.json b/CooperaSharp/CooperaSharp.Service/global.json new file mode 100644 index 0000000..93681ff --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "9.0.0", + "rollForward": "latestMinor", + "allowPrerelease": false + } +} \ No newline at end of file From 923decb20fea56b1cd295065f864f8514f5530f7 Mon Sep 17 00:00:00 2001 From: Edinei Cavalcanti Date: Fri, 19 Sep 2025 18:29:41 -0300 Subject: [PATCH 2/3] fix run test --- .../CooperaSharp.Service.Tests/CooperaSharp.Service.Tests.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/CooperaSharp.Service.Tests.csproj b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/CooperaSharp.Service.Tests.csproj index 371b614..6534c0b 100644 --- a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/CooperaSharp.Service.Tests.csproj +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/CooperaSharp.Service.Tests.csproj @@ -8,6 +8,7 @@ + all From e10ad8c6395e46573ae8b3c798991fea175878fa Mon Sep 17 00:00:00 2001 From: Edinei Cavalcanti Date: Fri, 19 Sep 2025 19:12:28 -0300 Subject: [PATCH 3/3] fix ajusts with error in testes --- .../PedidoServiceTests.cs | 7 +--- .../{ => Dto}/PedidoDto.cs | 0 .../{ => Model}/Client.cs | 0 .../{ => Model}/Pedido.cs | 0 .../{ => Repository}/IClienteRepository.cs | 0 .../{ => Repository}/IPedidoRepository.cs | 0 .../{ => Service}/IAuditoriaService.cs | 0 .../Service/IClienteService.cs | 33 +++++++++++++++++++ .../{ => Service}/IEmailService.cs | 0 .../{ => Service}/IEstoqueService.cs | 0 .../{ => Service}/IFreteService.cs | 0 .../{ => Service}/INotificacaoService.cs | 0 .../{ => Service}/IPagamentoService.cs | 0 .../{ => Service}/PedidoService.cs | 28 +++++++--------- .../CooperaSharp.Service/Validation.cs | 6 ++++ 15 files changed, 51 insertions(+), 23 deletions(-) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Dto}/PedidoDto.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Model}/Client.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Model}/Pedido.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Repository}/IClienteRepository.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Repository}/IPedidoRepository.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Service}/IAuditoriaService.cs (100%) create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IClienteService.cs rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Service}/IEmailService.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Service}/IEstoqueService.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Service}/IFreteService.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Service}/INotificacaoService.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Service}/IPagamentoService.cs (100%) rename CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/{ => Service}/PedidoService.cs (70%) create mode 100644 CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Validation.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/PedidoServiceTests.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/PedidoServiceTests.cs index 58d90f3..c9a5ac3 100644 --- a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/PedidoServiceTests.cs +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service.Tests/PedidoServiceTests.cs @@ -108,25 +108,20 @@ public void Registrar(string pedidoCriado, object toString) } private static PedidoService CreateService( - FakeClienteRepository clienteRepo, FakePedidoRepository pedidoRepo, FakeEstoqueService estoque, FakePagamentoService pagamento, - FakeEmailService email, FakeNotificacaoService notificacao, - FakeLogger logger, FakeFreteService frete, FakeMapper mapper, FakeAuditoria auditoria) { return new PedidoService( - clienteRepo, + clientecliente, pedidoRepo, estoque, pagamento, - email, notificacao, - logger, frete, mapper, auditoria); diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoDto.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Dto/PedidoDto.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoDto.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Dto/PedidoDto.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Client.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Model/Client.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Client.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Model/Client.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Pedido.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Model/Pedido.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Pedido.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Model/Pedido.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IClienteRepository.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Repository/IClienteRepository.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IClienteRepository.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Repository/IClienteRepository.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPedidoRepository.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Repository/IPedidoRepository.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPedidoRepository.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Repository/IPedidoRepository.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IAuditoriaService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IAuditoriaService.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IAuditoriaService.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IAuditoriaService.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IClienteService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IClienteService.cs new file mode 100644 index 0000000..df2739a --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IClienteService.cs @@ -0,0 +1,33 @@ +namespace CooperaSharp.Service; + +public interface IClienteService +{ + void EnviarConfirmacao(Client cliente); + (bool isValid, Client result) ObterPorId(int dtoClienteId); +} + +public class ClienteService : IClienteService +{ + private readonly IEmailService _emailService; + private readonly IClienteRepository _clienteRepository; + private ILogger _logger; + + public ClienteService(IClienteRepository _clienteRepository, IEmailService emailService, ILogger logger) + { + _emailService = emailService; + _logger = logger; + } + + public void EnviarConfirmacao(Client cliente) => _emailService.EnviarConfirmacao(cliente.Email); + public (bool isValid, Client result) ObterPorId(int clienteId) + { + var cliente = _clienteRepository.ObterPorId(clienteId); + if (cliente == null) + { + _logger.Log(Validation.ClienteNaoEncontrado); + return (false, null); + } + + return (false, cliente); + } +} \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEmailService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IEmailService.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEmailService.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IEmailService.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEstoqueService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IEstoqueService.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IEstoqueService.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IEstoqueService.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IFreteService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IFreteService.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IFreteService.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IFreteService.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/INotificacaoService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/INotificacaoService.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/INotificacaoService.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/INotificacaoService.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPagamentoService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IPagamentoService.cs similarity index 100% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/IPagamentoService.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/IPagamentoService.cs diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoService.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/PedidoService.cs similarity index 70% rename from CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoService.cs rename to CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/PedidoService.cs index 0a8d5f0..a48f57e 100644 --- a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/PedidoService.cs +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Service/PedidoService.cs @@ -2,49 +2,41 @@ namespace CooperaSharp.Service; public class PedidoService { - private readonly IClienteRepository _clienteRepository; private readonly IPedidoRepository _pedidoRepository; private readonly IEstoqueService _estoqueService; private readonly IPagamentoService _pagamentoService; - private readonly IEmailService _emailService; private readonly INotificacaoService _notificacaoService; - private readonly ILogger _logger; private readonly IFreteService _freteService; private readonly IMapper _mapper; private readonly IAuditoriaService _auditoriaService; + private readonly IClienteService _clienteService; public PedidoService( - IClienteRepository clienteRepository, + IClienteService clienteService, IPedidoRepository pedidoRepository, IEstoqueService estoqueService, IPagamentoService pagamentoService, - IEmailService emailService, INotificacaoService notificacaoService, - ILogger logger, IFreteService freteService, IMapper mapper, IAuditoriaService auditoriaService) { - _clienteRepository = clienteRepository; + _clienteService = clienteService; _pedidoRepository = pedidoRepository; _estoqueService = estoqueService; _pagamentoService = pagamentoService; - _emailService = emailService; _notificacaoService = notificacaoService; - _logger = logger; _freteService = freteService; _mapper = mapper; _auditoriaService = auditoriaService; } - public void ProcessarPedido(PedidoDto dto) + public (bool isValid, string descrition) ProcessarPedido(PedidoDto dto) { - var cliente = _clienteRepository.ObterPorId(dto.ClienteId); - - if (cliente == null) + var clienteValido = _clienteService.ObterPorId(dto.ClienteId); + if (!clienteValido.isValid) { - _logger.Log("Cliente não encontrado"); - return; + return (false, Validation.ClienteNaoEncontrado); } var pedido = _mapper.Map(dto); @@ -59,12 +51,14 @@ public void ProcessarPedido(PedidoDto dto) if (!pagamentoOk) { _notificacaoService.Enviar("Falha no pagamento"); - return; + return (false, "Falha no pagamento"); } _pedidoRepository.Salvar(pedido); - _emailService.EnviarConfirmacao(cliente.Email); + _clienteService.EnviarConfirmacao(clienteValido.result); _auditoriaService.Registrar("Pedido criado", pedido.Id.ToString()); + + return (true, "Sucesso"); } } \ No newline at end of file diff --git a/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Validation.cs b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Validation.cs new file mode 100644 index 0000000..42547a6 --- /dev/null +++ b/CooperaSharp/CooperaSharp.Service/CooperaSharp.Service/Validation.cs @@ -0,0 +1,6 @@ +namespace CooperaSharp.Service; + +public static class Validation +{ + public const string ClienteNaoEncontrado = "Cliente não encontrado"; +} \ No newline at end of file