-
Notifications
You must be signed in to change notification settings - Fork 96
Inserção de Pontos na Carteira com Design Patterns #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maria-luiza-duda
wants to merge
1
commit into
fructuoso:main
Choose a base branch
from
maria-luiza-duda:maria-luiza-duda
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
28
src/Application/Decorators/DetranPontosCarteiraDecorator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
24
src/Application/Implementations/DetranPontosCarteiraServices.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
10
src/Application/Repository/IDetranPontosCarteiraFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
11
src/Application/Repository/IDetranPontosCarteiraRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Infra.Repository.Detran.Tests/DetranPontosCarteiraFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
39
src/Infra.Repository.Detran/DetranPontosCarteiraFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
19
src/Infra.Repository.Detran/DetranPontosCarteiraRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.