init
This commit is contained in:
17
OpenCand.API/Repository/BaseRepository.cs
Normal file
17
OpenCand.API/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");
|
||||
}
|
||||
}
|
||||
}
|
26
OpenCand.API/Repository/BemCandidatoRepository.cs
Normal file
26
OpenCand.API/Repository/BemCandidatoRepository.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Dapper;
|
||||
using Npgsql;
|
||||
using OpenCand.Core.Models;
|
||||
|
||||
namespace OpenCand.Repository
|
||||
{
|
||||
public class BemCandidatoRepository : BaseRepository
|
||||
{
|
||||
public BemCandidatoRepository(IConfiguration configuration) : base(configuration)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<BemCandidato>?> GetBemCandidatoAsync(Guid idcandidato)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var query = @"
|
||||
SELECT * FROM bem_candidato
|
||||
WHERE idcandidato = @idcandidato
|
||||
ORDER BY ano DESC, ordembem ASC;";
|
||||
|
||||
return (await connection.QueryAsync<BemCandidato>(query, new { idcandidato })).AsList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
61
OpenCand.API/Repository/CandidatoRepository.cs
Normal file
61
OpenCand.API/Repository/CandidatoRepository.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Dapper;
|
||||
using Npgsql;
|
||||
using OpenCand.Core.Models;
|
||||
|
||||
namespace OpenCand.Repository
|
||||
{
|
||||
public class CandidatoRepository : BaseRepository
|
||||
{
|
||||
public CandidatoRepository(IConfiguration configuration) : base(configuration)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Candidato>> SearchCandidatosAsync(string query)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
return (await connection.QueryAsync<Candidato>(@"
|
||||
SELECT idcandidato, cpf, nome, datanascimento, email, sexo, estadocivil, escolaridade, ocupacao
|
||||
FROM candidato
|
||||
WHERE nome ILIKE '%' || @query || '%' OR
|
||||
cpf ILIKE '%' || @query || '%' OR
|
||||
email ILIKE '%' || @query || '%'
|
||||
LIMIT 10;",
|
||||
new { query })).AsList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Candidato?> GetCandidatoAsync(Guid idcandidato)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
return await connection.QueryFirstOrDefaultAsync<Candidato>(@"
|
||||
SELECT * FROM candidato
|
||||
WHERE idcandidato = @idcandidato;",
|
||||
new { idcandidato });
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<CandidatoMapping>?> GetCandidatoMappingById(Guid idcandidato)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var query = @"
|
||||
SELECT * FROM candidato_mapping
|
||||
WHERE idcandidato = @idcandidato";
|
||||
return (await connection.QueryAsync<CandidatoMapping>(query, new { idcandidato })).AsList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<RedeSocial>?> GetCandidatoRedeSocialById(Guid idcandidato)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var query = @"
|
||||
SELECT * FROM rede_social
|
||||
WHERE idcandidato = @idcandidato";
|
||||
return (await connection.QueryAsync<RedeSocial>(query, new { idcandidato })).AsList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
OpenCand.API/Repository/OpenCandRepository.cs
Normal file
29
OpenCand.API/Repository/OpenCandRepository.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Dapper;
|
||||
using Npgsql;
|
||||
using OpenCand.Core.Models;
|
||||
using OpenCand.Repository;
|
||||
|
||||
namespace OpenCand.API.Repository
|
||||
{
|
||||
public class OpenCandRepository : BaseRepository
|
||||
{
|
||||
public OpenCandRepository(IConfiguration configuration) : base(configuration)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<OpenCandStats> GetOpenCandStatsAsync()
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var stats = await connection.QueryFirstOrDefaultAsync<OpenCandStats>(@"
|
||||
SELECT
|
||||
(SELECT COUNT(idcandidato) FROM candidato) AS TotalCandidatos,
|
||||
(SELECT COUNT(*) FROM bem_candidato) AS TotalBemCandidatos,
|
||||
(SELECT SUM(valor) FROM bem_candidato) AS TotalValorBemCandidatos,
|
||||
(SELECT COUNT(*) FROM rede_social) AS TotalRedesSociais,
|
||||
(SELECT COUNT(DISTINCT ano) FROM bem_candidato) AS TotalEleicoes;");
|
||||
return stats ?? new OpenCandStats();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user