/// <summary> /// Create and add a grocery to a shoppinglist. /// </summary> /// <param name="grocery"></param> /// <param name="listId"></param> public void Create(Grocery grocery, int listId) { using (var scope = new TransactionScope()) { var existingList = _dataContext.Lists.FirstOrDefault(list => list.Id == listId); if (existingList == null) { throw new ArgumentException("Provided value does not exist in the database and cannot be updated."); } existingList.Groceries.Add(grocery); _dataContext.SaveChanges(); scope.Complete(); } }
/// <summary> /// Creates a new shopping list /// </summary> /// <param name="shoppingList"></param> /// <returns></returns> public void Create(ShoppingList shoppingList, string ownerId) { using (var scope = new TransactionScope()) { // Assign the provided owner to the shopping list. if (shoppingList.Users == null) { shoppingList.Users = new Collection <ApplicationUser>(); } shoppingList.Users.Add(_dataContext.Users.FirstOrDefault(usr => usr.Id == ownerId)); // Store the shopping list as part of this method _dataContext.Lists.Add(shoppingList); _dataContext.SaveChanges(); scope.Complete(); } }