Пример #1
0
        public static async Task <Account> GetAccountAsync(ClientsDb db, int accountId)
        {
            try
            {
                var account = await(
                    from a in db.Accounts
                    .Include(a => a.IdentityProviders)
                    .Include(a => a.Subscriptions)
                    .ThenInclude(s => s.IdentityProviders)
                    .ThenInclude(m => m.IdentityProvider)
                    where a.AccountId == accountId
                    select a
                    ).FirstOrDefaultAsync();

                if (account == null)
                {
                    throw new AccountNotFoundException($"An account with AccountId = {accountId} could not be found.");
                }

                return(account);
            }
            catch (DbException e)
            {
                throw new PersistenceException($"An error occurred while reading Account ({nameof(accountId)}={accountId})", e);
            }
        }
        public static ClientsDb CreateInMemoryClientDb()
        {
            var options = new DbContextOptionsBuilder <ClientsDb>()
                          .UseInMemoryDatabase(databaseName: $"InMemory:{DateTime.Now.GetHashCode()}{DateTime.UtcNow.Millisecond}")
                          .Options;

            var db = new ClientsDb(options);

            db.AccountTypes.Add(new AccountType {
                AccountTypeId = 1, Name = "Client", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });
            db.AccountTypes.Add(new AccountType {
                AccountTypeId = 2, Name = "Partner", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });
            db.AccountTypes.Add(new AccountType {
                AccountTypeId = 3, Name = "Referral", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });

            db.Archetypes.Add(new Archetype {
                ArchetypeId = 1, Name = "Basic", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });
            db.Archetypes.Add(new Archetype {
                ArchetypeId = 2, Name = "Segregated", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });
            db.Archetypes.Add(new Archetype {
                ArchetypeId = 3, Name = "Var", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });
            db.Archetypes.Add(new Archetype {
                ArchetypeId = 4, Name = "Hybrid", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });
            db.Archetypes.Add(new Archetype {
                ArchetypeId = 5, Name = "Enterprise", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });

            db.SubscriptionTypes.Add(new SubscriptionType {
                SubscriptionTypeId = 1, Name = "Production", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });
            db.SubscriptionTypes.Add(new SubscriptionType {
                SubscriptionTypeId = 2, Name = "Test", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });
            db.SubscriptionTypes.Add(new SubscriptionType {
                SubscriptionTypeId = 3, Name = "Demo", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });

            db.DataLinkTypes.Add(new DataLinkType {
                DataLinkTypeId = 1, Name = "Customization", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });
            db.DataLinkTypes.Add(new DataLinkType {
                DataLinkTypeId = 2, Name = "Activity", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
            });

            db.SaveChanges();
            return(db);
        }
Пример #3
0
        public static async Task <Subscription> GetSubscriptionAsync(ClientsDb db, int accountId, int subscriptionId)
        {
            try
            {
                var doesAccountExistsFuture = (
                    from a in db.Accounts
                    where a.AccountId == accountId
                    select 1
                    ).DeferredCount().FutureValue();

                var subscriptionFuture = (
                    from s in db.Subscriptions
                    .Include(s => s.IdentityProviders)
                    .ThenInclude(m => m.IdentityProvider)
                    where s.AccountId == accountId &&
                    s.SubscriptionId == subscriptionId
                    select s
                    ).DeferredFirstOrDefault().FutureValue();

                var doesAccountExist = await doesAccountExistsFuture.ValueAsync() > 0;

                var subscription = await subscriptionFuture.ValueAsync();

                if (!doesAccountExist)
                {
                    throw new AccountNotFoundException($"An account with AccountId {accountId} could not be found");
                }

                if (subscription == null)
                {
                    throw new SubscriptionNotFoundException($"A subscription with SubscriptionId {subscriptionId} could not be found");
                }

                return(subscription);
            }
            catch (DbException e)
            {
                throw new PersistenceException($"An error occurred while reading a DataLink ({nameof(accountId)} = {accountId}, {nameof(subscriptionId)} = {subscriptionId})", e);
            }
        }
Пример #4
0
        public static async Task <IdentityProvider> GetIdentityProviderAsync(ClientsDb db, int accountId, int identityProviderId)
        {
            try
            {
                var identityProvider = await(
                    from p in db.IdentityProviders
                    where p.IdentityProviderId == identityProviderId &&
                    p.AccountId == accountId
                    select p
                    ).FirstOrDefaultAsync();

                if (identityProvider == null)
                {
                    throw new IdentityProviderNotFoundException($"An identity provider with {nameof(IdentityProvider.IdentityProviderId)} = {identityProviderId} could not be found.");
                }

                return(identityProvider);
            }
            catch (DbException e)
            {
                throw new PersistenceException($"An error occurred while reading IdentityProvider ({nameof(identityProviderId)}={identityProviderId})", e);
            }
        }
 public UpdateIdentityProviderDelegate(ClientsDb db, IMapper mapper)
 {
     _db     = db;
     _mapper = mapper;
 }
 public GetDataLinkDelegate(ClientsDb db, IMapper mapper)
 {
     _db     = db;
     _mapper = mapper;
 }
Пример #7
0
 public UpdateAccountDelegate(ClientsDb db, IMapper mapper)
 {
     _db     = db;
     _mapper = mapper;
 }
        public static TestServer CreateTestServer(Dictionary <string, string> configurationEntries = null)
        {
            var databaseName = $"InMemory:{DateTime.Now}_{DateTime.UtcNow.Millisecond}";

            if (configurationEntries != null && configurationEntries.TryGetValue("ConnectionStrings:ClientsDbConnectionString", out var connectionstring) == true)
            {
                databaseName = connectionstring;
            }

            configurationEntries ??= new Dictionary <string, string>
            {
                ["ConnectionStrings:ClientsDbConnectionString"] = databaseName,
                ["DisableAuthenticationAndAuthorization"]       = "true",
                ["DisableHttpsRedirection"] = "true"
            };

            if (databaseName.StartsWith("InMemory:", StringComparison.OrdinalIgnoreCase))
            {
                var options = new DbContextOptionsBuilder <ClientsDb>()
                              .UseInMemoryDatabase(databaseName: databaseName.Replace("InMemory:", ""))
                              .Options;

                using var db = new ClientsDb(options);

                db.AccountTypes.Add(new AccountType {
                    AccountTypeId = 1, Name = "Client", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });
                db.AccountTypes.Add(new AccountType {
                    AccountTypeId = 2, Name = "Partner", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });
                db.AccountTypes.Add(new AccountType {
                    AccountTypeId = 3, Name = "Referral", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });

                db.Archetypes.Add(new Archetype {
                    ArchetypeId = 1, Name = "Basic", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });
                db.Archetypes.Add(new Archetype {
                    ArchetypeId = 2, Name = "Segregated", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });
                db.Archetypes.Add(new Archetype {
                    ArchetypeId = 3, Name = "Var", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });
                db.Archetypes.Add(new Archetype {
                    ArchetypeId = 4, Name = "Hybrid", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });
                db.Archetypes.Add(new Archetype {
                    ArchetypeId = 5, Name = "Enterprise", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });

                db.SubscriptionTypes.Add(new SubscriptionType {
                    SubscriptionTypeId = 1, Name = "Production", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });
                db.SubscriptionTypes.Add(new SubscriptionType {
                    SubscriptionTypeId = 2, Name = "Test", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });
                db.SubscriptionTypes.Add(new SubscriptionType {
                    SubscriptionTypeId = 3, Name = "Demo", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });

                db.DataLinkTypes.Add(new DataLinkType {
                    DataLinkTypeId = 1, Name = "Customization", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });
                db.DataLinkTypes.Add(new DataLinkType {
                    DataLinkTypeId = 2, Name = "Activity", CreatedDate = DateTime.UtcNow, CreatedBy = "TomStChief"
                });

                db.SaveChanges();
            }

            var configuration  = new ConfigurationBuilder().AddInMemoryCollection(configurationEntries).Build();
            var webhostBuilder = new WebHostBuilder().UseStartup <Startup>().UseConfiguration(configuration);

            return(new TestServer(webhostBuilder));
        }
Пример #9
0
 public CreateSubscriptionDelegate(ClientsDb db, IMapper mapper)
 {
     _db     = db;
     _mapper = mapper;
 }
 public GetIdentityProvidersDelegate(ClientsDb db, IMapper mapper)
 {
     _db     = db;
     _mapper = mapper;
 }