public static async Task DeleteTarget <T>(T itemToDelete) where T : class { using (var db = new TargetDataContext()) { db.Set <T>().Remove(itemToDelete); await db.SaveChangesAsync(); } }
private static async Task AddBalance(Transaction savedTransaction) { //performs async so the app doesnt slow down if there are a lot of goals await Task.Factory.StartNew(async() => { using (var db = new TargetDataContext()) { var targets = await db.Targets.ToListAsync(); // turns target into a list var target = targets.SingleOrDefault(x => x.TargetId == savedTransaction.TargetId); //retrives the target based on the target id target.CurrentBalance += savedTransaction.Amount; //adds new amount to balance await db.SaveChangesAsync(); } }); }
//generic method so both classes can share one method public static async Task AddRecord <T>(T newRecord) where T : class { using (var db = new TargetDataContext()) { db.Add <T>(newRecord); //adds a new target to the database await db.SaveChangesAsync(); //only runs if transaction is called if (typeof(T) == typeof(Transaction)) { var transaction = newRecord as Transaction; await AddBalance(transaction); } } }
public static async void UpdateTarget(Target updateTarget) { using (var db = new TargetDataContext()) { var target = db.Targets.FirstOrDefault(x => x.TargetId == updateTarget.TargetId); if (target != null) { //entity framwork keeps track of the target item so when the properties are saved the datacontext knows there has been a change. target.Name = updateTarget.Name; //Updates the name after a change by the user target.Notes = updateTarget.Notes; // Updates the notes after a change by the user target.SavingTarget = updateTarget.SavingTarget; // Updates the saving target after a change by the user // target.CurrentBalance = updateTarget.CurrentBalance; await db.SaveChangesAsync(); } } }