public override PersonRegion GetPersonRegionByPersonAndRegion(Person person, Region region)
 {
     using (var transaction = new TransactionScope(_configuration))
     {
         var prDS = new PersonRegionDataStore(transaction);
         return(prDS.FindByPersonAndRegion(person.Id, region.Id).FirstOrDefault());
     }
 }
 public override void UpdatePersonRegion(PersonRegion personRegion)
 {
     using (var transaction = new TransactionScope(_configuration))
     {
         var prDS = new PersonRegionDataStore(transaction);
         prDS.Update(personRegion);
         transaction.Commit();
     }
 }
        private IList <Region> GetRegionsByPerson(Person person, bool isAdmin, bool isManager, bool recursive)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var runningList          = new List <Region>();
                var ppDS                 = new PersonRegionDataStore(transaction);
                IList <PersonRegion> pps = ppDS.FindRegionsByPerson(person.Id);
                foreach (PersonRegion ppt in pps)
                {
                    if (isAdmin)
                    {
                        if (!ppt.IsAdministrator)
                        {
                            continue;
                        }
                    }
                    if (isManager)
                    {
                        if (!ppt.IsManager)
                        {
                            continue;
                        }
                    }

                    IEnumerable <Region> q = from x in runningList where x.Id.Equals(ppt.Region.Id) select x;
                    if (q.Count() == 0)
                    {
                        runningList.Add(ppt.Region);
                    }
                }
                if (recursive)
                {
                    // Find the sub regions, and add them to the runningList.
                    var toConcat = new List <Region>();
                    foreach (Region region in runningList)
                    {
                        foreach (Region childRegion in SiteManager.GetAllRegionsBelow(region))
                        {
                            if (runningList.FirstOrDefault(x => x.Id.Equals(childRegion.Id)) == null)
                            {
                                toConcat.Add(childRegion);
                            }
                        }
                    }
                    runningList.AddRange(toConcat);
                }
                runningList.Sort(new KeyComparer <Region>(x => x.Breadcrumb));
                return(runningList);
            }
        }
        public override void DeletePersonFromRegion(string personId, string regionId)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var prDS = new PersonRegionDataStore(transaction);
                IList <PersonRegion> prs = prDS.FindByPersonAndRegion(personId, regionId);

                foreach (PersonRegion item in prs)
                {
                    item.Deleted = true;
                    prDS.Update(item);
                }

                transaction.Commit();
            }
        }
        /// <summary>
        /// Modifies the PersonRegion record.
        /// If one of personId or regionId are null, then it is implied that all records for the not null item are to be updated.
        /// If one of isAdministrator or isManager are null, then it is implied that the not null item is to be updated.
        /// If a record is updated so that both isAdministrator and isManager are false, then that record is deleted.
        /// </summary>
        /// <param name="personId"></param>
        /// <param name="regionId"></param>
        /// <param name="isAdministrator"></param>
        /// <param name="isManager"></param>
        public override void UpdatePersonRegion(string personId, bool?isAdministrator, bool?isManager)
        {
            if (isAdministrator == null && isManager == null)
            {
                throw new ArgumentNullException("isAdministrator", "Both isAdministrator and isManager cannot be null");
            }

            using (var transaction = new TransactionScope(_configuration))
            {
                var prDS = new PersonRegionDataStore(transaction);
                IList <PersonRegion> prs = prDS.FindRegionsByPerson(personId);

                foreach (PersonRegion pr in prs)
                {
                    // A value of null for isAdministrator and isManager is equivalent to false.
                    if (!isAdministrator.HasValue)
                    {
                        // Only modify the manager values
                        pr.IsManager = (bool)isManager;

                        if (!pr.IsAdministrator && !pr.IsManager)
                        {
                            prDS.Delete(pr.Id);
                        }
                        else
                        {
                            prDS.Update(pr);
                        }
                    }
                    else if (!isManager.HasValue)
                    {
                        // Only modify the manager values
                        pr.IsAdministrator = (bool)isAdministrator;

                        if (!pr.IsAdministrator && !pr.IsManager)
                        {
                            prDS.Delete(pr.Id);
                        }
                        else
                        {
                            prDS.Update(pr);
                        }
                    }
                }
                transaction.Commit();
            }
        }
        public override bool IsPersonInPersonRegion(string personId)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var ds = new PersonRegionDataStore(transaction);
                IList <PersonRegion> peoples = ds.FindRegionsByPerson(personId);

                if (peoples.Count > 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        public override void CreatePersonRegion(string personId, string regionId, bool?isAdministrator, bool?isManager)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var psDS = new PersonRegionDataStore(transaction);
                var pr   = new PersonRegion();
                var pDS  = new PersonDataStore(transaction);
                pr.Person = pDS.FindByKey(personId);
                var rDS = new RegionDataStore(transaction);
                pr.Region = rDS.FindByKey(regionId);
                if (isAdministrator != null)
                {
                    pr.IsAdministrator = (bool)isAdministrator;
                }
                if (isManager != null)
                {
                    pr.IsManager = (bool)isManager;
                }
                psDS.Insert(pr);

                transaction.Commit();
            }
        }