Exemplo n.º 1
0
        public async void CreateOrUpdateNodeInformationAsync(NodeVm nodeInfo)
        {
            try
            {
                using (MessengerDbContext context = contextFactory.Create())
                {
                    Node node = await context.Nodes
                                .Include(opt => opt.DomainNodes)
                                .Include(opt => opt.Ipnodes)
                                .FirstOrDefaultAsync(opt => opt.Id == nodeInfo.Id)
                                .ConfigureAwait(false);

                    if (node == null)
                    {
                        node = NodeConverter.GetNode(nodeInfo);
                        await context.Nodes.AddAsync(node).ConfigureAwait(false);
                    }
                    else
                    {
                        node = NodeConverter.GetNode(node, nodeInfo);
                        context.Nodes.Update(node);
                    }
                    await context.SaveChangesAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }
        private async Task HandleEditNodeBlockSegmentAsync(BlockSegmentVm segment)
        {
            using (MessengerDbContext _context = CreateContext())
            {
                using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
                {
                    try
                    {
                        EditNodeBlockData blockData = (EditNodeBlockData)segment.PublicData;
                        var editedNode = NodeConverter.GetNode(blockData.Node);
                        if (await _context.Nodes.AnyAsync(node => node.Id == editedNode.Id).ConfigureAwait(false))
                        {
                            _context.Nodes.Attach(editedNode);
                            _context.Nodes.Update(editedNode);
                            await _context.SaveChangesAsync().ConfigureAwait(false);

                            transaction.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        AddErrorMessage(nameof(HandleEditNodeBlockSegmentAsync), ex.ToString());
                        transaction.Rollback();
                    }
                }
            }
        }
Exemplo n.º 3
0
        public async Task NewOrEditNodeAsync(NodeVm targetNode)
        {
            try
            {
                using (MessengerDbContext context = contextFactory.Create())
                {
                    var node = await context.Nodes.FindAsync(targetNode.Id).ConfigureAwait(false);

                    if (node == null)
                    {
                        node = NodeConverter.GetNode(targetNode);
                        await context.AddAsync(node).ConfigureAwait(false);
                    }
                    else
                    {
                        node = NodeConverter.GetNode(targetNode);
                        context.Update(node);
                    }
                    await context.SaveChangesAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }