public Boolean Save(out List<string> errors, out List<string> warnings, Boolean ignoreWarnings = false) { Boolean result = false; Validate(out errors, out warnings); if (errors.Count == 0 && (ignoreWarnings || warnings.Count == 0)) { try { var db = new BudgetEntities(); if (Mode == TransactionEntryWindow.ModeValues.Add) { // add the transaction var transaction = new Transaction(); transaction.Guid = Guid.NewGuid(); transaction = PopulateObject(transaction, db); db.Transactions.Add(transaction); db.SaveChanges(); result = true; } else if (Mode == TransactionEntryWindow.ModeValues.Edit) { // update the transaction var transaction = (from t in db.Transactions where t.Guid == TransationID select t).Single(); transaction = PopulateObject(transaction, db); db.SaveChanges(); result = true; } else { throw new InvalidOperationException("Invalid mode specified."); } } catch (Exception ex) { errors.Add(ex.Message); } } return result; }
private Transaction PopulateObject(Transaction transaction, BudgetEntities db) { transaction.Date = Date.Value; transaction.Amount = Double.Parse(Amount, NumberStyles.Currency); transaction.Payee = Payee.Guid; transaction.Account = Account.Guid; transaction.Notes = Notes; transaction.CheckNumber = CheckNumber; // clear transaction categories foreach (var transactionCategory in transaction.TransactionCategories.ToList()) { db.TransactionCategories.Remove(transactionCategory); } // add the transaction categories foreach (DictionaryEntry item in Budget) { var transactionCategory = new TransactionCategory(); transactionCategory.Guid = Guid.NewGuid(); transactionCategory.Amount = Double.Parse(item.Value.ToString(), NumberStyles.Currency); transactionCategory.Category = ((Category)item.Key).Guid; transaction.TransactionCategories.Add(transactionCategory); } return transaction; }