hrms-api-backend/BMA.EHR.MetaData.Service/Services/BloodGroupService.cs
kittapath 39acbb7f95
Some checks failed
release-dev / release-dev (push) Failing after 13s
no message
2024-12-25 10:21:31 +07:00

92 lines
3.1 KiB
C#

using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;
namespace BMA.EHR.MetaData.Service.Services
{
public class BloodGroupService
{
#region " Fields "
private readonly ApplicationDBContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<BloodGroupService> _logger;
#endregion
#region " Constructor and Destructor "
public BloodGroupService(ApplicationDBContext context,
IHttpContextAccessor httpContextAccessor,
ILogger<BloodGroupService> logger)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
_logger = logger;
}
#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<BloodGroup>> GetsAsync(bool showAll = true)
{
if (showAll)
return await _context.BloodGroups.AsQueryable()
.OrderBy(d => d.Name)
.ToListAsync();
else
return await _context.BloodGroups.AsQueryable()
.Where(p => p.IsActive)
.OrderBy(d => d.Name)
.ToListAsync();
}
public async Task<BloodGroup?> GetByIdAsync(Guid id)
{
return await _context.BloodGroups.FirstOrDefaultAsync(x => x.Id == id);
}
public async Task UpdateAsync(Guid id, BloodGroup updated)
{
var existData = await _context.BloodGroups.FirstOrDefaultAsync(x => x.Id == id);
if (existData != null)
{
if (existData.Name != updated.Name || existData.IsActive != updated.IsActive)
{
existData.Name = updated.Name;
existData.IsActive = updated.IsActive;
existData.LastUpdatedAt = DateTime.Now;
existData.LastUpdateUserId = UserId ?? "";
existData.LastUpdateFullName = FullName ?? "";
}
await _context.SaveChangesAsync();
}
}
public async Task CreateAsync(BloodGroup 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.BloodGroups.AddAsync(inserted);
await _context.SaveChangesAsync();
}
#endregion
}
}