using Microsoft.Extensions.Options; using OpenCand.API.Config; using OpenCand.API.Model; using OpenCand.API.Repository; using OpenCand.Core.Models; using OpenCand.Repository; namespace OpenCand.API.Services { public class OpenCandService { private readonly OpenCandRepository openCandRepository; private readonly CandidatoRepository candidatoRepository; private readonly BemCandidatoRepository bemCandidatoRepository; private readonly IConfiguration configuration; private readonly FotosSettings fotoSettings; private readonly ILogger logger; public OpenCandService( OpenCandRepository openCandRepository, CandidatoRepository candidatoRepository, BemCandidatoRepository bemCandidatoRepository, IOptions fotoSettings, IConfiguration configuration, ILogger logger) { this.openCandRepository = openCandRepository; this.candidatoRepository = candidatoRepository; this.bemCandidatoRepository = bemCandidatoRepository; this.fotoSettings = fotoSettings.Value; this.configuration = configuration; this.logger = logger; } public async Task GetOpenCandStatsAsync() { return await openCandRepository.GetOpenCandStatsAsync(); } public async Task SearchCandidatosAsync(string query) { return new CandidatoSearchResult() { Candidatos = await candidatoRepository.SearchCandidatosAsync(query) }; } public async Task GetCandidatoAsync(Guid idcandidato) { var result = await candidatoRepository.GetCandidatoAsync(idcandidato); var eleicoes = await candidatoRepository.GetCandidatoMappingById(idcandidato); foreach (var eleicao in eleicoes) { eleicao.Partido = await candidatoRepository.GetPartidoBySigla(eleicao.Sgpartido); } if (result == null) { throw new KeyNotFoundException($"Candidato with ID {idcandidato} not found."); } if (eleicoes == null || eleicoes.Count == 0) { throw new KeyNotFoundException($"CandidatoMapping for ID {idcandidato} not found."); } var lastEleicao = eleicoes.OrderByDescending(e => e.Ano).First(); result.FotoUrl = $"{fotoSettings.ApiBasePath}/foto_cand{lastEleicao.Ano}_{lastEleicao.SiglaUF}_div/F{lastEleicao.SiglaUF}{lastEleicao.SqCandidato}_div.jpg"; result.Eleicoes = eleicoes.OrderByDescending(e => e.Ano).ToList(); return result; } public async Task GetBemCandidatoById(Guid idcandidato) { var result = await bemCandidatoRepository.GetBemCandidatoAsync(idcandidato); if (result == null) { result = new List(); } return new BemCandidatoResult() { Bens = result.OrderByDescending(r => r.TipoBem).ThenByDescending(r => r.Valor).ToList() }; } public async Task GetCandidatoRedeSocialById(Guid idcandidato) { var result = await candidatoRepository.GetCandidatoRedeSocialById(idcandidato); if (result == null) { result = new List(); } return new RedeSocialResult() { RedesSociais = result.OrderByDescending(r => r.Ano).ToList() }; } public async Task GetCandidatoCpfById(Guid idcandidato) { var result = await candidatoRepository.GetCandidatoCpfAsync(idcandidato); if (result == null) { return new CpfRevealResult(); } return new CpfRevealResult() { Cpf = result }; } } }