Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Application/DTO/Carteira.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DesignPatternSamples.Application.DTO
{
public class Carteira
{
public string Numero { get; set; }
public string UF { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/Application/DTO/PontosCarteira.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
28 changes: 28 additions & 0 deletions src/Application/Decorators/DetranPontosCarteiraDecorator.cs
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<PontosCarteira>> ConsultarPontos(Carteira carteira)
{
return Task.FromResult(_Cache.GetOrCreate($"{carteira.UF}_{carteira.Numero}", () => _Inner.ConsultarPontos(carteira).Result));
}
}
}
24 changes: 24 additions & 0 deletions src/Application/Implementations/DetranPontosCarteiraServices.cs
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<PontosCarteira>> ConsultarPontos(Carteira Carteira)
{
IDetranPontosCarteiraRepository repository = _Factory.Create(carteira.UF);
return repository.ConsultarPontos(carteira);
}
}
}
10 changes: 10 additions & 0 deletions src/Application/Repository/IDetranPontosCarteiraFactory.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 11 additions & 0 deletions src/Application/Repository/IDetranPontosCarteiraRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using DesignPatternSamples.Application.DTO;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DesignPatternSamples.Application.Repository
{
public interface IDetranPontosCarteiraRepository
{
Task<IEnumerable<PontosCarteira>> ConsultarPontos(Carteira carteira);
}
}
11 changes: 11 additions & 0 deletions src/Application/Services/IDetranPontosCarteiraService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using DesignPatternSamples.Application.DTO;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DesignPatternSamples.Application.Services
{
public interface IDetranPontosCarteiraService
{
Task<IEnumerable<PontosCarteira>> ConsultarPontos(Carteira carteira);
}
}
9 changes: 9 additions & 0 deletions src/Domain/Repository/IDetranPontosCarteira.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace DesignPatternSamples.Domain.Repository.Detran
{
public interface IDetranPontosCarteira
{
Task<PontosCarteira> ConsultarPontos(Carteira carteira);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public DependencyInjectionFixture()
.AddTransient<DetranSPVerificadorDebitosRepository>()
.AddTransient<DetranRJVerificadorDebitosRepository>()
.AddTransient<DetranRSVerificadorDebitosRepository>()
.AddSingleton<IDetranVerificadorDebitosFactory, DetranVerificadorDebitosFactory>();
.AddSingleton<IDetranVerificadorDebitosFactory, DetranVerificadorDebitosFactory, IDetranPontosCarteiraFactory, DetranPontosCarteiraFactory>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O ideal é criar uma nova linha.


#region IConfiguration
services.AddTransient<IConfiguration>((services) =>
Expand Down
19 changes: 19 additions & 0 deletions src/Infra.Repository.Detran.Tests/DetranPontosCarteiraFactory.cs
Original file line number Diff line number Diff line change
@@ -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<DependencyInjectionFixture>
{
private readonly IDetranPontosCarteiraFactory _Factory;

public DetranPontosCarteiraFactoryTests(DependencyInjectionFixture dependencyInjectionFixture)
{
var serviceProvider = dependencyInjectionFixture.ServiceProvider;
_Factory = serviceProvider.GetService<IDetranPontosCarteiraFactory>();
}
}
}
39 changes: 39 additions & 0 deletions src/Infra.Repository.Detran/DetranPontosCarteiraFactory.cs
Original file line number Diff line number Diff line change
@@ -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<string, Type> _Repositories = new Dictionary<string, Type>();

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;
}
}
}
19 changes: 19 additions & 0 deletions src/Infra.Repository.Detran/DetranPontosCarteiraRepository.cs
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<PontosCarteira>> ConsultarPontos(Carteira carteira)
{
var html = await RealizarAcesso(carteira);
return await PadronizarResultado(html);
}

protected abstract Task<string> RealizarAcesso(Carteira carteira);
protected abstract Task<IEnumerable<PontosCarteira>> PadronizarResultado(string html);
}
}
37 changes: 37 additions & 0 deletions src/WebAPI/Controllers/Detran/PontosController.cs
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<PontosCarteiraModel>>), StatusCodes.Status200OK)]
public async Task<ActionResult> Get([FromQuery]CarteiraModel model)
{
var pontos = await _DetranPontosCarteiraServices.ConsultarPontos(_Mapper.Map<Carteira>(model));

var result = new SuccessResultModel<IEnumerable<PontosCarteiraModel>>(_Mapper.Map<IEnumerable<PontosCarteiraModel>>(pontos));

return Ok(result);
}
}
}
8 changes: 8 additions & 0 deletions src/WebAPI/Models/Detran/CarteiraModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DesignPatternSamples.WebAPI.Models.Detran
{
public class CarteiraModel
{
public string Numero { get; set; }
public string UF { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/WebAPI/Models/Detran/PontosCarteiraModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}