示例#1
0
        /// <summary>
        /// Delete / Restore stat. units
        /// </summary>
        /// <param name="unitType">Type of stat. units</param>
        /// <param name="id">Id stat. units</param>
        /// <param name="toDelete">Remoteness flag</param>
        /// <param name="userId">User ID</param>
        public void DeleteUndelete(StatUnitTypes unitType, int id, bool toDelete, string userId)
        {
            if (_dataAccessService.CheckWritePermissions(userId, unitType))
            {
                throw new UnauthorizedAccessException();
            }

            var  item       = _commonSvc.GetStatisticalUnitByIdAndType(id, unitType, true).Result;
            bool isEmployee = _userService.IsInRoleAsync(userId, DefaultRoleNames.Employee).Result;
            var  mappedItem = Mapper.Map <IStatisticalUnit, ElasticStatUnit>(item);

            if (isEmployee)
            {
                var helper = new StatUnitCheckPermissionsHelper(_dbContext);
                helper.CheckRegionOrActivityContains(userId, mappedItem.RegionIds, mappedItem.ActivityCategoryIds);
            }
            if (item.IsDeleted == toDelete)
            {
                _elasticService.EditDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(item)).Wait();
            }
            else
            {
                CheckBeforeDelete(item, toDelete);
                var deletedUnit = _deleteUndeleteActions[unitType](id, toDelete, userId);
                _postDeleteActions[unitType](deletedUnit, toDelete, userId);

                _elasticService.EditDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(deletedUnit)).Wait();
            }
        }