public void RemoveAccount(DalAccount account)
        {
            if (ReferenceEquals(account, null))
            {
                throw new ArgumentNullException(nameof(account));
            }

            using (var db = new AccountContext())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    var dbAccount = db.Accounts.FirstOrDefault(acc => acc.Active == true && acc.AccountNumber == account.AccountNumber);
                    if (ReferenceEquals(dbAccount, null))
                    {
                        throw new RepositoryException($"Can not find open account with account number {account.AccountNumber}");
                    }
                    try
                    {
                        dbAccount.Active = false;
                        var operationType = GetOrCreateOperationType(db, "Close");

                        CreateOperation(operationType, 0, dbAccount, db);
                        db.SaveChanges();
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        throw new RepositoryException("Remove account error.", e);
                    }
                }
            }
        }
 private void UpdateAccount(AccountContext db, Account dbAccount, DalAccount account, Owner accountOwner, AccountType accountType)
 {
     dbAccount.AccountType    = accountType;
     dbAccount.AccountOwnerId = accountOwner.Id;
     dbAccount.CurrentSum     = account.Balance;
     dbAccount.BonusPoints    = account.BenefitPoints;
     dbAccount.AccountNumber  = account.AccountNumber;
     db.SaveChanges();
 }
        private AccountOperation CreateOperation(OperationType operationType, decimal value, Account account, AccountContext db)
        {
            var operation = new AccountOperation
            {
                ChangedAccount       = account,
                AccountOperationType = operationType,
                OperationValue       = value
            };

            db.AccountOperations.Add(operation);
            db.SaveChanges();

            return(operation);
        }
        private OperationType GetOrCreateOperationType(AccountContext db, string opType)
        {
            var operationType = db.OperationTypes.FirstOrDefault(ot => ot.Type == opType);

            if (ReferenceEquals(operationType, null))
            {
                operationType = new OperationType
                {
                    Type = opType
                };
                db.OperationTypes.Add(operationType);

                db.SaveChanges();
            }

            return(operationType);
        }
        private AccountType GetOrCreateType(AccountContext db, DalAccount account)
        {
            AccountType accountType = db.AccountTypes.FirstOrDefault(at => at.Type == account.AccountType);

            if (!ReferenceEquals(accountType, null))
            {
                return(accountType);
            }
            accountType = new AccountType
            {
                Type = account.AccountType,
            };
            db.AccountTypes.Add(accountType);
            db.SaveChanges();

            return(accountType);
        }
        private Owner GetOrCreateOwner(AccountContext db, DalAccount account)
        {
            Owner owner = db.Owners.FirstOrDefault(dbAccount => dbAccount.Email == account.OwnerEmail);

            if (!ReferenceEquals(owner, null))
            {
                return(owner);
            }
            owner = new Owner
            {
                Email = account.OwnerEmail
            };
            db.Owners.Add(owner);
            db.SaveChanges();

            return(owner);
        }
        public void Update(AccountDto account)
        {
            if (account == null)
            {
                throw new ArgumentNullException($"{nameof(account)} can't be null");
            }

            using (var db = new AccountContext())
            {
                Account a = db.Accounts.FirstOrDefault(x => x.Number == account.Number);

                if (a == null)
                {
                    return;
                }

                a = account.ToAccount();
                db.SaveChanges();
            }
        }
        public void Create(AccountDto account)
        {
            if (account == null)
            {
                throw new ArgumentNullException($"{nameof(account)} can't be null");
            }

            Console.WriteLine(account.Name);

            Owner owner = account.ToOwner();

            Console.WriteLine($"Owner name: {owner.Name}");
            Account addedAccount = account.ToAccount(owner);

            Console.WriteLine($"In account owner name: {addedAccount.Owner.Name}");

            using (var db = new AccountContext())
            {
                db.Owners.Add(owner);
                db.Accounts.Add(addedAccount);
                db.SaveChanges();
            }
        }