From 721d37d0f59cb8896fc43a1635aa35f3066c181c Mon Sep 17 00:00:00 2001 From: Malu23 Date: Sat, 26 Dec 2020 00:57:28 -0300 Subject: [PATCH] =?UTF-8?q?Inser=C3=A7=C3=A3o=20de=20Pontos=20na=20Carteir?= =?UTF-8?q?a=20com=20Design=20Patterns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Application/DTO/Carteira.cs | 8 ++++ src/Application/DTO/PontosCarteira.cs | 12 ++++++ .../DetranPontosCarteiraDecorator.cs | 28 +++++++++++++ .../DetranPontosCarteiraServices.cs | 24 ++++++++++++ .../IDetranPontosCarteiraFactory.cs | 10 +++++ .../IDetranPontosCarteiraRepository.cs | 11 ++++++ .../Services/IDetranPontosCarteiraService.cs | 11 ++++++ .../Repository/IDetranPontosCarteira.cs | 9 +++++ .../DependencyInjectionFixture.cs | 2 +- .../DetranPontosCarteiraFactory.cs | 19 +++++++++ .../DetranPontosCarteiraFactory.cs | 39 +++++++++++++++++++ .../DetranPontosCarteiraRepository.cs | 19 +++++++++ .../Controllers/Detran/PontosController.cs | 37 ++++++++++++++++++ src/WebAPI/Models/Detran/CarteiraModel.cs | 8 ++++ .../Models/Detran/PontosCarteiraModel.cs | 11 ++++++ 15 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 src/Application/DTO/Carteira.cs create mode 100644 src/Application/DTO/PontosCarteira.cs create mode 100644 src/Application/Decorators/DetranPontosCarteiraDecorator.cs create mode 100644 src/Application/Implementations/DetranPontosCarteiraServices.cs create mode 100644 src/Application/Repository/IDetranPontosCarteiraFactory.cs create mode 100644 src/Application/Repository/IDetranPontosCarteiraRepository.cs create mode 100644 src/Application/Services/IDetranPontosCarteiraService.cs create mode 100644 src/Domain/Repository/IDetranPontosCarteira.cs create mode 100644 src/Infra.Repository.Detran.Tests/DetranPontosCarteiraFactory.cs create mode 100644 src/Infra.Repository.Detran/DetranPontosCarteiraFactory.cs create mode 100644 src/Infra.Repository.Detran/DetranPontosCarteiraRepository.cs create mode 100644 src/WebAPI/Controllers/Detran/PontosController.cs create mode 100644 src/WebAPI/Models/Detran/CarteiraModel.cs create mode 100644 src/WebAPI/Models/Detran/PontosCarteiraModel.cs diff --git a/src/Application/DTO/Carteira.cs b/src/Application/DTO/Carteira.cs new file mode 100644 index 0000000..4d5cdfc --- /dev/null +++ b/src/Application/DTO/Carteira.cs @@ -0,0 +1,8 @@ +namespace DesignPatternSamples.Application.DTO +{ + public class Carteira + { + public string Numero { get; set; } + public string UF { get; set; } + } +} \ No newline at end of file diff --git a/src/Application/DTO/PontosCarteira.cs b/src/Application/DTO/PontosCarteira.cs new file mode 100644 index 0000000..b81b1cd --- /dev/null +++ b/src/Application/DTO/PontosCarteira.cs @@ -0,0 +1,12 @@ +using System; + +namespace DesignPatternSamples.Application.DTO +{ + [Serializable] + public class PontosCarteira + { + public DateTime DataOcorrencia { get; set; } + public string Descricao { get; set; } + public double Quantidade { get; set; } + } +} \ No newline at end of file diff --git a/src/Application/Decorators/DetranPontosCarteiraDecorator.cs b/src/Application/Decorators/DetranPontosCarteiraDecorator.cs new file mode 100644 index 0000000..2b4c51b --- /dev/null +++ b/src/Application/Decorators/DetranPontosCarteiraDecorator.cs @@ -0,0 +1,28 @@ +using DesignPatternSamples.Application.DTO; +using DesignPatternSamples.Application.Services; +using Microsoft.Extensions.Caching.Distributed; +using System.Collections.Generic; +using System.Threading.Tasks; +using Workbench.IDistributedCache.Extensions; + +namespace DesignPatternSamples.Application.Decorators +{ + public class DetranPontosCarteiraDecorator : IDetranPontosCarteiraService + { + private readonly DetranPontosCarteiraDecorator _Inner; + private readonly IDistributedCache _Cache; + + public DetranPontosCarteiraDecorator( + DetranPontosCarteiraDecorator inner, + IDistributedCache cache) + { + _Inner = inner; + _Cache = cache; + } + + public Task> ConsultarPontos(Carteira carteira) + { + return Task.FromResult(_Cache.GetOrCreate($"{carteira.UF}_{carteira.Numero}", () => _Inner.ConsultarPontos(carteira).Result)); + } + } +} \ No newline at end of file diff --git a/src/Application/Implementations/DetranPontosCarteiraServices.cs b/src/Application/Implementations/DetranPontosCarteiraServices.cs new file mode 100644 index 0000000..b3cad06 --- /dev/null +++ b/src/Application/Implementations/DetranPontosCarteiraServices.cs @@ -0,0 +1,24 @@ +using DesignPatternSamples.Application.DTO; +using DesignPatternSamples.Application.Repository; +using DesignPatternSamples.Application.Services; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace DesignPatternSamples.Application.Implementations +{ + public class DetranPontosCarteiraServices : IDetranPontosCarteiraService + { + private readonly IDetranPontosCarteiraFactory _Factory; + + public DetranPontosCarteiraServices(IDetranPontosCarteiraFactory factory) + { + _Factory = factory; + } + + public Task> ConsultarPontos(Carteira Carteira) + { + IDetranPontosCarteiraRepository repository = _Factory.Create(carteira.UF); + return repository.ConsultarPontos(carteira); + } + } +} \ No newline at end of file diff --git a/src/Application/Repository/IDetranPontosCarteiraFactory.cs b/src/Application/Repository/IDetranPontosCarteiraFactory.cs new file mode 100644 index 0000000..f80413c --- /dev/null +++ b/src/Application/Repository/IDetranPontosCarteiraFactory.cs @@ -0,0 +1,10 @@ +using System; + +namespace DesignPatternSamples.Application.Repository +{ + public interface IDetranPontosCarteiraFactory + { + public IDetranPontosCarteiraFactory Register(string UF, Type repository); + public IDetranPontosCarteiraRepository Create(string UF); + } +} \ No newline at end of file diff --git a/src/Application/Repository/IDetranPontosCarteiraRepository.cs b/src/Application/Repository/IDetranPontosCarteiraRepository.cs new file mode 100644 index 0000000..65a2ee8 --- /dev/null +++ b/src/Application/Repository/IDetranPontosCarteiraRepository.cs @@ -0,0 +1,11 @@ +using DesignPatternSamples.Application.DTO; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace DesignPatternSamples.Application.Repository +{ + public interface IDetranPontosCarteiraRepository + { + Task> ConsultarPontos(Carteira carteira); + } +} diff --git a/src/Application/Services/IDetranPontosCarteiraService.cs b/src/Application/Services/IDetranPontosCarteiraService.cs new file mode 100644 index 0000000..d50e75d --- /dev/null +++ b/src/Application/Services/IDetranPontosCarteiraService.cs @@ -0,0 +1,11 @@ +using DesignPatternSamples.Application.DTO; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace DesignPatternSamples.Application.Services +{ + public interface IDetranPontosCarteiraService + { + Task> ConsultarPontos(Carteira carteira); + } +} diff --git a/src/Domain/Repository/IDetranPontosCarteira.cs b/src/Domain/Repository/IDetranPontosCarteira.cs new file mode 100644 index 0000000..93357e9 --- /dev/null +++ b/src/Domain/Repository/IDetranPontosCarteira.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace DesignPatternSamples.Domain.Repository.Detran +{ + public interface IDetranPontosCarteira + { + Task ConsultarPontos(Carteira carteira); + } +} diff --git a/src/Infra.Repository.Detran.Tests/DependencyInjectionFixture.cs b/src/Infra.Repository.Detran.Tests/DependencyInjectionFixture.cs index fae848b..1d714ba 100644 --- a/src/Infra.Repository.Detran.Tests/DependencyInjectionFixture.cs +++ b/src/Infra.Repository.Detran.Tests/DependencyInjectionFixture.cs @@ -18,7 +18,7 @@ public DependencyInjectionFixture() .AddTransient() .AddTransient() .AddTransient() - .AddSingleton(); + .AddSingleton(); #region IConfiguration services.AddTransient((services) => diff --git a/src/Infra.Repository.Detran.Tests/DetranPontosCarteiraFactory.cs b/src/Infra.Repository.Detran.Tests/DetranPontosCarteiraFactory.cs new file mode 100644 index 0000000..c87eab4 --- /dev/null +++ b/src/Infra.Repository.Detran.Tests/DetranPontosCarteiraFactory.cs @@ -0,0 +1,19 @@ +using DesignPatternSamples.Application.Repository; +using DesignPatternSamples.Infra.Repository.Detran; +using Microsoft.Extensions.DependencyInjection; +using System; +using Xunit; + +namespace DesignPatternsSamples.Infra.Repository.Detran.Tests +{ + public class DetranPontosCarteiraFactoryTests : IClassFixture + { + private readonly IDetranPontosCarteiraFactory _Factory; + + public DetranPontosCarteiraFactoryTests(DependencyInjectionFixture dependencyInjectionFixture) + { + var serviceProvider = dependencyInjectionFixture.ServiceProvider; + _Factory = serviceProvider.GetService(); + } + } +} diff --git a/src/Infra.Repository.Detran/DetranPontosCarteiraFactory.cs b/src/Infra.Repository.Detran/DetranPontosCarteiraFactory.cs new file mode 100644 index 0000000..5772d43 --- /dev/null +++ b/src/Infra.Repository.Detran/DetranPontosCarteiraFactory.cs @@ -0,0 +1,39 @@ +using DesignPatternSamples.Application.Repository; +using System; +using System.Collections.Generic; + +namespace DesignPatternSamples.Infra.Repository.Detran +{ + public class DetranPontosCarteiraFactory : IDetranPontosCarteiraFactory + { + private readonly IServiceProvider _ServiceProvider; + private readonly IDictionary _Repositories = new Dictionary(); + + public DetranPontosCarteiraFactory(IServiceProvider serviceProvider) + { + _ServiceProvider = serviceProvider; + } + + public IDetranPontosCarteiraRepository Create(string UF) + { + IDetranPontosCarteiraRepository result = null; + + if (_Repositories.TryGetValue(UF, out Type type)) + { + result = _ServiceProvider.GetService(type) as IDetranPontosCarteiraRepository; + } + + return result; + } + + public IDetranPontosCarteiraFactory Register(string UF, Type repository) + { + if (!_Repositories.TryAdd(UF, repository)) + { + _Repositories[UF] = repository; + } + + return this; + } + } +} \ No newline at end of file diff --git a/src/Infra.Repository.Detran/DetranPontosCarteiraRepository.cs b/src/Infra.Repository.Detran/DetranPontosCarteiraRepository.cs new file mode 100644 index 0000000..4eab6f1 --- /dev/null +++ b/src/Infra.Repository.Detran/DetranPontosCarteiraRepository.cs @@ -0,0 +1,19 @@ +using DesignPatternSamples.Application.DTO; +using DesignPatternSamples.Application.Repository; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace DesignPatternSamples.Infra.Repository.Detran +{ + public abstract class DetranPontosCarteiraRepositoryCrawlerBase : IDetranPontosCarteiraRepository + { + public async Task> ConsultarPontos(Carteira carteira) + { + var html = await RealizarAcesso(carteira); + return await PadronizarResultado(html); + } + + protected abstract Task RealizarAcesso(Carteira carteira); + protected abstract Task> PadronizarResultado(string html); + } +} \ No newline at end of file diff --git a/src/WebAPI/Controllers/Detran/PontosController.cs b/src/WebAPI/Controllers/Detran/PontosController.cs new file mode 100644 index 0000000..48318e9 --- /dev/null +++ b/src/WebAPI/Controllers/Detran/PontosController.cs @@ -0,0 +1,37 @@ +using AutoMapper; +using DesignPatternSamples.Application.DTO; +using DesignPatternSamples.Application.Services; +using DesignPatternSamples.WebAPI.Models; +using DesignPatternSamples.WebAPI.Models.Detran; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace DesignPatternSamples.WebAPI.Controllers.Detran +{ + [Route("api/Detran/[controller]")] + [ApiController] + public class PontosController : ControllerBase + { + private readonly IMapper _Mapper; + private readonly IDetranPontosCarteiraService _DetranPontosCarteiraServices; + + public PontosController(IMapper mapper, IDetranPontosCarteiraServices detranPontosCarteiraServices) + { + _Mapper = mapper; + _DetranPontosCarteiraServices = detranPontosCarteiraServices; + } + + [HttpGet()] + [ProducesResponseType(typeof(SuccessResultModel>), StatusCodes.Status200OK)] + public async Task Get([FromQuery]CarteiraModel model) + { + var pontos = await _DetranPontosCarteiraServices.ConsultarPontos(_Mapper.Map(model)); + + var result = new SuccessResultModel>(_Mapper.Map>(pontos)); + + return Ok(result); + } + } +} \ No newline at end of file diff --git a/src/WebAPI/Models/Detran/CarteiraModel.cs b/src/WebAPI/Models/Detran/CarteiraModel.cs new file mode 100644 index 0000000..52d932b --- /dev/null +++ b/src/WebAPI/Models/Detran/CarteiraModel.cs @@ -0,0 +1,8 @@ +namespace DesignPatternSamples.WebAPI.Models.Detran +{ + public class CarteiraModel + { + public string Numero { get; set; } + public string UF { get; set; } + } +} \ No newline at end of file diff --git a/src/WebAPI/Models/Detran/PontosCarteiraModel.cs b/src/WebAPI/Models/Detran/PontosCarteiraModel.cs new file mode 100644 index 0000000..1139a1a --- /dev/null +++ b/src/WebAPI/Models/Detran/PontosCarteiraModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace DesignPatternSamples.WebAPI.Models.Detran +{ + public class PontosCarteiraModel + { + public DateTime DataOcorrencia { get; set; } + public string Descricao { get; set; } + public double Quantidade { get; set; } + } +} \ No newline at end of file