From 93d0c2711faef4be73f9ea7211757b019010d53c Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 26 Nov 2024 17:17:13 +0700 Subject: [PATCH] checkpoint rabbitMq --- .../MessageQueue/NotificationRepository.cs | 44 ++++++++++++++++ .../Controllers/NotifyController.cs | 52 +++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs b/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs index a4043164..8388fd0c 100644 --- a/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs +++ b/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs @@ -233,6 +233,50 @@ namespace BMA.EHR.Application.Repositories.MessageQueue throw; } } + public async Task PushNotificationAsync2(Guid ReceiverUserId, string Subject, string Body, string Payload = "", string NotiLink = "", bool IsSendInbox = false, bool IsSendMail = false) + { + try + { + _dbContext.Set().Add(new Notification + { + Body = Body, + ReceiverUserId = ReceiverUserId, + Type = "", + Payload = NotiLink, + CreatedFullName = FullName ?? "System Administrator", + CreatedUserId = UserId ?? "", + CreatedAt = DateTime.Now, + LastUpdateFullName = FullName ?? "System Administrator", + LastUpdateUserId = UserId ?? "", + LastUpdatedAt = DateTime.Now, + }); + /* if (IsSendInbox == true) + { + _dbContext.Set().Add(new Inbox + { + Subject = Subject, + Body = Body, + ReceiverUserId = ReceiverUserId, + Payload = Payload, + CreatedFullName = FullName ?? "System Administrator", + CreatedUserId = UserId ?? "", + CreatedAt = DateTime.Now, + LastUpdateFullName = FullName ?? "System Administrator", + LastUpdateUserId = UserId ?? "", + LastUpdatedAt = DateTime.Now, + }); + }*/ + if (IsSendMail == true) + { + _emailSenderService.SendMail(Subject, Body, "kittapath@frappet.com"); + } + await _dbContext.SaveChangesAsync(); + } + catch + { + throw; + } + } public async Task PushEmailAsync(string Subject, string Body, string Email = "") { diff --git a/BMA.EHR.Placement.Service/Controllers/NotifyController.cs b/BMA.EHR.Placement.Service/Controllers/NotifyController.cs index 58d70f0a..8ef642fa 100644 --- a/BMA.EHR.Placement.Service/Controllers/NotifyController.cs +++ b/BMA.EHR.Placement.Service/Controllers/NotifyController.cs @@ -13,10 +13,13 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; +using RabbitMQ.Client.Events; +using RabbitMQ.Client; using Swashbuckle.AspNetCore.Annotations; using System.Net.Http.Headers; using System.Security.Claims; using System.Security.Cryptography; +using System.Text; namespace BMA.EHR.Placement.Service.Controllers { @@ -59,6 +62,55 @@ namespace BMA.EHR.Placement.Service.Controllers #endregion + /// + /// ทดสอบ (Rabbit MQ) + /// + /// + [HttpPost("test-queue")] + public async Task> TestRabbitMQ([FromBody] NotiRequest req) + { + var host = "localhost"; + var userName = "guest"; + var password = "guest"; + + var factory = new ConnectionFactory() + { + HostName = host, + UserName = userName, + Password = password + }; + Console.WriteLine("Send to Consume!"); + using (var connection = factory.CreateConnection()) + using (var channel = connection.CreateModel()) + { + channel.QueueDeclare(queue: "test_dotnet", durable: false, exclusive: false, autoDelete: false, arguments: null); + + var jsonString = JsonConvert.SerializeObject(req); + + var body = Encoding.UTF8.GetBytes(jsonString); + + channel.BasicPublish(exchange: "", routingKey: "test_dotnet", basicProperties: null, body: body); + var consumer = new EventingBasicConsumer(channel); + var receivedTaskCompletionSource = new TaskCompletionSource(); + consumer.Received += async (model, x) => + { + await _repositoryNoti.PushNotificationAsync2( + Guid.Parse(req.ReceiverUserId), + req.Subject, + req.Body, + req.Payload, + "", + req.IsSendInbox, + req.IsSendMail + ); + receivedTaskCompletionSource.SetResult(true); + }; + channel.BasicConsume("test_dotnet", autoAck: true, consumer); + Console.WriteLine("Consume Worked!"); + return Success(); + } + } + [HttpPost()] public async Task> UpdatePropertyByUser([FromBody] NotiRequest req) {