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 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); } } }