Пример #1
0
        public async void TradingPostListingEntityControllerGetTest()
        {
            // Arrange
            using var controller = _serviceProvider.GetRequiredService <TradingPostListingEntityController>();
            var entities = new EntityFactory <TradingPostListingEntity>(10)
                           .UseAttributes()
                           .UseReferences()
                           .UseOwner(Guid.NewGuid())
                           .Generate()
                           .ToList();

            _database.AddRange(entities);
            await _database.SaveChangesAsync();

            // Act
            var data = await controller.Get(null, default);

            // Assert
            data.Data.Select(d => d.Id).Should().Contain(entities.Select(d => d.Id));
        }
Пример #2
0
        /// <inheritdoc />
        public async Task <ICollection <T> > Create <T>(
            ICollection <T> models,
            UpdateOptions options          = null,
            CancellationToken cancellation = default)
            where T : class, IOwnerAbstractModel, new()
        {
            var entityName = typeof(T).Name;
            await _identityService.RetrieveUserAsync();

            var dbSet = _dbContext.Set <T>();

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    await MergeReferences(models, options, cancellation);

                    foreach (var model in models)
                    {
                        // Update is used here so references are properly handled
                        dbSet.Update(model);
                    }

                    // Ensure that we create all of the base entities instead of updating
                    var addedEntries = _dbContext
                                       .ChangeTracker
                                       .Entries()
                                       .Where(entry => models.Contains(entry.Entity));
                    foreach (var entry in addedEntries)
                    {
                        entry.State = EntityState.Added;
                    }

                    var fileModels = _dbContext
                                     .ChangeTracker
                                     .Entries()
                                     .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified)
                                     .Select(e => e.Entity)
                                     .ToList();
                    if (options?.Files != null)
                    {
                        await SaveFiles(fileModels, options.Files, cancellation);
                    }
                    else
                    {
                        ClearFileAttributes(models);
                    }

                    await AssignModelMetaData(_identityService.User, cancellation);

                    ValidateModels(_dbContext.ChangeTracker
                                   .Entries()
                                   .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified)
                                   .Select(e => e.Entity));


                    var errors = await _securityService.CheckDbSecurityChanges(_identityService, _dbContext);

                    if (errors.Any())
                    {
                        throw new AggregateException(
                                  errors.Select(error => new InvalidOperationException(error)));
                    }

                    foreach (var entry in _dbContext.ChangeTracker.Entries <IAbstractModel>().ToList())
                    {
                        await entry.Entity.BeforeSave(entry.State, _dbContext, _serviceProvider);
                    }

                    var changes = _dbContext
                                  .ChangeTracker
                                  .Entries <IAbstractModel>()
                                  .Select(e => new ChangeState {
                        State = e.State, Entry = e
                    })
                                  .ToList();
                    await _dbContext.SaveChangesAsync(cancellation);

                    foreach (var change in changes)
                    {
                        await change.Entry.Entity.AfterSave(change.State, _dbContext, _serviceProvider, changes);
                    }

                    transaction.Commit();

                    return(models);
                }
                catch (Exception e)
                {
                    _logger.LogError("Error completing create action - {Error}", e.ToString());
                    throw;
                }
            }
        }