adding despesas e receitas
This commit is contained in:
@@ -22,6 +22,11 @@ namespace OpenCand.API.Controllers
|
||||
[EnableRateLimiting(RateLimitingConfig.CandidatoSearchPolicy)]
|
||||
public async Task<CandidatoSearchResult> CandidatoSearch([FromQuery] string q)
|
||||
{
|
||||
if (string.IsNullOrEmpty(q) || q.Length == 1)
|
||||
{
|
||||
throw new ArgumentException("Query parameter 'q' cannot be null/empty.", nameof(q));
|
||||
}
|
||||
|
||||
return await openCandService.SearchCandidatosAsync(q);
|
||||
}
|
||||
|
||||
@@ -52,5 +57,17 @@ namespace OpenCand.API.Controllers
|
||||
await Task.Delay(randomWait);
|
||||
return await openCandService.GetCandidatoCpfById(id);
|
||||
}
|
||||
|
||||
[HttpGet("{id}/despesas")]
|
||||
public async Task<DespesasResult> GetCandidatoDespesas([FromRoute] Guid id)
|
||||
{
|
||||
return await openCandService.GetDespesasByIdAndYear(id);
|
||||
}
|
||||
|
||||
[HttpGet("{id}/receitas")]
|
||||
public async Task<ReceitaResult> GetCandidatoReceitas([FromRoute] Guid id)
|
||||
{
|
||||
return await openCandService.GetReceitasByIdAndYear(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -21,4 +21,14 @@ namespace OpenCand.API.Model
|
||||
{
|
||||
public string Cpf { get; set; }
|
||||
}
|
||||
|
||||
public class DespesasResult
|
||||
{
|
||||
public List<Despesa> Despesas { get; set; }
|
||||
}
|
||||
|
||||
public class ReceitaResult
|
||||
{
|
||||
public List<Receita> Receitas { get; set; }
|
||||
}
|
||||
}
|
||||
|
@@ -59,6 +59,7 @@ namespace OpenCand.API
|
||||
builder.Services.AddScoped<OpenCandRepository>();
|
||||
builder.Services.AddScoped<CandidatoRepository>();
|
||||
builder.Services.AddScoped<BemCandidatoRepository>();
|
||||
builder.Services.AddScoped<DespesaReceitaRepository>();
|
||||
builder.Services.AddScoped<OpenCandService>();
|
||||
}
|
||||
}
|
||||
|
42
OpenCand.API/Repository/DespesaReceitaRepository.cs
Normal file
42
OpenCand.API/Repository/DespesaReceitaRepository.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql;
|
||||
using OpenCand.Core.Models;
|
||||
|
||||
namespace OpenCand.Repository
|
||||
{
|
||||
public class DespesaReceitaRepository : BaseRepository
|
||||
{
|
||||
public DespesaReceitaRepository(IConfiguration configuration) : base(configuration)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Despesa>> GetDespesasByCandidatoIdYearAsync(Guid idcandidato)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
return (await connection.QueryAsync<Despesa>(@"
|
||||
SELECT * FROM despesas_candidato
|
||||
WHERE idcandidato = @idcandidato
|
||||
ORDER BY valor DESC;", new
|
||||
{
|
||||
idcandidato
|
||||
})).AsList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Receita>> GetReceitasByCandidatoIdYearAsync(Guid idcandidato)
|
||||
{
|
||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
return (await connection.QueryAsync<Receita>(@"
|
||||
SELECT * FROM receitas_candidato
|
||||
WHERE idcandidato = @idcandidato
|
||||
ORDER BY valor DESC;", new
|
||||
{
|
||||
idcandidato
|
||||
})).AsList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -12,6 +12,7 @@ namespace OpenCand.API.Services
|
||||
private readonly OpenCandRepository openCandRepository;
|
||||
private readonly CandidatoRepository candidatoRepository;
|
||||
private readonly BemCandidatoRepository bemCandidatoRepository;
|
||||
private readonly DespesaReceitaRepository despesaReceitaRepository;
|
||||
private readonly IConfiguration configuration;
|
||||
private readonly FotosSettings fotoSettings;
|
||||
private readonly ILogger<OpenCandService> logger;
|
||||
@@ -20,6 +21,7 @@ namespace OpenCand.API.Services
|
||||
OpenCandRepository openCandRepository,
|
||||
CandidatoRepository candidatoRepository,
|
||||
BemCandidatoRepository bemCandidatoRepository,
|
||||
DespesaReceitaRepository despesaReceitaRepository,
|
||||
IOptions<FotosSettings> fotoSettings,
|
||||
IConfiguration configuration,
|
||||
ILogger<OpenCandService> logger)
|
||||
@@ -27,6 +29,7 @@ namespace OpenCand.API.Services
|
||||
this.openCandRepository = openCandRepository;
|
||||
this.candidatoRepository = candidatoRepository;
|
||||
this.bemCandidatoRepository = bemCandidatoRepository;
|
||||
this.despesaReceitaRepository = despesaReceitaRepository;
|
||||
this.fotoSettings = fotoSettings.Value;
|
||||
this.configuration = configuration;
|
||||
this.logger = logger;
|
||||
@@ -113,5 +116,33 @@ namespace OpenCand.API.Services
|
||||
Cpf = result
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<DespesasResult> GetDespesasByIdAndYear(Guid idcandidato)
|
||||
{
|
||||
var result = await despesaReceitaRepository.GetDespesasByCandidatoIdYearAsync(idcandidato);
|
||||
if (result == null)
|
||||
{
|
||||
return new DespesasResult();
|
||||
}
|
||||
|
||||
return new DespesasResult()
|
||||
{
|
||||
Despesas = result.OrderByDescending(d => d.Ano).ThenByDescending(d => d.Valor).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ReceitaResult> GetReceitasByIdAndYear(Guid idcandidato)
|
||||
{
|
||||
var result = await despesaReceitaRepository.GetReceitasByCandidatoIdYearAsync(idcandidato);
|
||||
if (result == null)
|
||||
{
|
||||
return new ReceitaResult();
|
||||
}
|
||||
|
||||
return new ReceitaResult()
|
||||
{
|
||||
Receitas = result.OrderByDescending(d => d.Ano).ThenByDescending(d => d.Valor).ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user