148 lines
5 KiB
C#
148 lines
5 KiB
C#
using BMA.EHR.Application.Common.Interfaces;
|
|
using BMA.EHR.Domain.Models.Base;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using System.IO.Pipes;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
|
|
namespace BMA.EHR.Application.Repositories.Leaves
|
|
{
|
|
public class GenericLeaveRepository<S, T> : IGenericRepository<S, T> where T : class
|
|
{
|
|
#region " Field "
|
|
|
|
private readonly ILeaveDbContext _dbContext;
|
|
private readonly DbSet<T> _dbSet;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
#endregion
|
|
|
|
#region " Constructor and Destructor "
|
|
|
|
public GenericLeaveRepository(ILeaveDbContext dbContext,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_dbContext = dbContext;
|
|
_dbSet = _dbContext.Set<T>();
|
|
_httpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " Properties "
|
|
|
|
protected string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
|
|
protected string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
|
|
|
|
protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
|
|
|
|
protected string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
|
|
|
|
#endregion
|
|
|
|
#region " Methods "
|
|
|
|
public async Task<string> PostExternalAPIAsync(string apiPath, string accessToken, object? body, string apiKey, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
// กำหนด timeout เป็น 30 นาที
|
|
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(30));
|
|
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
|
|
var json = JsonConvert.SerializeObject(body);
|
|
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
|
|
//stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
|
|
client.DefaultRequestHeaders.Add("api-key", apiKey);
|
|
var _res = await client.PostAsync(apiPath, stringContent, combinedCts.Token);
|
|
if (_res.IsSuccessStatusCode)
|
|
{
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
|
|
return _result;
|
|
}
|
|
return string.Empty;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
|
|
public virtual async Task<IReadOnlyList<T>> GetAllAsync()
|
|
{
|
|
return await _dbSet.ToListAsync();
|
|
}
|
|
|
|
public virtual async Task<T?> GetByIdAsync(S id)
|
|
{
|
|
return await _dbSet.FindAsync(id);
|
|
}
|
|
|
|
public virtual async Task<T> AddAsync(T entity)
|
|
{
|
|
if (entity is EntityBase)
|
|
{
|
|
(entity as EntityBase).CreatedUserId = UserId ?? "";
|
|
(entity as EntityBase).CreatedFullName = FullName ?? "System Administrator";
|
|
(entity as EntityBase).CreatedAt = DateTime.Now;
|
|
}
|
|
|
|
await _dbSet.AddAsync(entity);
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return entity;
|
|
}
|
|
|
|
public virtual async Task<IReadOnlyList<T>> AddRangeAsync(List<T> entities)
|
|
{
|
|
foreach (var entity in entities)
|
|
{
|
|
if (entity is EntityBase)
|
|
{
|
|
(entity as EntityBase).CreatedUserId = UserId ?? "";
|
|
(entity as EntityBase).CreatedFullName = FullName ?? "System Administrator";
|
|
(entity as EntityBase).CreatedAt = DateTime.Now;
|
|
}
|
|
}
|
|
|
|
await _dbSet.AddRangeAsync(entities);
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return entities;
|
|
}
|
|
|
|
public virtual async Task<T> UpdateAsync(T entity)
|
|
{
|
|
if (entity is EntityBase)
|
|
{
|
|
(entity as EntityBase).LastUpdateUserId = UserId ?? "";
|
|
(entity as EntityBase).LastUpdateFullName = FullName ?? "System Administrator";
|
|
(entity as EntityBase).LastUpdatedAt = DateTime.Now;
|
|
}
|
|
|
|
//_dbContext.Detach(entity);
|
|
_dbSet.Update(entity);
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return entity;
|
|
}
|
|
|
|
public virtual async Task DeleteAsync(T entity)
|
|
{
|
|
_dbSet.Remove(entity);
|
|
await _dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|