init
This commit is contained in:
17
OpenCand.ETL/Repository/BaseRepository.cs
Normal file
17
OpenCand.ETL/Repository/BaseRepository.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql;
|
||||
|
||||
namespace OpenCand.Repository
|
||||
{
|
||||
public abstract class BaseRepository
|
||||
{
|
||||
protected string ConnectionString { get; private set; }
|
||||
protected NpgsqlConnection? Connection { get; private set; }
|
||||
|
||||
public BaseRepository(IConfiguration configuration)
|
||||
{
|
||||
ConnectionString = configuration["DatabaseSettings:ConnectionString"] ??
|
||||
throw new ArgumentNullException("Connection string not found in configuration");
|
||||
}
|
||||
}
|
||||
}
|
37
OpenCand.ETL/Repository/BemCandidatoRepository.cs
Normal file
37
OpenCand.ETL/Repository/BemCandidatoRepository.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql;
|
||||
using OpenCand.Core.Models;
|
||||
|
||||
namespace OpenCand.Repository
|
||||
{
|
||||
public class BemCandidatoRepository : BaseRepository
|
||||
{
|
||||
public BemCandidatoRepository(IConfiguration configuration) : base(configuration)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task AddBemCandidatoAsync(BemCandidato bemCandidato)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
await connection.ExecuteAsync(@"
|
||||
INSERT INTO bem_candidato (idcandidato, ano, ordembem, tipobem, descricao, valor)
|
||||
VALUES (@idcandidato, @ano, @ordembem, @tipobem, @descricao, @valor)
|
||||
ON CONFLICT (idcandidato, ano, ordembem) DO UPDATE SET
|
||||
tipobem = EXCLUDED.tipobem,
|
||||
descricao = EXCLUDED.descricao,
|
||||
valor = EXCLUDED.valor;",
|
||||
new
|
||||
{
|
||||
idcandidato = bemCandidato.IdCandidato,
|
||||
ano = bemCandidato.Ano,
|
||||
ordembem = bemCandidato.OrdemBem,
|
||||
tipobem = bemCandidato.TipoBem,
|
||||
descricao = bemCandidato.Descricao,
|
||||
valor = bemCandidato.Valor
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
122
OpenCand.ETL/Repository/CandidatoRepository.cs
Normal file
122
OpenCand.ETL/Repository/CandidatoRepository.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql;
|
||||
using OpenCand.Core.Models;
|
||||
|
||||
namespace OpenCand.Repository
|
||||
{
|
||||
public class CandidatoRepository : BaseRepository
|
||||
{
|
||||
public CandidatoRepository(IConfiguration configuration) : base(configuration)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task AddCandidatoAsync(Candidato candidato)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
await connection.ExecuteAsync(@"
|
||||
INSERT INTO candidato (idcandidato, cpf, nome, datanascimento, email, sexo, estadocivil, escolaridade, ocupacao)
|
||||
VALUES (@idcandidato, @cpf, @nome, @datanascimento, @email, @sexo, @estadocivil, @escolaridade, @ocupacao)
|
||||
ON CONFLICT (idcandidato) DO UPDATE SET
|
||||
cpf = EXCLUDED.cpf,
|
||||
nome = EXCLUDED.nome,
|
||||
datanascimento = EXCLUDED.datanascimento,
|
||||
email = EXCLUDED.email,
|
||||
sexo = EXCLUDED.sexo,
|
||||
estadocivil = EXCLUDED.estadocivil,
|
||||
escolaridade = EXCLUDED.escolaridade,
|
||||
ocupacao = EXCLUDED.ocupacao;",
|
||||
new
|
||||
{
|
||||
idcandidato = candidato.IdCandidato,
|
||||
cpf = candidato.Cpf,
|
||||
nome = candidato.Nome,
|
||||
datanascimento = candidato.DataNascimento,
|
||||
email = candidato.Email,
|
||||
sexo = candidato.Sexo,
|
||||
estadocivil = candidato.EstadoCivil,
|
||||
escolaridade = candidato.Escolaridade,
|
||||
ocupacao = candidato.Ocupacao
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async Task AddCandidatoMappingAsync(CandidatoMapping candidatoMapping)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
await connection.ExecuteAsync(@"
|
||||
INSERT INTO candidato_mapping (idcandidato, cpf, nome, sqcandidato, ano, tipoeleicao, siglauf, nomeue, cargo, nrcandidato, resultado)
|
||||
VALUES (@idcandidato, @cpf, @nome, @sqcandidato, @ano, @tipoeleicao, @siglauf, @nomeue, @cargo, @nrcandidato, @resultado);",
|
||||
new
|
||||
{
|
||||
idcandidato = candidatoMapping.IdCandidato,
|
||||
cpf = candidatoMapping.Cpf,
|
||||
nome = candidatoMapping.Nome,
|
||||
sqcandidato = candidatoMapping.SqCandidato,
|
||||
ano = candidatoMapping.Ano,
|
||||
tipoeleicao = candidatoMapping.TipoEleicao,
|
||||
siglauf = candidatoMapping.SiglaUF,
|
||||
nomeue = candidatoMapping.NomeUE,
|
||||
nrcandidato = candidatoMapping.NrCandidato,
|
||||
cargo = candidatoMapping.Cargo,
|
||||
resultado = candidatoMapping.Resultado
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<CandidatoMapping>?> GetCandidatoMappingByCpf(string cpf)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var query = @"
|
||||
SELECT idcandidato, cpf, nome, sqcandidato, ano, tipoeleicao, siglauf, nomeue, cargo, nrcandidato, resultado
|
||||
FROM candidato_mapping
|
||||
WHERE cpf = @cpf";
|
||||
return (await connection.QueryAsync<CandidatoMapping>(query, new { cpf })).AsList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<CandidatoMapping>?> GetCandidatoMappingByNome(string nome)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var query = @"
|
||||
SELECT idcandidato, cpf, nome, sqcandidato, ano, tipoeleicao, siglauf, nomeue, cargo, nrcandidato, resultado
|
||||
FROM candidato_mapping
|
||||
WHERE nome = @nome";
|
||||
return (await connection.QueryAsync<CandidatoMapping>(query, new { nome })).AsList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CandidatoMapping?> GetIdCandidatoBySqCandidato(string sqCandidato, int ano, string siglauf, string nomeue)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var query = @"
|
||||
SELECT idcandidato
|
||||
FROM candidato_mapping
|
||||
WHERE sqcandidato = @sqCandidato AND
|
||||
ano = @ano AND
|
||||
siglauf = @siglauf AND
|
||||
nomeue = @nomeue";
|
||||
return await connection.QueryFirstOrDefaultAsync<CandidatoMapping>(query, new { sqCandidato, ano, siglauf, nomeue });
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CandidatoMapping?> GetIdCandidatoBySqCandidato(string sqCandidato, int ano, string siglauf)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var query = @"
|
||||
SELECT idcandidato
|
||||
FROM candidato_mapping
|
||||
WHERE sqcandidato = @sqCandidato AND
|
||||
ano = @ano AND
|
||||
siglauf = @siglauf";
|
||||
return await connection.QueryFirstOrDefaultAsync<CandidatoMapping>(query, new { sqCandidato, ano, siglauf });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
OpenCand.ETL/Repository/RedeSocialRepository.cs
Normal file
34
OpenCand.ETL/Repository/RedeSocialRepository.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql;
|
||||
using OpenCand.Core.Models;
|
||||
|
||||
namespace OpenCand.Repository
|
||||
{
|
||||
public class RedeSocialRepository : BaseRepository
|
||||
{
|
||||
public RedeSocialRepository(IConfiguration configuration) : base(configuration)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task AddRedeSocialAsync(RedeSocial redeSocial)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
await connection.ExecuteAsync(@"
|
||||
INSERT INTO rede_social (idcandidato, rede, siglauf, ano, link)
|
||||
VALUES (@idcandidato, @rede, @siglauf, @ano, @link)
|
||||
ON CONFLICT (idcandidato, rede, siglauf, ano) DO UPDATE SET
|
||||
link = EXCLUDED.link;",
|
||||
new
|
||||
{
|
||||
idcandidato = redeSocial.IdCandidato,
|
||||
rede = redeSocial.Rede,
|
||||
siglauf = redeSocial.SiglaUF,
|
||||
ano = redeSocial.Ano,
|
||||
link = redeSocial.Link
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user