98 lines
3.1 KiB
C#
98 lines
3.1 KiB
C#
using BMA.EHR.Recurit.Exam.Service.Data;
|
|
using BMA.EHR.Recurit.Exam.Service.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Security.Claims;
|
|
|
|
namespace BMA.EHR.Recurit.Exam.Service.Services
|
|
{
|
|
public class PrefixService
|
|
{
|
|
#region " Fields "
|
|
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
#endregion
|
|
|
|
#region " Constructor and Destructor "
|
|
|
|
public PrefixService(ApplicationDbContext context,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_context = context;
|
|
_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<IEnumerable<Prefix>> GetsAsync(bool showAll = true)
|
|
{
|
|
if (showAll)
|
|
return await _context.Prefixes.AsQueryable()
|
|
.OrderBy(d => d.Name)
|
|
.ToListAsync();
|
|
else
|
|
return await _context.Prefixes.AsQueryable()
|
|
.Where(p => p.IsActive)
|
|
.OrderBy(d => d.Name)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<Prefix?> GetByIdAsync(Guid id)
|
|
{
|
|
return await _context.Prefixes.FirstOrDefaultAsync(x => x.Id == id);
|
|
}
|
|
|
|
public async Task UpdateAsync(Guid id, Prefix updated)
|
|
{
|
|
var existData = await _context.Prefixes.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (existData != null)
|
|
{
|
|
if (existData.Name != updated.Name)
|
|
{
|
|
existData.Name = updated.Name;
|
|
existData.LastUpdateUserId = UserId ?? "";
|
|
existData.LastUpdateFullName = FullName ?? "";
|
|
existData.LastUpdatedAt = DateTime.Now;
|
|
}
|
|
|
|
if (existData.IsActive != updated.IsActive)
|
|
{
|
|
existData.IsActive = updated.IsActive;
|
|
existData.LastUpdateUserId = UserId ?? "";
|
|
existData.LastUpdateFullName = FullName ?? "";
|
|
existData.LastUpdatedAt = DateTime.Now;
|
|
}
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task CreateAsync(Prefix inserted)
|
|
{
|
|
inserted.CreatedUserId = UserId ?? "";
|
|
inserted.CreatedFullName = FullName ?? "System Administrator";
|
|
inserted.CreatedAt = DateTime.Now;
|
|
inserted.LastUpdatedAt = DateTime.Now;
|
|
inserted.LastUpdateFullName = FullName ?? "System Administrator";
|
|
inserted.LastUpdateUserId = UserId ?? "";
|
|
|
|
await _context.Prefixes.AddAsync(inserted);
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|