hrms-api-backend/BMA.EHR.Application/Repositories/GenericRepository.cs
Suphonchai Phoonsawat 89de09d213 จัดระเบียบ Code ใหม่ และเพิ่ม Extension Method
implement GenericRepository Class ใหม่
2023-06-26 10:15:50 +07:00

52 lines
1.4 KiB
C#

using BMA.EHR.Application.Common.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;
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;
#endregion
#region " Constructor and Destructor "
public GenericRepository(IApplicationDBContext dbContext,
IHttpContextAccessor httpContextAccessor)
{
_dbContext = dbContext;
_dbSet = _dbContext.Set<T>();
_httpContextAccessor = httpContextAccessor;
}
#endregion
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
#endregion
#region " Methods "
public async Task<IReadOnlyList<T>> GetAllAsync()
{
return await _dbSet.ToListAsync();
}
public async Task<T?> GetByIdAsync(S id)
{
return await _dbSet.FindAsync(id);
}
#endregion
}
}