Example #1
0
        public virtual async Task <DataLinkDto> CreateDataLinkAsync(int accountId, DataLinkDto dataLinkDto)
        {
            try
            {
                var prefetch = await PrefetchAndValidate(accountId, dataLinkDto);

                var dataLink = new DataLink
                {
                    FromSubscriptionId = dataLinkDto.FromSubscriptionId,
                    From             = prefetch.From,
                    ToSubscriptionId = dataLinkDto.ToSubscriptionId,
                    To             = prefetch.To,
                    DataLinkTypeId = dataLinkDto.DataLinkTypeId,
                    Type           = prefetch.Type
                };

                _db.DataLinks.Add(dataLink);
                await _db.SaveChangesAsync();

                return(_mapper.Map <DataLinkDto>(dataLink));
            }
            catch (DbException e)
            {
                throw new PersistenceException($"An error occurred while creating the DataLink ({nameof(accountId)} = {accountId}, {nameof(dataLinkDto)} = {JsonConvert.SerializeObject(dataLinkDto)})", e);
            }
        }
        public virtual async Task <IdentityProviderDto> UpdateIdentityProvider(int accountId, int identityProviderId, UpdateIdentityProviderDto identityProviderDto)
        {
            var validationErrors = new List <ValidationResult>();

            Utils.ValidateDto(identityProviderDto, validationErrors);

            try
            {
                var identityProvider = await Utils.GetIdentityProviderAsync(_db, accountId, identityProviderId);

                if (!string.Equals(identityProvider.Name, identityProviderDto.Name))
                {
                    identityProvider.Name             = identityProviderDto.Name;
                    _db.Entry(identityProvider).State = EntityState.Modified;
                }

                await _db.SaveChangesAsync();

                return(_mapper.Map <IdentityProviderDto>(identityProvider));
            }
            catch (DbException e)
            {
                throw new PersistenceException($"An error occurred while reading an IdentityProvider ({nameof(accountId)} = {accountId},{nameof(identityProviderId)} = {identityProviderId}, {nameof(identityProviderDto)} = {JsonConvert.SerializeObject(identityProviderDto)})", e);
            }
        }
Example #3
0
        public virtual async Task <AccountDto> UpdateAccountAsync(int accountId, UpdateAccountDto accountDto)
        {
            var account = await Utils.GetAccountAsync(_db, accountId);

            try
            {
                var errors = new List <ValidationResult>();

                Utils.ValidateDto(accountDto, errors);
                Utils.ThrowAggregateExceptionOnValidationErrors(errors);

                account.Name = !string.IsNullOrEmpty(accountDto.AccountName)
                    ? accountDto.AccountName
                    : account.Name;

                account.AccountTypeId = accountDto.AccountTypeId != default
                    ? accountDto.AccountTypeId
                    : account.AccountTypeId;

                account.ArchetypeId = accountDto.ArchetypeId != default
                    ? accountDto.ArchetypeId
                    : account.ArchetypeId;

                account.SalesforceAccountId = !string.IsNullOrEmpty(accountDto.SalesforceAccountId)
                    ? accountDto.SalesforceAccountId
                    : account.SalesforceAccountId;

                account.SalesforceAccountManager = !string.IsNullOrEmpty(accountDto.SalesforceAccountManager)
                    ? accountDto.SalesforceAccountManager
                    : account.SalesforceAccountManager;

                account.SalesforceAccountNumber = !string.IsNullOrEmpty(accountDto.SalesforceAccountNumber)
                    ? accountDto.SalesforceAccountNumber
                    : account.SalesforceAccountNumber;

                account.SalesforceAccountUrl = !string.IsNullOrEmpty(accountDto.SalesforceAccountUrl)
                    ? accountDto.SalesforceAccountUrl
                    : account.SalesforceAccountUrl;

                account.ContractNumber = !string.IsNullOrEmpty(accountDto.ContractNumber)
                    ? accountDto.ContractNumber
                    : account.ContractNumber;

                _db.Entry(account).State = EntityState.Modified;
                await _db.SaveChangesAsync();

                return(_mapper.Map <AccountDto>(account));
            }
            catch (DbException e)
            {
                throw new PersistenceException($"An error occurred while updating Account ({nameof(accountId)}={accountId}, {nameof(accountDto)}={JsonConvert.SerializeObject(accountDto)})", e);
            }
        }
        private async Task <Account> PersistAccountAsync(AccountDto accountViewModel, CreateAccountPrefetch dependencies)
        {
            var account = new Account
            {
                AccountId                = accountViewModel.AccountId,
                Name                     = accountViewModel.AccountName,
                AccountTypeId            = dependencies.AccountType.AccountTypeId,
                AccountType              = dependencies.AccountType,
                Archetype                = dependencies.Archetype,
                ArchetypeId              = dependencies.Archetype.ArchetypeId,
                SalesforceAccountId      = accountViewModel.SalesforceAccountId,
                SalesforceAccountUrl     = accountViewModel.SalesforceAccountUrl,
                SalesforceAccountNumber  = accountViewModel.SalesforceAccountNumber,
                SalesforceAccountManager = accountViewModel.SalesforceAccountManager,
                ContractNumber           = accountViewModel.ContractNumber
            };

            var subscriptions = (
                from s in accountViewModel.Subscriptions ?? new List <SubscriptionDto>()
                select new Subscription
            {
                SubscriptionId = s.SubscriptionId,
                Account = account,
                Name = s.SubscriptionName,
                Description = s.Description,
                Tags = s.Tags == null || s.Tags.Keys.Count < 1 ? null : JsonConvert.SerializeObject(s.Tags),
                OrganizationalUnit = s.OrganizationalUnit,
                SubscriptionTypeId = s.SubscriptionTypeId,
                SubscriptionType = dependencies.SubscriptionTypes.First(t => t.SubscriptionTypeId == s.SubscriptionTypeId),
                ActivationDate = DateTime.UtcNow,
                Enabled = true
            }
                ).ToList();

            var identityProviders = (
                from i in accountViewModel.IdentityProviders ?? new List <IdentityProviderDto>()
                select new IdentityProvider
            {
                Account = account,
                Name = i.Name
            }
                ).ToList();

            subscriptions.ForEach(account.Subscriptions.Add);
            identityProviders.ForEach(account.IdentityProviders.Add);

            _db.Accounts.Add(account);

            await _db.SaveChangesAsync();

            return(account);
        }
Example #5
0
        public async Task <IdentityProviderDto> CreateIdentityProvider(int accountId, IdentityProviderDto identityProviderDto)
        {
            var errors = new List <ValidationResult>();

            Utils.ValidateDto(identityProviderDto, errors);
            Utils.ThrowAggregateExceptionOnValidationErrors(errors);

            try
            {
                var account = (from a in _db.Accounts where a.AccountId == accountId select a).FirstOrDefault();
                if (account == null)
                {
                    throw new AccountNotFoundException($"An account with {nameof(AccountDto.AccountId)} = {accountId} could not be found");
                }

                if (account.IdentityProviders.Any(p => string.Equals(p.Name, identityProviderDto.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    throw new MalformedAccountException($"An identity provider with the same name [{identityProviderDto.Name}] already exists.");
                }

                var newIdentityProvider = new IdentityProvider
                {
                    Name      = identityProviderDto.Name,
                    Account   = account,
                    AccountId = accountId
                };

                account.IdentityProviders.Add(newIdentityProvider);
                await _db.SaveChangesAsync();

                return(_mapper.Map <IdentityProviderDto>(newIdentityProvider));
            }
            catch (DbException e)
            {
                throw new PersistenceException($"An error occurred while creating the IdentityProvider ({nameof(accountId)} = {accountId}, {nameof(identityProviderDto)} = {JsonConvert.SerializeObject(identityProviderDto)})", e);
            }
        }
Example #6
0
        private async Task <Subscription> PersistSubscriptionAsync(Account account, SubscriptionType subscriptionType, SubscriptionDto subscriptionDto)
        {
            var subscription = new Subscription
            {
                SubscriptionId     = subscriptionDto.SubscriptionId,
                Account            = account,
                AccountId          = account.AccountId,
                Name               = subscriptionDto.SubscriptionName,
                Description        = subscriptionDto.Description,
                Tags               = subscriptionDto.Tags == null || subscriptionDto.Tags.Keys.Count < 1 ? null : JsonConvert.SerializeObject(subscriptionDto.Tags),
                OrganizationalUnit = subscriptionDto.OrganizationalUnit,
                SubscriptionTypeId = subscriptionDto.SubscriptionTypeId,
                SubscriptionType   = subscriptionType,
                ActivationDate     = DateTime.UtcNow,
                Enabled            = true
            };

            account.Subscriptions.Add(subscription);
            await _db.SaveChangesAsync();

            return(subscription);
        }
        public virtual async Task <SubscriptionDto> UpdateSubscriptionAsync(int accountId, int subscriptionId, UpdateSubscriptionDto subscriptionDto)
        {
            var validationErrors = new List <ValidationResult>();

            Utils.ValidateDto(subscriptionDto, validationErrors);

            try
            {
                var subscription = await Utils.GetSubscriptionAsync(_db, accountId, subscriptionId);

                subscription.Name = !string.IsNullOrEmpty(subscriptionDto.SubscriptionName)
                    ? subscriptionDto.SubscriptionName
                    : subscription.Name;

                subscription.Description = !string.IsNullOrEmpty(subscriptionDto.Description)
                    ? subscriptionDto.Description
                    : subscription.Description;

                subscription.Tags = subscriptionDto.Tags != null
                    ? JsonConvert.SerializeObject(subscriptionDto.Tags)
                    : subscription.Description;

                subscription.OrganizationalUnit = !string.IsNullOrEmpty(subscriptionDto.OrganizationalUnit)
                    ? subscriptionDto.OrganizationalUnit
                    : subscription.OrganizationalUnit;

                _db.Entry(subscription).State = EntityState.Modified;

                await _db.SaveChangesAsync();

                return(_mapper.Map <SubscriptionDto>(subscription));
            }
            catch (DbException e)
            {
                throw new PersistenceException($"An error occurred while reading an IdentityProvider ({nameof(accountId)} = {accountId}, {nameof(subscriptionId)} = {subscriptionId}, {nameof(subscriptionDto)} = {JsonConvert.SerializeObject(subscriptionDto)})", e);
            }
        }