Example #1
0
        public UpdateComponentResponse UpdateComponent(Guid accountId, UpdateComponentRequestData data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var request = GetRequest <UpdateComponentRequest>(accountId);

            request.Data = data;
            var dispatcher = DispatcherHelper.GetDispatcherService();

            return(dispatcher.UpdateComponent(request));
        }
        public Component UpdateComponent(Guid accountId, UpdateComponentRequestData data)
        {
            // проверим существование компонента
            var oldComponentRequest = new AccountCacheRequest()
            {
                AccountId = accountId,
                ObjectId  = data.Id.Value
            };
            var oldComponent = AllCaches.Components.Find(oldComponentRequest);

            if (oldComponent == null)
            {
                throw new UnknownComponentIdException(data.Id.Value, accountId);
            }

            // проверим нового родителя
            if (data.ParentId.HasValue)
            {
                var parent = AllCaches.Components.Find(new AccountCacheRequest()
                {
                    AccountId = accountId,
                    ObjectId  = data.ParentId.Value
                });
                if (parent == null)
                {
                    throw new UnknownComponentIdException(data.ParentId.Value, accountId);
                }
            }

            // проверим уникальность системного имени
            if (!string.IsNullOrEmpty(data.SystemName))
            {
                var parentId = data.ParentId ?? oldComponent.ParentId.Value;
                var parent   = AllCaches.Components.Find(new AccountCacheRequest()
                {
                    AccountId = accountId,
                    ObjectId  = parentId
                });
                if (parent == null)
                {
                    throw new UnknownComponentIdException(parentId, accountId);
                }
                var child = parent.Childs.FindByName(data.SystemName);
                if (child != null && child.Id != data.Id)
                {
                    throw new ParameterErrorException("Системное имя должно быть уникальным");
                }
            }

            // проверим тип
            if (data.TypeId.HasValue)
            {
                if (!Context.ComponentTypeService.Contains(accountId, data.TypeId.Value))
                {
                    throw new ParameterErrorException("TypeId");
                }
            }

            var request = new AccountCacheRequest()
            {
                AccountId = accountId,
                ObjectId  = data.Id.Value
            };

            using (var component = AllCaches.Components.Write(request))
            {
                if (!string.IsNullOrEmpty(data.DisplayName))
                {
                    component.DisplayName = data.DisplayName;
                }

                if (!string.IsNullOrEmpty(data.SystemName))
                {
                    component.SystemName = data.SystemName;
                }

                if (data.ParentId.HasValue)
                {
                    component.ParentId = data.ParentId.Value;
                }

                if (data.TypeId.HasValue)
                {
                    component.ComponentTypeId = data.TypeId.Value;
                }

                if (!string.IsNullOrEmpty(data.Version))
                {
                    component.Version = data.Version;
                }

                component.BeginSave();
            }

            AllCaches.Components.SaveChanges();

            var accountDbContext = Context.GetAccountDbContext(accountId);
            var repository       = accountDbContext.GetComponentRepository();
            var componentObj     = repository.GetById(data.Id.Value);

            if (data.Properties != null && data.Properties.Count > 0)
            {
                UpdateComponentProperies(componentObj, data.Properties, accountId); //todo если передать пустой список, то все сотрется...
            }
            accountDbContext.SaveChanges();
            return(componentObj);
        }