fix : Optimize Query

This commit is contained in:
Suphonchai Phoonsawat 2024-07-07 09:59:37 +07:00
parent 89ef146c19
commit a911648907
9 changed files with 168 additions and 232 deletions

View file

@ -25,6 +25,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Logging" Version="6.32.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />

View file

@ -15,8 +15,10 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Newtonsoft.Json;
using OfficeOpenXml;
using RabbitMQ.Client;
using Swashbuckle.AspNetCore.Annotations;
using System.Security.Claims;
using System.Text;
namespace BMA.EHR.Insignia.Service.Controllers
{
@ -36,10 +38,10 @@ namespace BMA.EHR.Insignia.Service.Controllers
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly string Royal_Type = "Royal";
private readonly UserProfileRepository _userProfileRepository;
private readonly RabbitMQProducer _producer;
private const string INSIGNIA_QUEUE = "bma_insignia_request";
private readonly InsigniaPeriodsRepository _insigniaPeriodRepository;
private readonly IConfiguration _configuration;
/// <summary>
///
@ -61,7 +63,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
IHttpContextAccessor httpContextAccessor,
UserProfileRepository userProfileRepository,
InsigniaPeriodsRepository insigniaPeriodRepository,
RabbitMQProducer producer)
IConfiguration configuration)
{
_context = context;
_documentService = documentService;
@ -71,7 +73,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
_hostingEnvironment = hostingEnvironment;
_userProfileRepository = userProfileRepository;
_insigniaPeriodRepository = insigniaPeriodRepository;
_producer = producer;
_configuration = configuration;
}
#region " Properties "
@ -320,21 +322,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
// }
#endregion
#region " ทดสอบ RabbitMQ "
/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet("rabbit")]
[AllowAnonymous]
public ActionResult<ResponseObject> CallRabbitMQ()
{
_producer.SendMessage("test send");
return Success();
}
#endregion
#region " จัดทำรายชื่อครูที่มีสิทธิในการยืนขอเครื่องราชฯ "
@ -603,9 +591,30 @@ namespace BMA.EHR.Insignia.Service.Controllers
[HttpGet("queue/{insigniaPeriodId:length(36)}")]
public ActionResult<ResponseObject> InsigniaRequestCalculate(Guid insigniaPeriodId)
{
_producer.CalculateInsignia(insigniaPeriodId);
return Success();
var host = _configuration["RabbitMQ:URL"];
var userName = _configuration["RabbitMQ:UserName"];
var password = _configuration["RabbitMQ:Password"];
var factory = new ConnectionFactory()
{
HostName = host,
UserName = userName,
Password = password
};
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: INSIGNIA_QUEUE, durable: false, exclusive: false, autoDelete: false, arguments: null);
var body = Encoding.UTF8.GetBytes(insigniaPeriodId.ToString("D"));
channel.BasicPublish(exchange: "", routingKey: INSIGNIA_QUEUE, basicProperties: null, body: body);
return Success();
}
}
#endregion
#region " บันทึกหมายเหตุ "

View file

@ -1,4 +1,5 @@
using BMA.EHR.Application;
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.Reports;
using BMA.EHR.Domain.Middlewares;
using BMA.EHR.Infrastructure;
@ -91,8 +92,7 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddLeaveApplication();
builder.Services.AddLeavePersistence(builder.Configuration);
// RabbitMQ
builder.Services.AddMessageQueue();
builder.Services.AddControllers(options =>
{
@ -106,7 +106,7 @@ var builder = WebApplication.CreateBuilder(args);
// Register DbContext
var defaultConnection = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDBContext>(options =>
options.UseMySql(defaultConnection, ServerVersion.AutoDetect(defaultConnection)));
options.UseMySql(defaultConnection, ServerVersion.AutoDetect(defaultConnection)), ServiceLifetime.Transient);
builder.Services.AddHealthChecks();
// Add Hangfire services.
builder.Services.AddHangfire(configuration => configuration
@ -129,6 +129,19 @@ var builder = WebApplication.CreateBuilder(args);
TablesPrefix = "Hangfire"
})));
builder.Services.AddHangfireServer();
// RabbitMQ
//builder.Services.AddTransient<RabbitMQConsumer>(provider =>
//{
// var serviceScopeFactory = provider.GetRequiredService<IServiceScopeFactory>();
// var userRepo = provider.GetRequiredService<UserProfileRepository>();
// var insigniaRepo = provider.GetRequiredService<InsigniaPeriodsRepository>();
// var httpContext = provider.GetRequiredService<IHttpContextAccessor>();
// var config = provider.GetRequiredService<IConfiguration>();
// return new RabbitMQConsumer(userRepo, insigniaRepo, httpContext, serviceScopeFactory, config);
//});
}
var app = builder.Build();
@ -151,6 +164,8 @@ var app = builder.Build();
app.MapHealthChecks("/health");
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthentication();
@ -176,8 +191,8 @@ var app = builder.Build();
await using var db = scope.ServiceProvider.GetRequiredService<ApplicationDBContext>();
await db.Database.MigrateAsync();
var rabbitMQConsumer = app.Services.GetRequiredService<RabbitMQConsumer>();
rabbitMQConsumer.StartReceiving();
//var rabbitMQConsumer = app.Services.GetRequiredService<RabbitMQConsumer>();
//rabbitMQConsumer.StartReceiving();
app.Run();
}