static public void AddSecurity(Security sec) { using (var db = new CouatlContext()) { db.Securities.Add(sec); db.SaveChanges(); } }
/// <summary> /// Add an account to the database. /// </summary> /// <param name="acct">The account to add to the database.</param> static public void AddAccount(Account acct) { using (var db = new CouatlContext()) { db.Accounts.Add(acct); db.SaveChanges(); } }
static private void DeletePosition(Position thePos) { using (var db = new CouatlContext()) { db.Positions.Attach(thePos); db.Remove(thePos); db.SaveChanges(); } }
static private void UpdatePosition(Position pos) { using (var db = new CouatlContext()) { db.Positions.Attach(pos); db.Entry(pos).State = EntityState.Modified; db.SaveChanges(); } }
// TODO: Make this private because the user should be doing delete/add; it is too difficult to change Type. static private void UpdateTransaction(Transaction xact) { using (var db = new CouatlContext()) { db.Transactions.Attach(xact); db.Entry(xact).State = EntityState.Modified; db.SaveChanges(); } }
static public void DeleteAccount(Account acct) { using (var db = new CouatlContext()) { db.Accounts.Attach(acct); db.Remove(acct); db.SaveChanges(); } }
static public void UpdateAccount(Account acct) { using (var db = new CouatlContext()) { db.Accounts.Attach(acct); db.Entry(acct).State = EntityState.Modified; db.SaveChanges(); } }
static public void DeleteSecurity(Security sec) { using (var db = new CouatlContext()) { db.Securities.Attach(sec); db.Securities.Remove(sec); db.SaveChanges(); } }
static public void AddPrice(int securityId, DateTime date, decimal amount, bool closing) { Price thePrice = new Price(); thePrice.Amount = amount; thePrice.Date = date; thePrice.Closing = closing; thePrice.SecurityId = securityId; using (var db = new CouatlContext()) { bool addNewPrice = true; // Check for existing price. List <Price> prices; prices = db.Prices.Where(p => p.SecurityId == securityId && p.Date == date).ToList(); // If there is more than one price for this date, then the database is in an invalid state. // TODO: Gracefully handle more than one price per date? if (prices.Count > 1) { throw new IndexOutOfRangeException(); } else if (prices.Count == 1) { // Remove the existing price, if necessary. // A non-closing price will always be removed in favor of a more-recent price. // A closing price will only be removed if the new price is also a closing price // (presumanly this means that the earlier closing price was incorrect). if (!prices[0].Closing || closing) { db.Attach(prices[0]); db.Remove(prices[0]); } else { addNewPrice = false; } } if (addNewPrice) { db.Prices.Add(thePrice); } db.SaveChanges(); } }
static public void DeleteTransaction(Transaction theXact) { int theAcctId = theXact.Account.AccountId; Account theAcct = theXact.Account; // TODO: Data validation here? What to do if there is a problem? Position thePos; // TODO: Update Positions here if this is a StockSplit? switch (theXact.Type) { case (int)TransactionType.Buy: // Find the Position that this Buy affects. thePos = theAcct.Positions.Find(p => p.SecurityId == theXact.SecurityId); // Back out the shares that were added. thePos.Quantity -= theXact.Quantity; if (thePos.Quantity == 0) { DeletePosition(thePos); } else { UpdatePosition(thePos); } break; case (int)TransactionType.Sell: // Find the Position that this Sell affects. thePos = theAcct.Positions.Find(p => p.SecurityId == theXact.SecurityId); // Add back the shares that were removed. thePos.Quantity += theXact.Quantity; // TODO: Revisit the handling of short sells. // It is possible that this Sell transaction was the only one for this position. // In that case then we want to delete the entry in the Position table. if (thePos.Quantity == 0) { DeletePosition(thePos); } else { UpdatePosition(thePos); } break; case (int)TransactionType.Deposit: // Update Cash. theAcct.Cash -= theXact.Value; UpdateAccount(theAcct); break; case (int)TransactionType.Withdrawal: // Update Cash. theAcct.Cash += theXact.Value; UpdateAccount(theAcct); break; } // Delete the transaction from the database table. using (var db = new CouatlContext()) { // TODO: Figure out if Attach is really needed before Remove; it seems it is not. //db.Transactions.Attach(theXact); db.Transactions.Remove(theXact); db.SaveChanges(); } }