using Microsoft.EntityFrameworkCore; using REC_HUMA.CORE.Entities; using REC_HUMA.CORE.Interfaces; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; namespace REC_HUMA.INFRASTRUCTURE.Repositories { public class NotificacionesRepo : INotificacionesRepo { private readonly RECHUMAContext _Context; public NotificacionesRepo(RECHUMAContext Context) { _Context = Context; } public Task DeleteNotificacion(int IdNotificacion) { throw new NotImplementedException(); } public async Task GetNotificacion(int IdNotificacion) { var notificacion = await _Context.Notificaciones.FirstOrDefaultAsync(m => m.IdNotificacion == IdNotificacion); return notificacion; } public async Task GetNotificacionEnvioCorreo(int id) { try { var notificacion = await _Context.Notificaciones .Where(x => x.IdNotificacion == id) .FirstAsync(); var destinatarios = await _Context.Destinatarios .Where(x => x.IdNotificacion == notificacion.IdNotificacion) .Include(x => x.IdPersonaNavigation).ToListAsync(); var personas = new List(); destinatarios.ForEach(x => { personas.Add(x.IdPersonaNavigation); }); var adjuntos = new List(); adjuntos = await _Context.Adjunto.Where(x => x.IdNotificacion == notificacion.IdNotificacion).ToListAsync(); var retornoCorreo = new ModeloCorreo { ListaPersona = personas, ListaAdjunto = adjuntos, Notificacion = notificacion }; return retornoCorreo; } catch (Exception) { throw; } } //public async Task> GetNotificaciones() //{ // try // { // List ListaCorreo = new List(); // var listaNotificaciones = await _Context.Notificaciones.Where(x => x.IdEstado == 1).Include(x => x.Adjunto).ToListAsync(); // foreach (var notificacion in listaNotificaciones) // { // var Correo = new ModeloCorreo(); // Correo.Notificacion = notificacion; // var destinatarios = await _Context.Destinatarios.Include(d => d.IdPersonaNavigation). // Where(d => d.IdNotificacion == notificacion.IdNotificacion). // ToListAsync(); // var listaPersonas = new List(); // foreach (var d in destinatarios) // { // Persona persona = new Persona(); // persona = d.IdPersonaNavigation; // listaPersonas.Add(persona); // } // Correo.ListaPersona = listaPersonas; // var ListaAdjunto = notificacion.Adjunto.ToList(); // Correo.ListaAdjunto = ListaAdjunto; // ListaCorreo.Add(Correo); // } // //var list = await ListaNotificaones.ToListAsync(); // return ListaCorreo; // } // catch (Exception) // { // throw; // } //} public async Task PostNotificacion(ModeloCorreo modeloCorreo) { try { _Context.Notificaciones.Add(modeloCorreo.Notificacion); await _Context.SaveChangesAsync(); var notificacion = new Notificaciones(); notificacion = modeloCorreo.Notificacion; if (modeloCorreo.ListaPersona != null) { List listaPersona = new List(); listaPersona = modeloCorreo.ListaPersona; listaPersona.ForEach(x => { var destinatario = new Destinatarios(); destinatario.IdNotificacion = notificacion.IdNotificacion; destinatario.IdPersona = x.IdPersona; _Context.Destinatarios.Add(destinatario); _Context.SaveChanges(); }); } if (modeloCorreo.ListaAdjunto != null || modeloCorreo.ListaAdjunto.Count > 0) { List listaAdjunto = new List(); listaAdjunto = modeloCorreo.ListaAdjunto; listaAdjunto.ForEach(x => { var adjunto = new Adjunto(); adjunto = x; adjunto.IdNotificacion = notificacion.IdNotificacion; _Context.Adjunto.Add(adjunto); _Context.SaveChanges(); }); } return (int)modeloCorreo.Notificacion.IdNotificacion; } catch (Exception) { throw; } } public async Task PutNotificacion(Notificaciones notificacion) { try { var notificacionActual = await GetNotificacion((int)notificacion.IdNotificacion); notificacionActual.IdHabilitado = notificacion.IdHabilitado; int rows = await _Context.SaveChangesAsync(); return rows > 0; } catch (Exception) { throw; } } } }