checkpoint rabbitMq

This commit is contained in:
AdisakKanthawilang 2024-11-26 17:17:13 +07:00
parent be18edf0a6
commit 93d0c2711f
2 changed files with 96 additions and 0 deletions

View file

@ -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<Notification>().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<Inbox>().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 = "")
{

View file

@ -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
/// <summary>
/// ทดสอบ (Rabbit MQ)
/// </summary>
/// <returns></returns>
[HttpPost("test-queue")]
public async Task<ActionResult<ResponseObject>> 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<bool>();
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<ActionResult<ResponseObject>> UpdatePropertyByUser([FromBody] NotiRequest req)
{