119 lines
No EOL
4.4 KiB
C#
119 lines
No EOL
4.4 KiB
C#
using System.Collections.Immutable;
|
|
using System.Security.Claims;
|
|
using BMA.EHR.Extensions;
|
|
using BMA.EHR.Recurit.Exam.Service.Data;
|
|
using BMA.EHR.Recurit.Exam.Service.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace BMA.EHR.Recurit.Exam.Service.Services
|
|
{
|
|
public class SubDistrictService
|
|
{
|
|
#region " Fields "
|
|
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
#endregion
|
|
|
|
#region " Constructor and Destructor "
|
|
|
|
public SubDistrictService(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<SubDistrict>> GetsAsync(string districtId, bool showAll = true)
|
|
{
|
|
if (showAll)
|
|
return await _context.SubDistricts.AsQueryable()
|
|
.Include(d => d.District)
|
|
.Where(x => x.District.Id == Guid.Parse(districtId))
|
|
.OrderBy(d => d.Name)
|
|
.ToListAsync();
|
|
else
|
|
return await _context.SubDistricts.AsQueryable()
|
|
.Include(d => d.District)
|
|
.Where(x => x.District.Id == Guid.Parse(districtId))
|
|
.Where(p => p.IsActive)
|
|
.OrderBy(d => d.Name)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<SubDistrict?> GetByIdAsync(Guid id)
|
|
{
|
|
return await _context.SubDistricts.AsQueryable()
|
|
.Include(d => d.District)
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
}
|
|
|
|
public async Task UpdateAsync(Guid id, SubDistrict updated)
|
|
{
|
|
var existData = await _context.SubDistricts.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (existData != null)
|
|
{
|
|
// if (!existData.Compare(updated))
|
|
// {
|
|
// existData.Name = updated.Name;
|
|
// existData.ZipCode = updated.ZipCode;
|
|
// existData.IsActive = updated.IsActive;
|
|
// existData.LastUpdatedAt = DateTime.Now;
|
|
// existData.LastUpdateUserId = UserId ?? "";
|
|
// existData.LastUpdateFullName = FullName ?? "";
|
|
|
|
// await _context.SaveChangesAsync();
|
|
// }
|
|
if (existData.Name != updated.Name || existData.ZipCode != updated.ZipCode)
|
|
{
|
|
existData.Name = updated.Name;
|
|
existData.ZipCode = updated.ZipCode;
|
|
existData.LastUpdatedAt = DateTime.Now;
|
|
existData.LastUpdateUserId = UserId ?? "";
|
|
existData.LastUpdateFullName = FullName ?? "";
|
|
}
|
|
|
|
if (existData.IsActive != updated.IsActive)
|
|
{
|
|
existData.IsActive = updated.IsActive;
|
|
existData.LastUpdatedAt = DateTime.Now;
|
|
existData.LastUpdateUserId = UserId ?? "";
|
|
existData.LastUpdateFullName = FullName ?? "";
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task CreateAsync(SubDistrict inserted, string districtId)
|
|
{
|
|
var district = await _context.Districts.FirstOrDefaultAsync(x => x.Id == Guid.Parse(districtId));
|
|
inserted.CreatedUserId = UserId ?? "";
|
|
inserted.CreatedFullName = FullName ?? "System Administrator";
|
|
inserted.CreatedAt = DateTime.Now;
|
|
inserted.LastUpdatedAt = DateTime.Now;
|
|
inserted.LastUpdateFullName = FullName ?? "System Administrator";
|
|
inserted.LastUpdateUserId = UserId ?? "";
|
|
inserted.District = district;
|
|
|
|
await _context.SubDistricts.AddAsync(inserted);
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |