private void FindAndRemoveIngredient(ObservableCollection<IngredientViewModel> source, IngredientViewModel item) { foreach (var ingredient in source) { if (ingredient.Id == item.Id) { bool removed = source.Remove(ingredient); break; } } }
private async void ImportIngredientsData(string fileContent) { string line = ""; using (System.IO.StringReader stringReader = new System.IO.StringReader(fileContent)) { while ((line = await stringReader.ReadLineAsync()) != null) { IngredientViewModel ingredient = new IngredientViewModel(); string[] ingredientAndManufacturer = line.Split(';'); ingredient.Name = ingredientAndManufacturer[0].TrimEnd(); //ingredient.Manufacturer = ingredientAndManufacturer[1].TrimEnd(); ingredient.Category = "Metzgerei"; ingredient.UnitOfMeasure = 1; ingredient.SaveIngredientCheckExisting(ingredient); } } }
public ObservableCollection<IngredientViewModel> GetAllIngredients() { ingredients = new ObservableCollection<IngredientViewModel>(); using (var db = new SQLite.SQLiteConnection(App.DBPath)) { var query = db.Table<Ingredient>().OrderBy(c => c.Name); foreach (var _ingredient in query) { var ingredient = new IngredientViewModel() { Id = _ingredient.Id, Name = _ingredient.Name, Weight = _ingredient.Weight, UnitOfMeasure = _ingredient.UnitOfMeasure }; ingredients.Add(ingredient); } } return ingredients; }
public string SaveIngredientCheckExisting(IngredientViewModel ingredient) { string result = string.Empty; using (var db = new SQLite.SQLiteConnection(App.DBPath)) { try { var existingIngredient = (db.Table<Ingredient>().Where( c => c.Name.Contains(ingredient.Name))).SingleOrDefault(); if (existingIngredient == null) { int success = db.Insert(new Ingredient() { Name = ingredient.Name, Weight = ingredient.Weight, UnitOfMeasure = ingredient.UnitOfMeasure, Category = ingredient.Category, Manufacturer = ingredient.Manufacturer }); } } catch { } } return result; }
public string SaveIngredient(IngredientViewModel ingredient) { string result = string.Empty; using (var db = new SQLite.SQLiteConnection(App.DBPath)) { string change = string.Empty; try { var existingIngredient = (db.Table<Ingredient>().Where( c => c.Id == ingredient.Id)).SingleOrDefault(); if (existingIngredient != null) { existingIngredient.Name = ingredient.Name; existingIngredient.Weight = ingredient.Weight; existingIngredient.UnitOfMeasure = ingredient.UnitOfMeasure; existingIngredient.Manufacturer = ingredient.Manufacturer; existingIngredient.Category = ingredient.Category; int success = db.Update(existingIngredient); } else { int success = db.Insert(new Ingredient() { Name = ingredient.Name, Weight = ingredient.Weight, UnitOfMeasure = ingredient.UnitOfMeasure, Category = ingredient.Category }); } result = "Success"; } catch { result = "This ingredient was not saved."; } } return result; }
public IngredientViewModel IngredientExists(string ingredientName) { IngredientViewModel result = null; //initialized true because nothing happens in that case using (var db = new SQLite.SQLiteConnection(App.DBPath)) { try { var ingredient = db.Table<Ingredient>() .Where(c => c.Name == ingredientName).SingleOrDefault(); if (ingredient != null) { result = new IngredientViewModel(); result.Id = ingredient.Id; result.Name = ingredient.Name; result.Weight = ingredient.Weight; result.UnitOfMeasure = ingredient.UnitOfMeasure; result.Manufacturer = ingredient.Manufacturer; result.Category = ingredient.Category; } else { } } catch(Exception e) { string exceptionMsg = e.Message; //Looks like the ingredient was not found. A linq problem I need to investigate } } return result; }
public IngredientViewModel GetIngredient(float ingredientId, float ingredientMealWeight) { var ingredient = new IngredientViewModel(); using (var db = new SQLite.SQLiteConnection(App.DBPath)) { var _ingredient = (db.Table<Ingredient>().Where( c => c.Id == ingredientId)).Single(); ingredient.Id = _ingredient.Id; ingredient.Name = _ingredient.Name; ingredient.Manufacturer = _ingredient.Manufacturer; ingredient.Weight = _ingredient.Weight; ingredient.UnitOfMeasure = _ingredient.UnitOfMeasure; ingredient.Category = _ingredient.Category; ingredient.IngredientMealWeight = ingredientMealWeight; } return ingredient; }
private IngredientViewModel GetIngredientDetails(string rawDetails, out string amount, out string unitOfMeasure) { IngredientViewModel result = null; amount = String.Empty; unitOfMeasure = String.Empty; Regex regex = new Regex(@"[,\d]{1,6}(kg|g|x| Ltr|ml| Stck)", RegexOptions.IgnoreCase); Match match = regex.Match(rawDetails); if (match.Success) { result = new IngredientViewModel(); string ingredientName = rawDetails.Substring(match.Value.Length, rawDetails.Length - match.Value.Length).Trim(); ; if (match.Value.Contains("x")) { Regex regexValue = new Regex(@"[\(\d]{1,4}(ml)[\)]"); Match matchValue = regexValue.Match(ingredientName); ingredientName = ingredientName.Substring(0, matchValue.Index).Trim(); } result = result.IngredientExists(ingredientName); if (result != null) { Regex regexAmount = new Regex(@"[\d,]{1,4}", RegexOptions.IgnoreCase); Match matchAmount = regexAmount.Match(match.Value); amount = matchAmount.Value.ToString().Trim(); unitOfMeasure = match.Value.Substring(amount.Length, match.Value.Length - amount.Length).Trim(); } else { _unknownIngredients.Add(rawDetails + "|" + ingredientName); } } else { _noMatchIngredients.Add(rawDetails); } return result; }