Esempio n. 1
0
        public async Task<BlResult<IEnumerable<NodeModel>>> GetByGraphsAsync(int graphId)
        {
            BlResult<IEnumerable<NodeModel>> result = new BlResult<IEnumerable<NodeModel>>();

            try
            {
                if (graphId == 0)
                {
                    throw new ArgumentException(nameof(graphId));
                }

                //EnsureTransaction();

                var entity = await _nodeRepository.GetNodesByGraphIdAsync(graphId);
                IEnumerable<NodeModel> nodeModels = _mapper.Map<IEnumerable<NodeModel>>(entity);
                result.Success(nodeModels);
            }
            catch (ArgumentException)
            {
                result.Fail(BLErrorCodeTypeEnum.CantFetchUncreatedEntity);
            }
            catch (Exception ex)
            {
                result.Fail(BLErrorCodeTypeEnum.Unknown, ex.Message, ex);
            }

            return result;
        }
        public async Task <BlResult> DeleteAsync(int id)
        {
            BlResult blResult = new BlResult();

            try
            {
                var entity = await _connectionTypeRepository.GetByIdWithReferencAsync(id);

                if (entity != null)
                {
                    EnsureTransaction();
                    _connectionTypeRepository.Delete(id);
                    await SaveChangesAsync();

                    blResult.Success();
                }
                else
                {
                    throw new ArgumentException();
                }
            }
            catch (ArgumentException)
            {
                blResult.Fail(BLErrorCodeTypeEnum.NoSuchEntityExistsWithID);
            }
            catch (Exception ex)
            {
                blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex);
            }

            return(blResult);
        }
        public async Task <BlResult <AttributeDescriptionModel> > SaveAsync(AttributeDescriptionModel attributeDescriptionModel)
        {
            BlResult <AttributeDescriptionModel> blResult = new BlResult <AttributeDescriptionModel>();

            try
            {
                if (attributeDescriptionModel is null)
                {
                    throw new ArgumentNullException();
                }

                if (attributeDescriptionModel.Id == 0 && await _attribuetDescriptionRepository.ExistsEntityWithSuchAName(attributeDescriptionModel.Name))
                {
                    throw new Exception("Duplicated Attribute Name");
                }

                EnsureTransaction();

                var fetchedEntity = await _attribuetDescriptionRepository.GetByIdAsync(attributeDescriptionModel.Id);

                fetchedEntity = _mapper.Map(attributeDescriptionModel, fetchedEntity);

                if (attributeDescriptionModel.Id > 0)
                {
                    _attribuetDescriptionRepository.Edit(fetchedEntity);
                }
                else
                {
                    fetchedEntity = _attribuetDescriptionRepository.Add(fetchedEntity);
                }

                await SaveChangesAsync();

                blResult.Success(_mapper.Map <AttributeDescriptionModel>(fetchedEntity));
            }
            catch (ArgumentNullException)
            {
                blResult.Fail(ConstDictionary.CantSaveNullModel);
            }
            catch (Exception ex)
            {
                blResult.Fail(ex);
            }

            return(blResult);
        }
        public async Task <BlResult <ConnectionModel> > SaveAsync(ConnectionModel connectionModel)
        {
            BlResult <ConnectionModel> blResult = new BlResult <ConnectionModel>();

            try
            {
                if (connectionModel is null)
                {
                    throw new ArgumentNullException(nameof(connectionModel));
                }

                EnsureTransaction();

                var fetchedEntity = await _connectionRepository.GetByIdWithReferencAsync(connectionModel.Id);

                fetchedEntity = _mapper.Map(connectionModel, fetchedEntity);
                fetchedEntity.OverwritesId = null;

                if (connectionModel.Id > 0)
                {
                    _connectionRepository.Edit(fetchedEntity);
                }
                else
                {
                    fetchedEntity = _connectionRepository.Add(fetchedEntity);
                }

                await SaveChangesAsync();

                blResult.Success(_mapper.Map <ConnectionModel>(fetchedEntity));
            }
            catch (ArgumentNullException)
            {
                blResult.Fail(BLErrorCodeTypeEnum.ArgumentIsNull);
            }
            catch (Exception ex)
            {
                blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex.Message, ex);
            }

            return(blResult);
        }
Esempio n. 5
0
        public async Task<BlResult> DeleteAsync(int id)
        {
            BlResult blResult = new BlResult();

            try
            {
                var entity = await _nodeRepository.GetByIdWithReferencAsync(id);

                if (entity != null)
                {
                    if (entity.ConnectionsFrom.Count + entity.ConnectionsTo.Count > 0)
                    {
                        blResult.Fail(ConstDictionary.CantDeleteNodeWithConnections);
                    }
                    else
                    {
                        EnsureTransaction();
                        _nodeRepository.Delete(id);
                        await SaveChangesAsync();

                        blResult.Success();
                    }
                }
                else
                {
                    throw new ArgumentException();
                }
            }
            catch (ArgumentException)
            {
                blResult.Fail(BLErrorCodeTypeEnum.NoSuchEntityExistsWithID);
            }
            catch (Exception ex)
            {
                blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex);
            }

            return blResult;
        }
Esempio n. 6
0
        public async Task<BlResult<NodeModel>> GetByIdAsync(int id)
        {
            BlResult<NodeModel> blResult = new BlResult<NodeModel>();

            try
            {
                var entity = await _nodeRepository.GetByIdWithReferencAsync(id);
                blResult.Success(_mapper.Map<NodeModel>(entity));
            }
            catch (Exception ex)
            {
                blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex);
            }

            return blResult;
        }
Esempio n. 7
0
        public async Task<BlResult<Dictionary<int, string>>> GetNodesIdAndNameAsync()
        {
            BlResult<Dictionary<int, string>> result = new BlResult<Dictionary<int, string>>();

            try
            {
                var entity = await _nodeRepository.GetNodesIdAndNameAsync();
                result.Success(entity);
            }
            catch (Exception ex)
            {
                result.Fail(BLErrorCodeTypeEnum.Unknown, ex.Message, ex);
            }

            return result;
        }
        public async Task <BlResult <ConnectionTypeModel> > GetByIdAsync(int id)
        {
            BlResult <ConnectionTypeModel> blResult = new BlResult <ConnectionTypeModel>();

            try
            {
                var entity = await _connectionTypeRepository.GetByIdAsync(id);

                blResult.Success(_mapper.Map <ConnectionTypeModel>(entity));
            }
            catch (Exception ex)
            {
                blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex);
            }

            return(blResult);
        }
        public async Task <BlResult <IEnumerable <ConnectionTypeModel> > > GetAllAsync()
        {
            BlResult <IEnumerable <ConnectionTypeModel> > blResult = new BlResult <IEnumerable <ConnectionTypeModel> >();

            try
            {
                var entity = await _connectionTypeRepository.GetAllWithReferencsAsync();

                blResult.Success(_mapper.Map <IEnumerable <ConnectionTypeModel> >(entity));
            }
            catch (Exception ex)
            {
                blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex);
            }

            return(blResult);
        }
        public async Task <BlResult <IEnumerable <AttributeDescriptionModel> > > GetAllAsync()
        {
            BlResult <IEnumerable <AttributeDescriptionModel> > blResult = new BlResult <IEnumerable <AttributeDescriptionModel> >();

            try
            {
                var entity = await _attribuetDescriptionRepository.GetAllAsync();

                blResult.Success(_mapper.Map <IEnumerable <AttributeDescriptionModel> >(entity));
            }
            catch (Exception ex)
            {
                blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex);
            }

            return(blResult);
        }
        public async Task <BlResult <IEnumerable <SimpleModel> > > GetAllByNameAndIdAsync()
        {
            BlResult <IEnumerable <SimpleModel> > blResult = new BlResult <IEnumerable <SimpleModel> >();

            try
            {
                var entity = await _connectionRepository.GetConnectionsByNameAndIdAsync();

                var result = entity.ToList().Select(x => new SimpleModel(x.Key, x.Value)).ToList();
                blResult.Success(result);
            }
            catch (Exception ex)
            {
                blResult.Fail(ex);
            }

            return(blResult);
        }
        public async Task <BlResult <ConnectionTypeModel> > SaveAsync(ConnectionTypeModel connectionTypeModel)
        {
            BlResult <ConnectionTypeModel> blResult = new BlResult <ConnectionTypeModel>();

            try
            {
                if (connectionTypeModel is null)
                {
                    throw new ArgumentException();
                }

                EnsureTransaction();

                var fetchedEntity = await _connectionTypeRepository.GetByIdAsync(connectionTypeModel.Id);

                fetchedEntity = _mapper.Map(connectionTypeModel, fetchedEntity);
                var attributesToDelete = fetchedEntity.ConnectionTypeAttributes.
                                         Where(x => !connectionTypeModel.ConnectionTypeAttributes.Any(y => y.Id == x.Id)).ToList();
                var attributesToAdd = connectionTypeModel.ConnectionTypeAttributes.
                                      Where(x => !fetchedEntity.ConnectionTypeAttributes.Any(y => y.Id == x.Id)).Select(z => z.Id);

                if (connectionTypeModel.Id > 0)
                {
                    _connectionTypeRepository.Edit(fetchedEntity);
                }
                else
                {
                    fetchedEntity = _connectionTypeRepository.Add(fetchedEntity);
                }

                await SaveChangesAsync(false);

                if (attributesToDelete != null && attributesToDelete.Count() > 0)
                {
                    foreach (var item in attributesToDelete)
                    {
                        fetchedEntity.ConnectionTypeAttributes.Remove(item);
                    }
                    await SaveChangesAsync(false);
                }

                if (attributesToAdd != null && attributesToAdd.Count() > 0)
                {
                    await _connectionTypeRepository.AddAttribuets(fetchedEntity, attributesToAdd);
                    await SaveChangesAsync(false);
                }

                await SaveChangesAsync();

                blResult.Success(_mapper.Map <ConnectionTypeModel>(fetchedEntity));
            }
            catch (ArgumentException)
            {
                blResult.Fail(ConstDictionary.CantSaveNullModel);
            }
            catch (Exception ex)
            {
                blResult.Fail(ex);
            }

            return(blResult);
        }
Esempio n. 13
0
        public async Task<BlResult<NodeModel>> SaveAsync(NodeModel nodeModel)
        {
            BlResult<NodeModel> blResult = new BlResult<NodeModel>();

            try
            {
                if (nodeModel is null)
                {
                    throw new ArgumentNullException(nameof(NodeModel));
                }

                EnsureTransaction();

                var fetchedEntity = await _nodeRepository.GetByIdAsync(nodeModel.Id);
                fetchedEntity = _mapper.Map(nodeModel, fetchedEntity);
                var attributesToDelete = fetchedEntity.NodeAttributes.
                  Where(x => !nodeModel.NodeAttributes.Any(y => y.Id == x.Id)).ToList();
                var attributesToAdd = nodeModel.NodeAttributes.
                    Where(x => !fetchedEntity.NodeAttributes.Any(y => y.Id == x.Id)).Select(z => z.Id);

                if (nodeModel.Id > 0)
                {
                    _nodeRepository.Edit(fetchedEntity);
                }
                else
                {
                    fetchedEntity = _nodeRepository.Add(fetchedEntity);
                }

                await SaveChangesAsync(false);

                if (attributesToDelete != null && attributesToDelete.Count() > 0)
                {
                    foreach (var item in attributesToDelete)
                    {
                        fetchedEntity.NodeAttributes.Remove(item);
                    }
                    await SaveChangesAsync(false);
                }

                if (attributesToAdd != null && attributesToAdd.Count() > 0)
                {
                    await _nodeRepository.AddAttribuets(fetchedEntity, attributesToAdd);
                    await SaveChangesAsync(false);
                }

                await SaveChangesAsync();

                blResult.Success(_mapper.Map<NodeModel>(fetchedEntity));

            }
            catch (ArgumentNullException)
            {
                blResult.Fail(BLErrorCodeTypeEnum.ArgumentIsNull);
            }
            catch (Exception ex)
            {
                blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex.Message, ex);
            }

            return blResult;
        }
        public async Task <BlResult <GraphModel> > SaveAsync(GraphModel graphModel)
        {
            BlResult <GraphModel> blResult = new BlResult <GraphModel>();

            try
            {
                if (graphModel is null)
                {
                    throw new ArgumentException();
                }

                EnsureTransaction();

                var fetchedEntity = await _graphRepository.GetByIdWithReferencAsync(graphModel.Id);

                fetchedEntity = _mapper.Map(graphModel, fetchedEntity);

                if (graphModel.Id > 0)
                {
                    _graphRepository.Edit(fetchedEntity);
                }
                else
                {
                    fetchedEntity = _graphRepository.Add(fetchedEntity);
                    await SaveChangesAsync();

                    NodeModel nodeModel = new NodeModel()
                    {
                        Name           = fetchedEntity.Name,
                        GraphId        = fetchedEntity.Id,
                        Data           = "Entery node for graph " + fetchedEntity.Name,
                        NodeAttributes = new System.Collections.ObjectModel.ObservableCollection <AttributeDescriptionModel>()
                        {
                            new AttributeDescriptionModel()
                            {
                                Id = (int)AttributesTypesEnum.EnteryNode
                            }
                        }
                    };

                    if (fetchedEntity.IsAbstract)
                    {
                        nodeModel.NodeAttributes.Add(new AttributeDescriptionModel()
                        {
                            Id = (int)AttributesTypesEnum.Abstract
                        });
                    }

                    var result = await _nodeService.SaveAsync(nodeModel);

                    if (result.Succeed)
                    {
                        EnsureTransaction();
                        fetchedEntity.EnteryNodeId = result.Value.Id;
                        _graphRepository.Edit(fetchedEntity);
                    }
                    else
                    {
                        RollbackTransaction();
                        throw result.Exception;
                    }
                }

                await SaveChangesAsync();

                blResult.Success(_mapper.Map <GraphModel>(fetchedEntity));
            }
            catch (ArgumentException)
            {
                blResult.Fail(ConstDictionary.CantSaveNullModel);
            }
            catch (Exception ex)
            {
                blResult.Fail(ex);
            }

            return(blResult);
        }