public void CreateAccount(Account account)
        {
            try
            {
                using (CapitalContext context = new CapitalContext())
                {
                    account.CreationDate = DateTime.Now;
                    // Clear these objects to prevent duplicate entry.
                    account.AccountType = null;
                    account.Frequency = null;

                    ValidationContext valContext = new ValidationContext(this, null, null);
                    var errors = account.Validate(valContext);

                    if (errors.Count() == 0)
                    {
                        if (context.Accounts.FirstOrDefault(x => x.AccountName == account.AccountName) != null)
                            throw new ModelException("An account by that name already exists! Please use a different name.");
                        context.Accounts.Add(account);
                        context.SaveChanges();
                    }
                    else
                        throw new ModelException(errors);
                }
            }
            catch (ModelException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                LogError(account, ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
        }
        public void CreateInvalidAccount()
        {
            UserManagement userManagement = new UserManagement();
            User user = userManagement.Login(new User { Email = "*****@*****.**", Password = "******" });

            if (user == null)
                Assert.Fail("No user for new account.");

            Account newAccount = new Account();

            AccountManagement accountManagement = new AccountManagement();
            accountManagement.CreateAccount(newAccount);
        }
 public void CreateAccount(Account account)
 {
     try
     {
         AccountManagement accountManagement = new AccountManagement();
         accountManagement.CreateAccount(account);
     }
     catch (ModelException ex)
     {
         CapitalError error = new CapitalError(ex);
         throw new FaultException<CapitalError>(error, error.Message);
     }
     catch (Exception)
     {
         return;
     }
 }
        public void CreateValidAccount()
        {
            UserManagement userManagement = new UserManagement();
            User user = userManagement.Login(new User { Email = "*****@*****.**", Password = "******" });

            if (user == null)
                Assert.Fail("No user for new account.");

            Account newAccount = new Account();
            newAccount.AccountName = "TEST 1";
            newAccount.CreationDate = DateTime.Now;
            newAccount.StartDate = DateTime.Now;
            newAccount.DefaultPayment = 200.00;
            newAccount.FrequencyId = 2;
            newAccount.UserId = user.UserId;
            newAccount.AccountTypeId = 1;

            AccountManagement accountManagement = new AccountManagement();
            accountManagement.CreateAccount(newAccount);
        }
        public void UpdateAccount(Account account)
        {
            using (CapitalContext context = new CapitalContext())
            {
                try
                {
                    ValidationContext valContext = new ValidationContext(this, null, null);
                    var errors = account.Validate(valContext);
                    if (errors.Count() > 0)
                        throw new ModelException(errors);


                    // Get Account
                    Account acc = context.Accounts.Where(x => x.AccountId == account.AccountId).FirstOrDefault();

                    // Update
                    if (acc != null)
                    {
                        acc.Balance = account.Balance;
                        acc.AccountName = account.AccountName;
                        acc.FrequencyId = account.FrequencyId;
                        acc.StartDate = account.StartDate;
                        acc.DefaultPayment = account.DefaultPayment;

                        // Update latest statement est. Payment amount.

                        Statement statement = context.Statements.Where(x => x.AccountId == acc.AccountId && x.IsPaid == false).FirstOrDefault();

                        if (statement != null)
                            statement.Balance = acc.DefaultPayment;

                        // Save
                        context.SaveChanges();
                    }
                }
                catch (ModelException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    LogError(account, ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                }
            }

        }
 public void DeleteAccount(Account account)
 {
     try
     {
         using (CapitalContext context = new CapitalContext())
         {
             var target = context.Accounts.Where(x => x.AccountId == account.AccountId).FirstOrDefault();
             if (target != null)
             {
                 context.Accounts.Remove(target);
                 context.SaveChanges();
             }
             else
                 throw new ModelException("That account does not exists.");
         }
     }
     catch (ModelException ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         LogError(account, ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
     }
 }