2025-05-08 22:19:12 +07:00
|
|
|
|
using Amazon.Runtime.Internal.Endpoints.StandardLibrary;
|
|
|
|
|
|
using Amazon.S3.Model.Internal.MarshallTransformations;
|
2023-08-28 16:18:35 +07:00
|
|
|
|
using BMA.EHR.Application.Common.Interfaces;
|
2023-08-04 09:47:58 +07:00
|
|
|
|
using BMA.EHR.Domain.Models.Base;
|
2023-08-28 16:18:35 +07:00
|
|
|
|
using BMA.EHR.Domain.Models.HR;
|
2023-06-26 10:15:50 +07:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-05-08 22:19:12 +07:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2024-05-29 13:33:23 +07:00
|
|
|
|
using Newtonsoft.Json;
|
2025-05-08 22:19:12 +07:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2024-05-29 13:33:23 +07:00
|
|
|
|
using System.Net.Http.Headers;
|
2023-06-26 10:15:50 +07:00
|
|
|
|
using System.Security.Claims;
|
2024-05-29 13:33:23 +07:00
|
|
|
|
using System.Text;
|
2023-06-26 10:15:50 +07:00
|
|
|
|
|
|
|
|
|
|
namespace BMA.EHR.Application.Repositories
|
|
|
|
|
|
{
|
|
|
|
|
|
public class GenericRepository<S, T> : IGenericRepository<S, T> where T : class
|
|
|
|
|
|
{
|
|
|
|
|
|
#region " Field "
|
|
|
|
|
|
|
|
|
|
|
|
private readonly IApplicationDBContext _dbContext;
|
|
|
|
|
|
private readonly DbSet<T> _dbSet;
|
|
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
2023-08-28 16:18:35 +07:00
|
|
|
|
|
2023-06-26 10:15:50 +07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region " Constructor and Destructor "
|
|
|
|
|
|
|
|
|
|
|
|
public GenericRepository(IApplicationDBContext dbContext,
|
2024-08-15 23:31:34 +07:00
|
|
|
|
IHttpContextAccessor httpContextAccessor)
|
2023-06-26 10:15:50 +07:00
|
|
|
|
{
|
|
|
|
|
|
_dbContext = dbContext;
|
|
|
|
|
|
_dbSet = _dbContext.Set<T>();
|
|
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
2024-08-15 23:31:34 +07:00
|
|
|
|
// _configuration = configuration;
|
2023-06-26 10:15:50 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region " Properties "
|
|
|
|
|
|
|
2023-06-26 14:02:04 +07:00
|
|
|
|
protected string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
2023-06-26 10:15:50 +07:00
|
|
|
|
|
2023-06-26 14:02:04 +07:00
|
|
|
|
protected string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
|
2023-06-26 10:15:50 +07:00
|
|
|
|
|
2023-08-28 16:18:35 +07:00
|
|
|
|
protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
|
2023-08-17 12:43:47 +07:00
|
|
|
|
|
2024-05-30 09:32:34 +07:00
|
|
|
|
protected string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
|
|
|
|
|
|
|
2023-06-26 10:15:50 +07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region " Methods "
|
|
|
|
|
|
|
2024-05-29 13:33:23 +07:00
|
|
|
|
#region " For Call External API "
|
|
|
|
|
|
|
2026-01-30 13:35:58 +07:00
|
|
|
|
protected async Task<string> GetExternalAPIAsync(string apiPath, string accessToken, string apiKey, CancellationToken cancellationToken = default)
|
2024-05-29 13:33:23 +07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-30 13:35:58 +07:00
|
|
|
|
// กำหนด timeout เป็น 30 นาที
|
|
|
|
|
|
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(30));
|
|
|
|
|
|
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
|
2024-05-29 13:33:23 +07:00
|
|
|
|
using (var client = new HttpClient())
|
|
|
|
|
|
{
|
|
|
|
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
|
2025-11-12 01:56:06 +07:00
|
|
|
|
client.DefaultRequestHeaders.Add("api-key", apiKey);
|
2026-01-30 13:35:58 +07:00
|
|
|
|
var _res = await client.GetAsync(apiPath,cancellationToken: combinedCts.Token);
|
2024-05-29 13:33:23 +07:00
|
|
|
|
if (_res.IsSuccessStatusCode)
|
|
|
|
|
|
{
|
|
|
|
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return _result;
|
|
|
|
|
|
}
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-30 13:35:58 +07:00
|
|
|
|
protected async Task<string> SendExternalAPIAsync(HttpMethod method, string apiPath, string accessToken, object? body, string apiKey, CancellationToken cancellationToken = default)
|
2025-05-08 22:19:12 +07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-30 13:35:58 +07:00
|
|
|
|
// กำหนด timeout เป็น 30 นาที
|
|
|
|
|
|
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(30));
|
|
|
|
|
|
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
|
2025-05-08 22:19:12 +07:00
|
|
|
|
// สร้าง request message
|
|
|
|
|
|
var request = new HttpRequestMessage(method, apiPath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var json = JsonConvert.SerializeObject(body);
|
|
|
|
|
|
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
|
|
|
|
|
|
|
|
using (var client = new HttpClient())
|
|
|
|
|
|
{
|
|
|
|
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
|
2025-11-12 01:56:06 +07:00
|
|
|
|
client.DefaultRequestHeaders.Add("api-key", apiKey);
|
2026-01-30 13:35:58 +07:00
|
|
|
|
var _res = await client.SendAsync(request, combinedCts.Token);
|
2025-05-08 22:19:12 +07:00
|
|
|
|
if (_res.IsSuccessStatusCode)
|
|
|
|
|
|
{
|
|
|
|
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return _result;
|
|
|
|
|
|
}
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-30 13:35:58 +07:00
|
|
|
|
protected async Task<string> PostExternalAPIAsync(string apiPath, string accessToken, object? body, string apiKey, CancellationToken cancellationToken = default)
|
2024-05-29 13:33:23 +07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-30 13:35:58 +07:00
|
|
|
|
// กำหนด timeout เป็น 30 นาที
|
|
|
|
|
|
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(30));
|
|
|
|
|
|
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
|
2024-05-29 13:33:23 +07:00
|
|
|
|
var json = JsonConvert.SerializeObject(body);
|
2025-05-08 22:19:12 +07:00
|
|
|
|
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
|
|
//stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
2024-05-29 13:33:23 +07:00
|
|
|
|
|
|
|
|
|
|
using (var client = new HttpClient())
|
|
|
|
|
|
{
|
|
|
|
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
|
2025-11-12 01:56:06 +07:00
|
|
|
|
client.DefaultRequestHeaders.Add("api-key", apiKey);
|
2026-01-30 13:35:58 +07:00
|
|
|
|
var _res = await client.PostAsync(apiPath, stringContent, combinedCts.Token);
|
2024-05-29 13:33:23 +07:00
|
|
|
|
if (_res.IsSuccessStatusCode)
|
|
|
|
|
|
{
|
|
|
|
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return _result;
|
|
|
|
|
|
}
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-30 13:35:58 +07:00
|
|
|
|
protected async Task<bool> PostExternalAPIBooleanAsync(string apiPath, string accessToken, object? body, string apiKey, CancellationToken cancellationToken = default)
|
2024-06-20 13:37:25 +07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-30 13:35:58 +07:00
|
|
|
|
// กำหนด timeout เป็น 30 นาที
|
|
|
|
|
|
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(30));
|
|
|
|
|
|
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
|
2024-06-20 13:37:25 +07:00
|
|
|
|
var json = JsonConvert.SerializeObject(body);
|
|
|
|
|
|
var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
|
|
|
|
|
|
stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
|
|
|
|
|
|
|
|
|
|
using (var client = new HttpClient())
|
|
|
|
|
|
{
|
|
|
|
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
|
2025-11-12 01:56:06 +07:00
|
|
|
|
client.DefaultRequestHeaders.Add("api-key", apiKey);
|
2026-01-30 13:35:58 +07:00
|
|
|
|
var _res = await client.PostAsync(apiPath, stringContent, combinedCts.Token);
|
2024-06-20 13:37:25 +07:00
|
|
|
|
return _res.IsSuccessStatusCode;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-29 13:33:23 +07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
2023-08-28 16:44:31 +07:00
|
|
|
|
public async Task<Guid> GetProfileOrganizationAsync(string citizenId)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var pf = await _dbContext.Set<Profile>()
|
|
|
|
|
|
.Where(x => x.CitizenId == citizenId)
|
|
|
|
|
|
.Where(x => x.ProfileType.ToLower().Trim() == "officer" && x.IsActive && !x.IsLeave)
|
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return pf == null || pf.Oc == null ? Guid.Empty : pf.OcId!.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<bool> CheckIsActiveOfficerAsync(string citizenId)
|
2023-08-28 16:18:35 +07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var pf = await _dbContext.Set<Profile>()
|
|
|
|
|
|
.Where(x => x.CitizenId == citizenId)
|
|
|
|
|
|
.Where(x => x.ProfileType.ToLower().Trim() == "officer" && x.IsActive && !x.IsLeave)
|
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return pf != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-28 15:04:26 +07:00
|
|
|
|
public virtual async Task<IReadOnlyList<T>> GetAllAsync()
|
2023-06-26 10:15:50 +07:00
|
|
|
|
{
|
|
|
|
|
|
return await _dbSet.ToListAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-28 15:04:26 +07:00
|
|
|
|
public virtual async Task<T?> GetByIdAsync(S id)
|
2023-06-26 10:15:50 +07:00
|
|
|
|
{
|
|
|
|
|
|
return await _dbSet.FindAsync(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-28 15:04:26 +07:00
|
|
|
|
public virtual async Task<T> AddAsync(T entity)
|
2023-06-26 14:02:04 +07:00
|
|
|
|
{
|
2023-08-04 09:47:58 +07:00
|
|
|
|
if (entity is EntityBase)
|
|
|
|
|
|
{
|
|
|
|
|
|
(entity as EntityBase).CreatedUserId = UserId!;
|
|
|
|
|
|
(entity as EntityBase).CreatedFullName = FullName!;
|
|
|
|
|
|
(entity as EntityBase).CreatedAt = DateTime.Now;
|
|
|
|
|
|
}
|
2023-06-26 14:02:04 +07:00
|
|
|
|
|
|
|
|
|
|
await _dbSet.AddAsync(entity);
|
|
|
|
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-28 15:04:26 +07:00
|
|
|
|
public virtual async Task<T> UpdateAsync(T entity)
|
2023-06-26 14:02:04 +07:00
|
|
|
|
{
|
2023-08-04 09:47:58 +07:00
|
|
|
|
if (entity is EntityBase)
|
|
|
|
|
|
{
|
|
|
|
|
|
(entity as EntityBase).LastUpdateUserId = UserId!;
|
|
|
|
|
|
(entity as EntityBase).LastUpdateFullName = FullName!;
|
|
|
|
|
|
(entity as EntityBase).LastUpdatedAt = DateTime.Now;
|
|
|
|
|
|
}
|
2023-06-26 14:02:04 +07:00
|
|
|
|
|
|
|
|
|
|
_dbSet.Update(entity);
|
|
|
|
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-28 15:04:26 +07:00
|
|
|
|
public virtual async Task DeleteAsync(T entity)
|
2023-06-26 14:02:04 +07:00
|
|
|
|
{
|
|
|
|
|
|
_dbSet.Remove(entity);
|
|
|
|
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-26 10:15:50 +07:00
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|