using AutoMapper; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using REC_HUMA.CORE.DTOs; using REC_HUMA.CORE.Entities; using REC_HUMA.CORE.Interfaces; using System.Collections.Generic; using System.Threading.Tasks; using System; using Microsoft.AspNetCore.Connections; using Newtonsoft.Json; using System.Text; using RabbitMQ.Client; using System.Linq; using REC_HUMA.INFRASTRUCTURE.Repositories; namespace REC_HUMA.API.Controllers { [Route("api/[controller]")] [ApiController] public class NotificacionController : ControllerBase { private readonly INotificacionesRepo _NotificacionRepo; private readonly IMapper _mapper; private readonly ILogger _logger; public NotificacionController(INotificacionesRepo NotificacionRepo, IMapper mapper, ILogger logger) { _NotificacionRepo = NotificacionRepo; _mapper = mapper; _logger = logger; } [HttpGet("GetNotificacionesID")] public async Task GetNotificacionesID(int idNotificacion) { try { var notificaciones = await _NotificacionRepo.GetNotificacionEnvioCorreo(idNotificacion); var notificacionesDto = _mapper.Map(notificaciones); return notificacionesDto; } catch (Exception ex) { var notificacionesDto = new ModeloCorreoDto { CodResultado = 500, Mensaje = "Error" }; _logger.LogError(ex, ex.Message); return notificacionesDto; } } [HttpPut] public async Task PutNotificacion( NotificacionesDto notificacionesDto) { try { var notificacion = _mapper.Map(notificacionesDto); await _NotificacionRepo.PutNotificacion(notificacion); return Ok(); } catch (Exception ex) { _logger.LogError(ex, ex.Message); return StatusCode(500); } } [HttpPost] public async Task PostNotificacion( ModeloCorreoDto modeloCorreo) { try { var modeloCorreoNuevo = _mapper.Map(modeloCorreo); int idNotificacion = await _NotificacionRepo.PostNotificacion(modeloCorreoNuevo); EnviarColaId(idNotificacion); return Ok(idNotificacion); } catch (Exception ex) { _logger.LogError(ex, ex.Message); return StatusCode(500); } } private void EnviarCola(List modelo) { string json = ""; var factory = new ConnectionFactory() { HostName = "localhost" }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "notificacion", durable: false, exclusive: false, autoDelete: false, arguments: null); json = JsonConvert.SerializeObject(modelo); var body = Encoding.UTF8.GetBytes(json); channel.BasicPublish(exchange: "", routingKey: "notificacion", basicProperties: null, body: body); Console.WriteLine(" [x] Sent {0}", json); } } private void EnviarColaId(int id, ModeloCorreoDto modelo =null) { string jsonModeloCorreo = ""; var factory = new ConnectionFactory() { HostName = "localhost" }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "notificacion", durable: false, exclusive: false, autoDelete: false, arguments: null); channel.QueueDeclare(queue: "notificacionPorId", durable: false, exclusive: false, autoDelete: false, arguments: null); jsonModeloCorreo = JsonConvert.SerializeObject(modelo); var bodyPorId = Encoding.UTF8.GetBytes(id.ToString()); var notificacion = Encoding.UTF8.GetBytes(jsonModeloCorreo); channel.BasicPublish(exchange: "", routingKey: "notificacion", basicProperties: null, body: notificacion); channel.BasicPublish(exchange: "", routingKey: "notificacionPorId", basicProperties: null, body: bodyPorId); //Console.WriteLine(" [x] Sent {0}", bodyPorId); } } } }