示例#1
0
        /// <inheritdoc/>
        public async Task DeleteAsync(
            SourceFormat sourceFormat,
            CancellationToken cancellationToken = default)
        {
            try
            {
                Check.IsNotNull(sourceFormat);
                await _masterDataValidators.SourceFormatValidator.ValidateAsync(sourceFormat, o =>
                {
                    o.IncludeRuleSets(SourceFormatValidatorRulesets.Delete);
                    o.ThrowOnFailures();
                }, cancellationToken).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    SourceFormat result = await ctx.SourceFormats
                                          .Include(p => p.DimensionStructureNodes)
                                          .Include(pp => pp.SourceFormatDimensionStructureNode)
                                          .FirstOrDefaultAsync(
                        w => w.Id == sourceFormat.Id,
                        cancellationToken
                        )
                                          .ConfigureAwait(false);

                    if (result is null)
                    {
                        string msg = $"There is no {nameof(SourceFormat)} with id: {sourceFormat.Id}";
                        throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                    }

                    if (result.DimensionStructureNodes.Any())
                    {
                        foreach (DimensionStructureNode node in result.DimensionStructureNodes)
                        {
                            ctx.Entry(node).State = EntityState.Deleted;
                        }

                        await ctx.SaveChangesAsync(cancellationToken)
                        .ConfigureAwait(false);
                    }

                    if (result.SourceFormatDimensionStructureNode is not null)
                    {
                        ctx.Entry(result.SourceFormatDimensionStructureNode).State = EntityState.Deleted;
                        await ctx.SaveChangesAsync(cancellationToken)
                        .ConfigureAwait(false);
                    }

                    ctx.Entry(result).State = EntityState.Deleted;
                    await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                             $"{nameof(DeleteAsync)} operation failed! " +
                             "For further information see inner exception!";
                throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
            }
        }
示例#2
0
        private void Save()
        {
            using (var context = new MasterDataContext())
            {
                if (!Validate(context))
                {
                    return;
                }

                var existingSupplier = context.Suppliers
                                       .Include(s => s.Contacts)
                                       .FirstOrDefault(s => s.Id == CurrentSupplier.Id);

                if (existingSupplier == null)
                {
                    context.Add(CurrentSupplier);
                }
                else
                {
                    context.Entry(existingSupplier).CurrentValues.SetValues(CurrentSupplier);
                    foreach (var contact in CurrentSupplier.Contacts)
                    {
                        var existingContact = existingSupplier.Contacts
                                              .FirstOrDefault(c => c.Id == contact.Id);

                        if (existingContact == null)
                        {
                            existingSupplier.Contacts.Add(contact);
                        }
                        else
                        {
                            context.Entry(existingContact).CurrentValues.SetValues(contact);
                        }
                    }

                    foreach (var contact in existingSupplier.Contacts)
                    {
                        if (!CurrentSupplier.Contacts.Any(c => c.Id == contact.Id))
                        {
                            context.Remove(contact);
                        }
                    }
                }

                context.SaveChanges();
                NotificationRequest.Raise(new Notification {
                    Content = "保存成功", Title = "提示"
                });
                IsEdit = true;
            }
        }
示例#3
0
        /// <inheritdoc/>
        public async Task <SourceFormat> UpdateAsync(
            SourceFormat sourceFormat,
            CancellationToken cancellationToken = default)
        {
            try
            {
                Check.IsNotNull(sourceFormat);

                await _masterDataValidators.SourceFormatValidator.ValidateAsync(sourceFormat, o =>
                {
                    o.IncludeProperties(SourceFormatValidatorRulesets.Update);
                    o.ThrowOnFailures();
                }, cancellationToken).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    SourceFormat target = await ctx.SourceFormats
                                          .FirstOrDefaultAsync(w => w.Id == sourceFormat.Id, cancellationToken)
                                          .ConfigureAwait(false);

                    if (target != null)
                    {
                        target.Name     = sourceFormat.Name;
                        target.Desc     = sourceFormat.Desc;
                        target.IsActive = sourceFormat.IsActive;

                        ctx.Entry(target).State = EntityState.Modified;
                        await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                        return(await ctx.SourceFormats
                               .AsNoTracking()
                               .FirstOrDefaultAsync(w => w.Id == sourceFormat.Id, cancellationToken)
                               .ConfigureAwait(false));
                    }

                    string msg = $"There is no {nameof(SourceFormat)} entity in the system with " +
                                 $"id: {sourceFormat.Id}.";
                    throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                }
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                             $"{nameof(UpdateAsync)} operation failed! " +
                             "For further information see inner exception!";
                throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
            }
        }
示例#4
0
        /// <inheritdoc/>
        public async Task <DimensionStructure> UpdateAsync(
            DimensionStructure dimensionStructure,
            CancellationToken cancellationToken = default)
        {
            try
            {
                Check.IsNotNull(dimensionStructure);

                await _masterDataValidators.DimensionStructureValidator.ValidateAsync(dimensionStructure, o =>
                {
                    o.IncludeRuleSets(DimensionStructureValidatorRulesets.Update);
                    o.ThrowOnFailures();
                }, cancellationToken).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    DimensionStructure toBeModified = await ctx.DimensionStructures
                                                      .FirstOrDefaultAsync(w => w.Id == dimensionStructure.Id, cancellationToken)
                                                      .ConfigureAwait(false);

                    string msg = $"There is no {typeof(DimensionStructure)} " +
                                 $"entity with id: {dimensionStructure.Id}";
                    Check.IsNotNull(toBeModified, msg);

                    toBeModified.Name     = dimensionStructure.Name;
                    toBeModified.Desc     = dimensionStructure.Desc;
                    toBeModified.IsActive = dimensionStructure.IsActive;

                    ctx.Entry(toBeModified).State = EntityState.Modified;
                    await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                    return(toBeModified);
                }
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataDimensionStructureBusinessLogic)}." +
                             $"{nameof(UpdateAsync)} operation failed. " +
                             $"For further information see inner exception.";
                throw new MasterDataBusinessLogicDimensionStructureDatabaseOperationException(msg, e);
            }
        }
        /// <inheritdoc/>
        public async Task InactivateAsync(
            DimensionStructure dimensionStructure,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
            {
                try
                {
                    Check.IsNotNull(dimensionStructure);

                    await _masterDataValidators.DimensionStructureValidator.ValidateAsync(
                        dimensionStructure, options =>
                    {
                        options.IncludeRuleSets(DimensionStructureValidatorRulesets.Inactivate);
                        options.ThrowOnFailures();
                    }, cancellationToken)
                    .ConfigureAwait(false);

                    DimensionStructure toBeDeleted = await ctx.DimensionStructures.FirstOrDefaultAsync(
                        w => w.Id == dimensionStructure.Id,
                        cancellationToken)
                                                     .ConfigureAwait(false);

                    string msg = $"There is no {nameof(DimensionStructure)} entity " +
                                 $"with id: {dimensionStructure.Id}.";
                    Check.IsNotNull(toBeDeleted, msg);

                    toBeDeleted.IsActive         = 0;
                    ctx.Entry(toBeDeleted).State = EntityState.Modified;
                    await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    string msg = $"{nameof(MasterDataDimensionStructureBusinessLogic)}." +
                                 $"{nameof(InactivateAsync)} operation failed! " +
                                 $"For further info see inner exception!";
                    throw new MasterDataBusinessLogicDimensionStructureDatabaseOperationException(msg, e);
                }
            }
        }
        private async Task DeleteChildNodesOfDimensionStructureNodeAsync(
            DimensionStructureNode tree,
            MasterDataContext ctx,
            CancellationToken cancellationToken)
        {
            try
            {
                if (tree.ChildNodes.Any())
                {
                    foreach (DimensionStructureNode treeChildNode in tree.ChildNodes)
                    {
                        await DeleteChildNodesOfDimensionStructureNodeAsync(
                            treeChildNode,
                            ctx,
                            cancellationToken)
                        .ConfigureAwait(false);
                    }
                }

                DimensionStructureNode dimensionStructureNode = await ctx.DimensionStructureNodes
                                                                .FirstAsync(
                    w => w.Id == tree.Id,
                    cancellationToken)
                                                                .ConfigureAwait(false);

                ctx.Entry(dimensionStructureNode).State = EntityState.Deleted;
                await ctx.SaveChangesAsync(cancellationToken)
                .ConfigureAwait(false);
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                             $"{nameof(DeleteChildNodesOfDimensionStructureNodeAsync)} operation has failed. " +
                             $"For further information see inner exception.";
                throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
            }
        }
        public async Task <Dimension> UpdateDimensionAsync(Dimension dimension)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
            {
                try
                {
                    string msg = $"{nameof(dimension)} is null.";
                    Check.IsNotNull(dimension, msg);

                    await _masterDataValidators.DimensionValidator.ValidateAsync(dimension, o =>
                    {
                        o.IncludeRuleSets(ValidatorRulesets.UpdateDimension);
                        o.ThrowOnFailures();
                    }).ConfigureAwait(false);

                    Dimension toBeModified = await ctx.Dimensions.FindAsync(dimension.Id)
                                             .ConfigureAwait(false);

                    string tobeModifiedErrorMessage = $"No Dimension entity with id: {dimension.Id}";
                    Check.IsNotNull(toBeModified, tobeModifiedErrorMessage);

                    toBeModified.Name        = dimension.Name;
                    toBeModified.Description = dimension.Description;
                    toBeModified.IsActive    = dimension.IsActive;

                    ctx.Entry(toBeModified).State = EntityState.Modified;
                    await ctx.SaveChangesAsync().ConfigureAwait(false);

                    return(toBeModified);
                }
                catch (Exception e)
                {
                    throw new MasterDataBusinessLogicUpdateDimensionAsyncOperationException(e.Message, e);
                }
            }
        }
示例#8
0
        /// <inheritdoc/>
        public async Task InactivateAsync(
            SourceFormat sourceFormat,
            CancellationToken cancellationToken = default)
        {
            try
            {
                Check.IsNotNull(sourceFormat);
                await _masterDataValidators.SourceFormatValidator.ValidateAsync(sourceFormat, o =>
                {
                    o.IncludeRuleSets(SourceFormatValidatorRulesets.Inactivate);
                    o.ThrowOnFailures();
                }, cancellationToken).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    SourceFormat result = await ctx.SourceFormats.FirstOrDefaultAsync(
                        w => w.Id == sourceFormat.Id,
                        cancellationToken)
                                          .ConfigureAwait(false);

                    if (result != null)
                    {
                        result.IsActive         = 0;
                        ctx.Entry(result).State = EntityState.Modified;
                        await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                             $"{nameof(InactivateAsync)} operation failed! " +
                             $"For further information see inner exception!";
                throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
            }
        }
        /// <inheritdoc/>
        public async Task DeleteRootDimensionStructureNodeAsync(
            long dimensionStructureNodeId,
            long sourceFormatId,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                using (IDbContextTransaction transaction = await ctx.Database
                                                           .BeginTransactionAsync(cancellationToken)
                                                           .ConfigureAwait(false))
                {
                    try
                    {
                        Check.AreNotEqual(dimensionStructureNodeId, 0);
                        Check.AreNotEqual(sourceFormatId, 0);

                        DimensionStructureNode rootNode = await ctx.DimensionStructureNodes
                                                          .AsNoTracking()
                                                          .Include(i => i.SourceFormatDimensionStructureNode)
                                                          .ThenInclude(ii => ii.SourceFormat)
                                                          .Include(iii => iii.ChildNodes)
                                                          .FirstOrDefaultAsync(
                            w => w.Id == dimensionStructureNodeId &&
                            w.SourceFormatDimensionStructureNode.SourceFormatId == sourceFormatId,
                            cancellationToken)
                                                          .ConfigureAwait(false);

                        if (rootNode == null)
                        {
                            string msg = $"There is no {nameof(DimensionStructureNode)} with Id: " +
                                         $"{dimensionStructureNodeId}.";
                            throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                        }

                        SourceFormatDimensionStructureNode sourceFormatDimensionStructureNode = await ctx
                                                                                                .SourceFormatDimensionStructureNodes
                                                                                                .FirstOrDefaultAsync(
                            w => w.Id == rootNode.Id,
                            cancellationToken)
                                                                                                .ConfigureAwait(false);

                        ctx.Entry(sourceFormatDimensionStructureNode).State = EntityState.Deleted;
                        await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                        if (rootNode.ChildNodes.Any())
                        {
                            DimensionStructureNode rootNodeWithoutChildren = new DimensionStructureNode
                            {
                                Id = rootNode.Id,
                            };

                            DimensionStructureNode tree = await GetDimensionStructureNodeTreeAsync(
                                rootNodeWithoutChildren, ctx)
                                                          .ConfigureAwait(false);
                            await DeleteChildNodesOfDimensionStructureNodeAsync(
                                tree,
                                ctx,
                                cancellationToken)
                            .ConfigureAwait(false);
                        }

                        DimensionStructureNode refresh = await ctx.DimensionStructureNodes
                                                         .FirstOrDefaultAsync(
                            w => w.Id == dimensionStructureNodeId,
                            cancellationToken)
                                                         .ConfigureAwait(false);

                        if (refresh != null)
                        {
                            ctx.Entry(refresh).State = EntityState.Deleted;
                            await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                        }

                        await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        await transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);

                        string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                                     $"{nameof(DeleteRootDimensionStructureNodeAsync)} operation has failed. " +
                                     "For further information see inner exception.";
                        throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
                    }
                }
        }
        /// <inheritdoc/>
        public async Task AddRootDimensionStructureNodeAsync(
            long sourceFormatId,
            long dimensionStructureNodeId,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                using (IDbContextTransaction transaction = await ctx.Database
                                                           .BeginTransactionAsync(cancellationToken)
                                                           .ConfigureAwait(false))
                {
                    try
                    {
                        Check.AreNotEqual(sourceFormatId, 0);
                        Check.AreNotEqual(dimensionStructureNodeId, 0);

                        SourceFormat sourceFormat = await ctx.SourceFormats
                                                    .Include(root => root.SourceFormatDimensionStructureNode)
                                                    .FirstOrDefaultAsync(k => k.Id == sourceFormatId, cancellationToken)
                                                    .ConfigureAwait(false);

                        DimensionStructureNode dimensionStructureNode = await ctx.DimensionStructureNodes
                                                                        .FirstOrDefaultAsync(k => k.Id == dimensionStructureNodeId, cancellationToken)
                                                                        .ConfigureAwait(false);

                        if (sourceFormat is null)
                        {
                            string msg = $"No {nameof(SourceFormat)} with id: {sourceFormatId}.";
                            throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                        }

                        if (dimensionStructureNode is null)
                        {
                            string msg = $"No {nameof(DimensionStructureNode)} with id: {dimensionStructureNodeId}.";
                            throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                        }

                        if (sourceFormat.SourceFormatDimensionStructureNode is not null)
                        {
                            string msg = $"{nameof(SourceFormat)}(${sourceFormat.Id}) already has " +
                                         $"root ${nameof(DimensionStructureNode)}.";
                            throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                        }

                        dimensionStructureNode.SourceFormat     = sourceFormat;
                        ctx.Entry(dimensionStructureNode).State = EntityState.Modified;

                        SourceFormatDimensionStructureNode sourceFormatDimensionStructureNode =
                            new SourceFormatDimensionStructureNode
                        {
                            DimensionStructureNode = dimensionStructureNode,
                            SourceFormat           = sourceFormat,
                        };

                        await ctx.AddAsync(sourceFormatDimensionStructureNode, cancellationToken)
                        .ConfigureAwait(false);

                        await ctx.SaveChangesAsync(cancellationToken)
                        .ConfigureAwait(false);

                        await transaction.CommitAsync(cancellationToken)
                        .ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        await transaction.RollbackAsync(cancellationToken)
                        .ConfigureAwait(false);

                        string msg = $"{nameof(AddRootDimensionStructureNodeAsync)} operation failed!" +
                                     $" For further information see inner exception.";
                        throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
                    }
                }
        }
示例#11
0
 public void Edit(T entity)
 {
     _dbContext.Entry(entity).State = EntityState.Modified;
     _dbContext.SaveChanges();
 }
        public async Task <DimensionValue> ModifyDimensionValueAsync(
            long dimensionId,
            DimensionValue oldDimensionValue,
            DimensionValue newDimensionValue)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
            {
                using (IDbContextTransaction transaction = await ctx.Database.BeginTransactionAsync()
                                                           .ConfigureAwait(false))
                {
                    try
                    {
                        string dimensionIdErrorMsg = $"{nameof(dimensionId)} is zero.";
                        Check.AreNotEqual(dimensionId, 0, dimensionIdErrorMsg);

                        string oldDimensionValueErrorMsg = $"{nameof(oldDimensionValue)} is null.";
                        Check.IsNotNull(oldDimensionValue, oldDimensionValueErrorMsg);

                        string newDimensionValueErrorMsg = $"{nameof(newDimensionValue)} is null.";
                        Check.IsNotNull(newDimensionValue, newDimensionValueErrorMsg);

                        await _masterDataValidators.DimensionValueValidator.ValidateAsync(oldDimensionValue, o =>
                        {
                            o.IncludeRuleSets(ValidatorRulesets.ModifyDimensionValue);
                            o.ThrowOnFailures();
                        }).ConfigureAwait(false);

                        DomainModel.Dimension dim = await ctx.Dimensions.FindAsync(dimensionId).ConfigureAwait(false);

                        string noDimErrMsg = $"There is no dimension with id: {dimensionId}";
                        Check.IsNotNull(dim, noDimErrMsg);

                        DimensionValue dimVal = await ctx.DimensionValues.FindAsync(oldDimensionValue.Id)
                                                .ConfigureAwait(false);

                        string dimValErrMsg = $"There is no dimension value with id: {oldDimensionValue.Id}";
                        Check.IsNotNull(dimVal, dimValErrMsg);

                        // count how many dimension - dimension value relation exists
                        List <DimensionDimensionValue> countOfDimensionDimensionValueRelation = await ctx
                                                                                                .DimensionDimensionValues
                                                                                                .Where(p => p.DimensionValueId == oldDimensionValue.Id &&
                                                                                                       p.DimensionId != dimensionId)
                                                                                                .ToListAsync().ConfigureAwait(false);

                        if (countOfDimensionDimensionValueRelation.Any())
                        {
                            // If multiple dimensions references to the given dimension value
                            // then we are going to create a new dimension value and we are going to modify
                            // the dimension - dimension value reference to that
                            DimensionValue modifiedButNewDimensionValue = new DimensionValue
                            {
                                Value = newDimensionValue.Value,
                            };
                            await ctx.DimensionValues.AddAsync(modifiedButNewDimensionValue).ConfigureAwait(false);

                            await ctx.SaveChangesAsync().ConfigureAwait(false);

                            DimensionDimensionValue theOneGoingToBeModified = await ctx.DimensionDimensionValues
                                                                              .FirstOrDefaultAsync(p => p.DimensionId == dimensionId &&
                                                                                                   p.DimensionValueId == oldDimensionValue.Id)
                                                                              .ConfigureAwait(false);

                            if (theOneGoingToBeModified == null)
                            {
                                string msg = $"There is no DimensionDimensionValue entity with " +
                                             $"dimension id: {dimensionId}, and" +
                                             $"dimension value id: {oldDimensionValue.Id}!";
                                throw new MasterDataBusinessLogicNoSuchDimensionDimensionValueEntity(msg);
                            }

                            theOneGoingToBeModified.DimensionValueId = modifiedButNewDimensionValue.Id;
                            ctx.Entry(theOneGoingToBeModified).State = EntityState.Modified;
                            await ctx.SaveChangesAsync().ConfigureAwait(false);

                            await transaction.CommitAsync().ConfigureAwait(false);

                            DimensionValue modifiedResult = await ctx.DimensionValues.FirstOrDefaultAsync(
                                p => p.Id == modifiedButNewDimensionValue.Id).ConfigureAwait(false);

                            return(modifiedResult);
                        }

                        DimensionValue dimValToBeModified = await ctx.DimensionValues.FirstOrDefaultAsync(
                            p => p.Id == oldDimensionValue.Id).ConfigureAwait(false);

                        if (dimValToBeModified == null)
                        {
                            string erMsg = $"There is no dimension value with id: {oldDimensionValue.Id}";
                            throw new MasterDataBusinessLogicNoSuchDimensionValueEntity(erMsg);
                        }

                        dimValToBeModified.Value            = newDimensionValue.Value;
                        ctx.Entry(dimValToBeModified).State = EntityState.Modified;
                        await ctx.SaveChangesAsync().ConfigureAwait(false);

                        await transaction.CommitAsync().ConfigureAwait(false);

                        return(dimValToBeModified);
                    }
                    catch (Exception e)
                    {
                        await transaction.CommitAsync().ConfigureAwait(false);

                        throw new MasterDataBusinessLogicModifyDimensionValueAsyncOperationException(
                                  e.Message, e);
                    }
                }
            }
        }