using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Responses; using BMA.EHR.Domain.Models.Organizations; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; namespace BMA.EHR.Application.Repositories { public class OrganizationCommonRepository { #region " Fields " private readonly IApplicationDBContext _dbContext; private readonly IHttpContextAccessor _httpContextAccessor; #endregion #region " Constructor and Destuctor " public OrganizationCommonRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; } #endregion #region " Methods " public string GetOrganizationNameFullPath(Guid id, bool showRoot = false, bool descending = false,string seperator = " ") { try { var ocList = GetOCWithFullPath(id, showRoot); if (descending) ocList = ocList.OrderBy(x => x.Order).ToList(); var ret = String.Empty; foreach (var oc in ocList) { ret = oc.Name + seperator + ret; } if (ret.Length > 2) ret = ret.Substring(0, ret.Length - 1); return ret; } catch { throw; } } public List GetOCWithFullPath(Guid id, bool showRoot = false) { try { var ocList = new List(); var ocData = _dbContext.Set() .Include(x => x.OrganizationOrganization) .Include(x => x.Parent) .Select(x => new { Id = x.Id, Name = x.OrganizationOrganization!.Name, ParentId = x.Parent == null ? Guid.Empty : x.Parent!.Id }) .FirstOrDefault(x => x.Id == id); if (ocData != null) { ocList.Add(new OrganizationItem { Id = ocData.Id, Name = ocData.Name }); //if (!showRoot) //{ // if (!oc.IsRoot) // ocList.Add(new OrganizationItem { Id = oc.OCId, Name = oc.OrganizationName }); //} //else // ocList.Add(new OrganizationItem { Id = oc.OCId, Name = oc.OrganizationName }); if (ocData.ParentId != Guid.Empty) { ocList.AddRange(GetOCWithFullPath(ocData.ParentId, showRoot)); } } return ocList; } catch { throw; } } #endregion } }