Ejemplo n.º 1
0
        /// <summary>
        /// Adds a new account to the repository.
        /// </summary>
        /// <param name="account">New account.</param>
        /// <exception cref="StorageException">
        /// It is thrown in the event of a storage error.
        /// </exception>
        public void AddAccount(AccountDal account)
        {
            if (ReferenceEquals(account, null))
            {
                throw new ArgumentNullException(nameof(account));
            }

            using (var context = new BankAccountContext())
            {
                context.Accounts.Add(account.ToAccountEF());
                context.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns all BankAccount <see cref="AccountDal"/> from the repository.
        /// </summary>
        /// <returns>
        /// BankAccount  <see cref="BankAccount"/> from the repository.
        /// </returns>
        /// <exception cref="StorageException">
        /// It is thrown out in case of storage problems.
        /// </exception>
        public IEnumerable <AccountDal> GetBankAccounts()
        {
            var list = new List <AccountEF>();

            using (var context = new BankAccountContext())
            {
                list.AddRange(context.Accounts
                              .Include(acc => acc.AccountType));
            }

            return(list
                   .Select(ac => ac.ToAccountDalFromEF())
                   .ToArray());
        }
        public BankAccount GetBankAccountByNumber(string number)
        {
            using (var context = new BankAccountContext())
            {
                var bankAccount = context.BankAccounts.FirstOrDefault(m => m.AccountNumber == number);

                if (bankAccount == null)
                {
                    throw new BankAccountNotFoundException("Account is not found.", nameof(number));
                }

                return(bankAccount);
            }
        }
        public void UpdateBankAccount(BankAccount account)
        {
            using (var context = new BankAccountContext())
            {
                var bankAccount = context.BankAccounts.FirstOrDefault(m => m.AccountNumber == account.AccountNumber);

                if (bankAccount == null)
                {
                    throw new BankAccountNotFoundException("Account is not found.", nameof(account));
                }

                bankAccount.Balance     = account.Balance;
                bankAccount.BonusPoints = account.BonusPoints;
                bankAccount.Status      = account.Status;

                context.SaveChanges();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// A method that removes a account <paramref name="account"/> from the repository.
        /// </summary>
        /// <param name="account">
        /// The Account for removal.
        /// </param>
        /// <exception cref="StorageException">
        /// It is thrown out in case of storage problems.
        /// </exception>
        public void RemoveAccount(AccountDal account)
        {
            if (ReferenceEquals(account, null))
            {
                throw new ArgumentNullException(nameof(account));
            }

            using (var context = new BankAccountContext())
            {
                var accountForRemove = FindAccount(account, context);

                if (ReferenceEquals(accountForRemove, null))
                {
                    throw new AccountNotFoundException();
                }

                context.Accounts.Remove(accountForRemove);

                context.SaveChanges();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Updates the account information in the repository.
        /// </summary>
        /// <param name="account">Account to update.</param>
        /// <exception cref="StorageException">
        /// It is thrown out in case of storage problems.
        /// </exception>
        public void UpdateAccount(AccountDal account)
        {
            if (ReferenceEquals(account, null))
            {
                throw new ArgumentNullException(nameof(account));
            }

            using (var context = new BankAccountContext())
            {
                var accountForUpdate = FindAccount(account, context);

                if (ReferenceEquals(accountForUpdate, null))
                {
                    throw new AccountNotFoundException();
                }

                accountForUpdate.Balance = account.Balance;
                accountForUpdate.Bonus   = account.Bonus;

                context.SaveChanges();
            }
        }
        public void AddBankAccount(BankAccount account)
        {
            using (var context = new BankAccountContext())
            {
                var bankAccount = context.BankAccounts.FirstOrDefault(m => m.AccountNumber == account.AccountNumber);

                if (bankAccount == null)
                {
                    context.BankAccounts.Add(account);

                    var accountOwner = context.Owners.FirstOrDefault(m => m.AccountOwnerId == account.Owner.AccountOwnerId);
                    if (accountOwner == null)
                    {
                        context.Owners.Add(account.Owner);
                    }

                    context.SaveChanges();
                    return;
                }
            }

            throw new ArgumentException("This value is already exists.", nameof(account));
        }
Ejemplo n.º 8
0
 private AccountEF FindAccount(AccountDal account, BankAccountContext context)
 {
     return(context.Accounts
            .Include(acc => acc.AccountType)
            .FirstOrDefault(acc => account.Number == acc.AccountId));
 }
 public void Save()
 {
     using (var context = new BankAccountContext())
         context.SaveChanges();
 }