/// <summary> /// Method to delete an ItemQuantityPairModel from the API database asynchronously /// Returns true if successful, false if not /// </summary> /// <param name="item"></param> /// <returns></returns> public async Task <bool> DeleteItemQuantityPairModelAsync(ItemQuantityPairModel item) { if (item.RemoteDbId == null) { return(true); } return(await helper.DeleteItemAsync(helper.BaseUrl + ShoppingListModel.UrlSuffix + "/" + ItemQuantityPairModel.UrlSuffix + "/" + item.RemoteDbId)); }
/// <summary> /// Method to get the best ipl match for the given iqp /// </summary> /// <param name="iqp"></param> /// <param name="location"></param> /// <returns></returns> private ItemPriceLocationModel GetBestMatch(ItemQuantityPairModel iqp, LocationModel location) { // Split the iqp name into its contituent words var split = iqp.Name.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); split = split.Distinct().ToArray(); // Make all the split substrings lower case for (int i = 0; i < split.Length; i++) { split[i] = split[i].ToLower(); } // Select a structure that contains the ipl and the number of words in the ipl name that match words in the iqp name var res = location.ItemPriceLocations.Select(ipl => new { item = ipl, count = ipl.Name.Split(' ').Distinct().Sum(p => split.Contains(p.ToLower()) ? 1 : 0) }); // Order by number of hits res = res.OrderByDescending(p => p.count); // Remove lower matches if (res.Any()) { var count = res.First().count; res = res.Where(p => p.count == count); } var measureMatches = res.Where(p => p.item.Name.ToLower().Contains(iqp.Measure.ToLower())); if (measureMatches.Any()) { return(measureMatches.First().item); } // Finally order by price res = res.OrderBy(p => p.item.Price); // Return null if there is no match (all the counts are 0) if (!res.Any() || res.First().count == 0) { return(null); } // Return the ipl with the greatest count return(res.First().item); }
/// <summary> /// Method to delete an item from the given shopping list /// </summary> /// <param name="list"></param> /// <param name="iqp"></param> public async void DeleteItem(ShoppingListModel list, ItemQuantityPairModel iqp) { list.Items.Remove(iqp); iqp.Deleted = true; // Save this item to the database as deleted so that it can be deleted if the api call fails databaseHelper.SaveShoppingListAsync(list); // Make the api call, store the return value (if it is deleted or not) var deleted = await apiHelper.DeleteItemQuantityPairModelAsync(iqp); // Delete from local db if it is deleted on the api if (deleted) { databaseHelper.DeleteItemAsync(iqp); } }
/// <summary> /// Method to create a new ItemQuantityPair and raise the relevant event /// </summary> private void RaiseNewItemQuantityPairEvent() { // Create return list var models = new List <ItemQuantityPairModel>(); // Check which mode this is running in if (RowRecipeSearch.Height.Value == 0) { // Add the new item to the collection App.MasterController.AddItem(ItemFilterText); var iqp = new ItemQuantityPairModel() { Name = ItemFilterText.Trim(), Quantity = Double.Parse(Quantity), Measure = Measure.Trim() }; models.Add(iqp); } else if (RecipeListView.SelectedItem != null) { // Add the recipe ingredients to the collection var recipe = (Recipe)RecipeListView.SelectedItem; foreach (var ingredient in recipe.Ingredients) { if (ingredient.Food != null && ingredient.Measure != null) { models.Add(new ItemQuantityPairModel { Name = ingredient.Food, Quantity = ingredient.Quantity, Measure = ingredient.Measure }); } } } // Invoke the event callBack?.Invoke(this, new ItemQuantityPairArgs(models)); }
/// <summary> /// Method to calculate the price of the given item using the given ipl /// </summary> /// <param name="iqp"></param> /// <param name="ipl"></param> /// <returns></returns> private double CalculatePrice(ItemQuantityPairModel iqp, ItemPriceLocationModel ipl) { if (iqp.Measure == ipl.Measure) { var baseRate = ipl.Price / ipl.Quantity; return(iqp.Quantity * baseRate); } switch (iqp.Measure.ToLower()) { case "loaf": return(ipl.Price); case "loaves": return(iqp.Quantity * ipl.Price); default: break; } // TODO if measure is not the same return(ipl.Price); }