-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from fga-eps-mds/release/importacao_planilha
Release/importacao planilha
- Loading branch information
Showing
17 changed files
with
8,355 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,59 @@ | ||
using dominio; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using service; | ||
using service.Interfaces; | ||
using System; | ||
using System.IO; | ||
|
||
namespace app.Controllers | ||
{ | ||
[ApiController] | ||
[Route("api/rodovia")] | ||
public class RodoviaController : ControllerBase | ||
{ | ||
private readonly IRodoviaService rodoviaService; | ||
|
||
public RodoviaController(IRodoviaService rodoviaService) | ||
{ | ||
this.rodoviaService = rodoviaService; | ||
} | ||
|
||
[Consumes("multipart/form-data")] | ||
[HttpPost("cadastrarRodoviaPlanilha")] | ||
public async Task<IActionResult> EnviarPlanilha(IFormFile arquivo) | ||
{ | ||
try | ||
{ | ||
if (arquivo == null || arquivo.Length == 0) | ||
return BadRequest("Nenhum arquivo enviado."); | ||
|
||
|
||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
await arquivo.CopyToAsync(memoryStream); | ||
memoryStream.Seek(0, SeekOrigin.Begin); | ||
|
||
if (rodoviaService.SuperaTamanhoMaximo(memoryStream)) | ||
{ | ||
return StatusCode(406, "Tamanho máximo de arquivo ultrapassado!"); | ||
} | ||
} | ||
|
||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
await arquivo.CopyToAsync(memoryStream); | ||
memoryStream.Seek(0, SeekOrigin.Begin); | ||
rodoviaService.CadastrarRodoviaViaPlanilha(memoryStream); | ||
} | ||
|
||
return Ok(); | ||
} | ||
catch (Exception) | ||
{ | ||
return StatusCode(StatusCodes.Status500InternalServerError, "Arquivo incompatível"); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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,25 @@ | ||
namespace dominio | ||
{ | ||
public class RodoviaDTO | ||
{ | ||
|
||
public int AnoApuracao { get; set; } | ||
public string SiglaUF { get; set; } | ||
public int NumeroRodovia { get; set; } | ||
public string TipoTrecho { get; set; } | ||
public string CodigoSNV { get; set; } | ||
public string LocalInicioFim { get; set; } | ||
public double KmInicial { get; set; } | ||
public double KmFinal { get; set; } | ||
public double Extensao { get; set; } | ||
public string Superficie { get; set; } | ||
public string? FederalCoincidente { get; set; } | ||
public string? EstadualCoincidente { get; set; } | ||
public string? SuperficieEstadual { get; set; } | ||
public bool MP082 { get; set; } | ||
public string ConcessaoConvenio { get; set; } | ||
|
||
} | ||
|
||
|
||
} |
This file contains 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,14 @@ | ||
using dominio; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace repositorio.Interfaces | ||
{ | ||
public interface IRodoviaRepositorio | ||
{ | ||
public void CadastrarRodovia(RodoviaDTO rodoviaDTO); | ||
} | ||
} |
This file contains 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,48 @@ | ||
using Dapper; | ||
using dominio; | ||
using dominio.Enums; | ||
using repositorio.Contexto; | ||
using repositorio.Interfaces; | ||
using static repositorio.Contexto.ResolverContexto; | ||
|
||
namespace repositorio | ||
{ | ||
public class RodoviaRepositorio : IRodoviaRepositorio | ||
{ | ||
private readonly IContexto contexto; | ||
|
||
public RodoviaRepositorio(ResolverContextoDelegate resolverContexto) | ||
{ | ||
contexto = resolverContexto(ContextoBancoDeDados.Postgresql); | ||
} | ||
|
||
public void CadastrarRodovia(RodoviaDTO rodoviaDTO) | ||
{ | ||
var sqlInserirRodovia = @"INSERT INTO public.rodovia( ano_apuracao, sigla_uf, numero_rodovia, tipo_trecho, codigo_snv, | ||
local_inicio_fim, km_inicial, km_final, extensao, superficie, federal_coincidente, estadual_coincidente, | ||
superficie_estadual, mp082, concessao_convenio) | ||
VALUES( @AnoApuracao, @SiglaUF, @NumeroRodovia, @TipoTrecho, @CodigoSNV, @LocalInicioFim, @KmInicial, @KmFinal, | ||
@Extensao, @Superficie, @FederalCoincidente, @EstadualCoincidente, @SuperficieEstadual, @MP082, @ConcessaoConvenio)"; | ||
var parametrosRodovia = new | ||
{ | ||
AnoApuracao = rodoviaDTO.AnoApuracao, | ||
SiglaUF = rodoviaDTO.SiglaUF, | ||
NumeroRodovia = rodoviaDTO.NumeroRodovia, | ||
TipoTrecho = rodoviaDTO.TipoTrecho, | ||
CodigoSNV = rodoviaDTO.CodigoSNV, | ||
LocalInicioFim = rodoviaDTO.LocalInicioFim, | ||
KmInicial = rodoviaDTO.KmInicial, | ||
KmFinal = rodoviaDTO.KmFinal, | ||
Extensao = rodoviaDTO.Extensao, | ||
Superficie = rodoviaDTO.Superficie, | ||
FederalCoincidente = rodoviaDTO.FederalCoincidente, | ||
EstadualCoincidente = rodoviaDTO.EstadualCoincidente, | ||
SuperficieEstadual = rodoviaDTO.SuperficieEstadual, | ||
MP082 = rodoviaDTO.MP082, | ||
ConcessaoConvenio = rodoviaDTO.ConcessaoConvenio | ||
}; | ||
|
||
contexto?.Conexao.Execute(sqlInserirRodovia, parametrosRodovia); | ||
} | ||
} | ||
} |
This file contains 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.IO; | ||
using System.Collections.Generic; | ||
using dominio; | ||
|
||
namespace service.Interfaces | ||
{ | ||
public interface IRodoviaService | ||
{ | ||
public bool SuperaTamanhoMaximo(MemoryStream planilha); | ||
public void CadastrarRodoviaViaPlanilha(MemoryStream planilha); | ||
} | ||
} |
This file contains 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,86 @@ | ||
using System.IO; | ||
using dominio; | ||
using repositorio.Interfaces; | ||
using service.Interfaces; | ||
using Microsoft.VisualBasic.FileIO; | ||
|
||
|
||
|
||
namespace service | ||
{ | ||
public class RodoviaService : IRodoviaService | ||
{ | ||
private readonly IRodoviaRepositorio rodoviaRepositorio; | ||
public RodoviaService(IRodoviaRepositorio rodoviaRepositorio) | ||
{ | ||
this.rodoviaRepositorio = rodoviaRepositorio; | ||
} | ||
|
||
public bool SuperaTamanhoMaximo(MemoryStream planilha) | ||
{ | ||
using (var reader = new StreamReader(planilha)) | ||
{ | ||
int tamanho_max = 8000; | ||
int quantidade_rodovias = -1; | ||
|
||
while (reader.ReadLine() != null) { quantidade_rodovias++; } | ||
|
||
return quantidade_rodovias > tamanho_max; | ||
} | ||
} | ||
public void CadastrarRodoviaViaPlanilha(MemoryStream planilha) | ||
{ | ||
|
||
int numero_linha = 2; | ||
|
||
using (var reader = new StreamReader(planilha)) | ||
{ | ||
using (var parser = new TextFieldParser(reader)) | ||
{ | ||
parser.TextFieldType = FieldType.Delimited; | ||
parser.SetDelimiters(","); | ||
|
||
bool primeiralinha = false; | ||
|
||
while (!parser.EndOfData) | ||
{ | ||
string[] linha = parser.ReadFields(); | ||
if (!primeiralinha) | ||
{ | ||
primeiralinha = true; | ||
continue; | ||
} | ||
|
||
RodoviaDTO rodovia = new RodoviaDTO(); | ||
rodovia.AnoApuracao = int.Parse(linha[0]); | ||
rodovia.SiglaUF = linha[1]; | ||
rodovia.NumeroRodovia = int.Parse(linha[2]); | ||
rodovia.TipoTrecho = linha[3]; | ||
rodovia.CodigoSNV = linha[4]; | ||
rodovia.LocalInicioFim = linha[5]; | ||
rodovia.KmInicial = double.Parse(linha[6]); | ||
rodovia.KmFinal = double.Parse(linha[7]); | ||
rodovia.Extensao = double.Parse(linha[8]); | ||
rodovia.Superficie = linha[9]; | ||
rodovia.FederalCoincidente = linha[10]; | ||
rodovia.EstadualCoincidente = linha[11]; | ||
rodovia.SuperficieEstadual = linha[12]; | ||
rodovia.MP082 = (linha[13] != "Não"); | ||
rodovia.ConcessaoConvenio = linha[14]; | ||
|
||
rodoviaRepositorio.CadastrarRodovia(rodovia); | ||
numero_linha++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} | ||
} | ||
|
||
|
||
|
This file contains 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
This file contains 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,54 @@ | ||
using Moq; | ||
using service; | ||
using repositorio.Interfaces; | ||
using dominio; | ||
|
||
namespace test.RodoviaServiceTests | ||
{ | ||
public class RodoviaServiceTest | ||
{ | ||
private readonly RodoviaService rodoviaService; | ||
private readonly Mock<IRodoviaRepositorio> mockRodoviaRepositorio; | ||
private readonly string caminhoDoArquivo; | ||
public RodoviaServiceTest() | ||
{ | ||
caminhoDoArquivo = "..\\..\\..\\..\\test\\Stub\\planilhaExemplo.csv"; | ||
mockRodoviaRepositorio = new(); | ||
rodoviaService = new RodoviaService(mockRodoviaRepositorio.Object); | ||
} | ||
[Fact] | ||
public void CadastrarRodoviaViaPlanilha_QuandoPlanilhaForPassadaENaoTiverDado_NaoDevePassarPeloRepositorio() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => rodoviaService.CadastrarRodoviaViaPlanilha(null)); | ||
} | ||
[Fact] | ||
public void CadastrarRodoviaViaPlanilha_QuandoForChamado_DeveChamarORepositorio() | ||
{; | ||
var memoryStream = new MemoryStream(File.ReadAllBytes(caminhoDoArquivo)); | ||
|
||
rodoviaService.CadastrarRodoviaViaPlanilha(memoryStream); | ||
mockRodoviaRepositorio.Verify(mock => mock.CadastrarRodovia(It.IsAny<RodoviaDTO>()), Times.Exactly(3)); | ||
} | ||
[Fact] | ||
public void CadastrarRodoviaViaPlanilha_QuandForChamado_DeveCadastrarRodovias() | ||
{ | ||
var memoryStream = new MemoryStream(File.ReadAllBytes(caminhoDoArquivo)); | ||
|
||
rodoviaService.CadastrarRodoviaViaPlanilha(memoryStream); | ||
mockRodoviaRepositorio.Verify(mock => mock.CadastrarRodovia(It.IsAny<RodoviaDTO>()), Times.Exactly(3)); | ||
} | ||
|
||
[Fact] | ||
public void SuperaTamanhoMaximo_QuandoPlanilhaComTamanhoMaiorQueOMaximoForPassada_DeveRetornarTrue() | ||
{ | ||
string caminhoArquivo = "..\\..\\..\\..\\test\\Stub\\planilha_tamanho_max.csv"; | ||
|
||
MemoryStream memoryStream = new MemoryStream(File.ReadAllBytes(caminhoArquivo)); | ||
|
||
bool resultado = rodoviaService.SuperaTamanhoMaximo(memoryStream); | ||
|
||
Assert.True(resultado); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.