จัดระเบียบ Code ใหม่ และเพิ่ม Extension Method
implement GenericRepository Class ใหม่
This commit is contained in:
parent
e49c6a4aca
commit
89de09d213
21 changed files with 1439 additions and 63 deletions
|
|
@ -1,26 +1,28 @@
|
|||
using BMA.EHR.Application.Common.Interfaces;
|
||||
using BMA.EHR.Domain.Entities.MetaData.BloodGroup;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace BMA.EHR.Application.Repositories.BloodGroup
|
||||
{
|
||||
public class BloodGroupRepository : IGenericRepository<Guid, BloodGroupEntity>
|
||||
public class BloodGroupRepository : GenericRepository<Guid, BloodGroupEntity>
|
||||
{
|
||||
private readonly IApplicationDBContext _dbContext;
|
||||
#region " Fields "
|
||||
|
||||
public BloodGroupRepository(IApplicationDBContext dbContext)
|
||||
private readonly IApplicationDBContext _dbContext;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Constructor and Destructor "
|
||||
|
||||
public BloodGroupRepository(IApplicationDBContext dbContext,
|
||||
IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<BloodGroupEntity>> GetAllAsync()
|
||||
{
|
||||
return await _dbContext.MD_BloodGroups.ToListAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
public async Task<BloodGroupEntity?> GetByIdAsync(Guid id)
|
||||
{
|
||||
return await _dbContext.MD_BloodGroups.FirstOrDefaultAsync(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
52
BMA.EHR.Application/Repositories/GenericRepository.cs
Normal file
52
BMA.EHR.Application/Repositories/GenericRepository.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +1,28 @@
|
|||
using BMA.EHR.Application.Common.Interfaces;
|
||||
using BMA.EHR.Domain.Entities.MetaData.Prefix;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace BMA.EHR.Application.Repositories.Prefix
|
||||
{
|
||||
public class PrefixRepository : IGenericRepository<Guid, PrefixEntity>
|
||||
public class PrefixRepository : GenericRepository<Guid, PrefixEntity>
|
||||
{
|
||||
private readonly IApplicationDBContext _dbContext;
|
||||
#region " Fields "
|
||||
|
||||
public PrefixRepository(IApplicationDBContext dbContext)
|
||||
private readonly IApplicationDBContext _dbContext;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Constructor and Destructor "
|
||||
|
||||
public PrefixRepository(IApplicationDBContext dbContext,
|
||||
IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<PrefixEntity>> GetAllAsync()
|
||||
{
|
||||
return await _dbContext.MD_Prefixes.ToListAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
public async Task<PrefixEntity?> GetByIdAsync(Guid id)
|
||||
{
|
||||
return await _dbContext.MD_Prefixes.FirstOrDefaultAsync(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue