init
This commit is contained in:
37
OpenCand.ETL/Services/BemCandidatoService.cs
Normal file
37
OpenCand.ETL/Services/BemCandidatoService.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using OpenCand.Core.Models;
|
||||
using OpenCand.Repository;
|
||||
|
||||
namespace OpenCand.Services
|
||||
{
|
||||
public class BemCandidatoService
|
||||
{
|
||||
private readonly CandidatoRepository candidatoRepository;
|
||||
private readonly BemCandidatoRepository bemCandidatoRepository;
|
||||
|
||||
public BemCandidatoService(CandidatoRepository candidatoRepository, BemCandidatoRepository bemCandidatoRepository)
|
||||
{
|
||||
this.candidatoRepository = candidatoRepository;
|
||||
this.bemCandidatoRepository = bemCandidatoRepository;
|
||||
}
|
||||
|
||||
public async Task AddBemCandidatoAsync(BemCandidato bemCandidato)
|
||||
{
|
||||
if (bemCandidato == null || string.IsNullOrWhiteSpace(bemCandidato.SqCandidato))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bemCandidato), "BemCandidato cannot be null");
|
||||
}
|
||||
|
||||
// Get idCandidato from CandidatoRepository
|
||||
var candidato = await candidatoRepository.GetIdCandidatoBySqCandidato(bemCandidato.SqCandidato, bemCandidato.Ano, bemCandidato.SiglaUF, bemCandidato.NomeUE);
|
||||
|
||||
if (candidato == null || candidato.IdCandidato == Guid.Empty)
|
||||
{
|
||||
throw new InvalidOperationException($"AddBemCandidatoAsync - Candidato '{bemCandidato.SqCandidato}'/{bemCandidato.Ano}/'{bemCandidato.SiglaUF}'/'{bemCandidato.NomeUE}' not found.");
|
||||
}
|
||||
|
||||
bemCandidato.IdCandidato = candidato.IdCandidato;
|
||||
|
||||
await bemCandidatoRepository.AddBemCandidatoAsync(bemCandidato);
|
||||
}
|
||||
}
|
||||
}
|
85
OpenCand.ETL/Services/CandidatoService.cs
Normal file
85
OpenCand.ETL/Services/CandidatoService.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using OpenCand.Core.Models;
|
||||
using OpenCand.Repository;
|
||||
|
||||
namespace OpenCand.Services
|
||||
{
|
||||
public class CandidatoService
|
||||
{
|
||||
private readonly CandidatoRepository candidatoRepository;
|
||||
|
||||
public CandidatoService(CandidatoRepository candidatoRepository)
|
||||
{
|
||||
this.candidatoRepository = candidatoRepository;
|
||||
}
|
||||
|
||||
public async Task AddCandidatoAsync(Candidato candidato)
|
||||
{
|
||||
if (candidato == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(candidato), "Candidato cannot be null");
|
||||
}
|
||||
|
||||
if (candidato.Eleicoes == null || candidato.Eleicoes.Count == 0)
|
||||
{
|
||||
throw new ArgumentException("Candidato must have at least one mapping", nameof(candidato));
|
||||
}
|
||||
|
||||
var candidatoMapping = candidato.Eleicoes.First();
|
||||
|
||||
List<CandidatoMapping>? mappings = null;
|
||||
CandidatoMapping? existingMapping = null;
|
||||
if (candidato.Cpf == null || candidato.Cpf.Length != 11)
|
||||
{
|
||||
mappings = await candidatoRepository.GetCandidatoMappingByNome(candidato.Nome);
|
||||
}
|
||||
else
|
||||
{
|
||||
mappings = await candidatoRepository.GetCandidatoMappingByCpf(candidato.Cpf);
|
||||
}
|
||||
|
||||
// Check if exists
|
||||
if (mappings != null && mappings.Count > 0)
|
||||
{
|
||||
existingMapping = mappings.FirstOrDefault(m => m.Ano == candidatoMapping.Ano &&
|
||||
m.Cargo == candidatoMapping.Cargo &&
|
||||
m.SiglaUF == candidatoMapping.SiglaUF &&
|
||||
m.NomeUE == candidatoMapping.NomeUE &&
|
||||
m.NrCandidato == candidatoMapping.NrCandidato &&
|
||||
m.Resultado == candidatoMapping.Resultado);
|
||||
|
||||
// Already exists one for the current election
|
||||
if (existingMapping != null)
|
||||
{
|
||||
candidato.IdCandidato = existingMapping.IdCandidato;
|
||||
candidato.Cpf = existingMapping.Cpf;
|
||||
|
||||
await candidatoRepository.AddCandidatoAsync(candidato);
|
||||
return;
|
||||
}
|
||||
// If exists (but not for the current election), we take the existing idcandidato
|
||||
// and create a new mapping for the current election
|
||||
else
|
||||
{
|
||||
existingMapping = mappings.First();
|
||||
candidato.IdCandidato = existingMapping.IdCandidato;
|
||||
candidato.Cpf = existingMapping.Cpf;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No current mapping, we create a new one
|
||||
// and create a new mapping for the current election
|
||||
candidato.IdCandidato = Guid.NewGuid();
|
||||
}
|
||||
|
||||
// Set the mapping properties
|
||||
candidatoMapping.IdCandidato = candidato.IdCandidato;
|
||||
candidatoMapping.Cpf = candidato.Cpf;
|
||||
candidatoMapping.Nome = candidato.Nome;
|
||||
|
||||
await candidatoRepository.AddCandidatoAsync(candidato);
|
||||
await candidatoRepository.AddCandidatoMappingAsync(candidatoMapping);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
74
OpenCand.ETL/Services/RedeSocialService.cs
Normal file
74
OpenCand.ETL/Services/RedeSocialService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using OpenCand.Core.Models;
|
||||
using OpenCand.Repository;
|
||||
|
||||
namespace OpenCand.Services
|
||||
{
|
||||
public class RedeSocialService
|
||||
{
|
||||
private readonly CandidatoRepository candidatoRepository;
|
||||
private readonly RedeSocialRepository redeSocialRepository;
|
||||
|
||||
public RedeSocialService(CandidatoRepository candidatoRepository, RedeSocialRepository redeSocialRepository)
|
||||
{
|
||||
this.candidatoRepository = candidatoRepository;
|
||||
this.redeSocialRepository = redeSocialRepository;
|
||||
}
|
||||
|
||||
public async Task AddRedeSocialAsync(RedeSocial redeSocial)
|
||||
{
|
||||
if (redeSocial == null || string.IsNullOrWhiteSpace(redeSocial.SqCandidato))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(redeSocial), "RedeSocial cannot be null");
|
||||
}
|
||||
|
||||
// Get idCandidato from CandidatoRepository
|
||||
var candidato = await candidatoRepository.GetIdCandidatoBySqCandidato(redeSocial.SqCandidato, redeSocial.Ano, redeSocial.SiglaUF);
|
||||
|
||||
if (candidato == null || candidato.IdCandidato == Guid.Empty)
|
||||
{
|
||||
throw new InvalidOperationException($"AddRedeSocialAsync - Candidato '{redeSocial.SqCandidato}'/{redeSocial.Ano}/'{redeSocial.SiglaUF}' not found.");
|
||||
}
|
||||
|
||||
redeSocial.IdCandidato = candidato.IdCandidato;
|
||||
redeSocial.Rede = GetRedeSocialType(redeSocial.Link);
|
||||
|
||||
await redeSocialRepository.AddRedeSocialAsync(redeSocial);
|
||||
}
|
||||
|
||||
private string GetRedeSocialType(string url)
|
||||
{
|
||||
switch (url.ToLower())
|
||||
{
|
||||
case var s when s.Contains("facebook.com"):
|
||||
return "Facebook";
|
||||
case var s when s.Contains("twitter.com"):
|
||||
case var ss when ss.Contains("x.com"):
|
||||
return "X/Twitter";
|
||||
case var s when s.Contains("instagram.com"):
|
||||
return "Instagram";
|
||||
case var s when s.Contains("youtube.com"):
|
||||
return "YouTube";
|
||||
case var s when s.Contains("linkedin.com"):
|
||||
return "LinkedIn";
|
||||
case var s when s.Contains("spotify.com"):
|
||||
return "Spotify";
|
||||
case var s when s.Contains("kwai.com"):
|
||||
return "Kwai";
|
||||
case var s when s.Contains("tiktok.com"):
|
||||
return "TikTok";
|
||||
case var s when s.Contains("threads.com"):
|
||||
case var ss when ss.Contains("threads.net"):
|
||||
return "Threads";
|
||||
case var s when s.Contains("t.me"):
|
||||
case var ss when ss.Contains("telegram.com"):
|
||||
return "Telegram";
|
||||
case var s when s.Contains("api.whatsapp"):
|
||||
case var ss when ss.Contains("whatsapp.com"):
|
||||
case var sss when sss.Contains("wa.me"):
|
||||
return "WhatsApp";
|
||||
default:
|
||||
return "Outros";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user