public void UpdateCategory(Category category, CategoryList list, string oldname = null) { // update record in database string tableName = GetDBTableName(list); bool ok = DBHelper.Update(category, tableName); if (!ok) { throw new InvalidOperationException(); } if (oldname != null && list == IngredientCategories) { // update category name of Ingredient objects var group = GroupedIngredients.GetOrAddGroup(oldname); string newName = category.Name; group.Key = newName; foreach (var item in group) { var ingredient = item as Ingredient; ingredient.Category = newName; UpdateIngredient(ingredient); } // recreate grouped records // (otherwise there is display problem in grid view) this.GroupedIngredients = Ingredients.GetGroupedList(); // update item count of all categories IngredientCategories.CountItemsFrom(GroupedIngredients); } }
public void UpdateIngredient(Ingredient item) { // update record in db bool ok = DBHelper.Update(item); if (!ok) { throw new InvalidOperationException(); } // remove item from old group if category is changed bool isRemoved = false; foreach (var group in GroupedIngredients) { if (group.Contains(item) && (string)group.Key != item.Category) { group.Remove(item); isRemoved = true; } } // add item to new group if needed if (isRemoved) { GroupedIngredients.GetOrAddGroup(item.Category).Add(item); // recount items in categories IngredientCategories.CountItemsFrom(GroupedIngredients); } }
// constructor private MainModelView() { InitializeDatabase(); ReadAllFromDB(); IngredientCategories.CountItemsFrom(GroupedIngredients); // async post-process for loading thumbnails and delete unused thumbnails LoadThumbtails().ContinueWith(t => DeleteUnusedThumbnails()); }
public void DeleteIngredient(Ingredient item) { // delete record from db bool ok = DBHelper.Delete(item); if (!ok) { throw new InvalidOperationException(); } // remove item from collection Ingredients.Remove(item); // remove item from grouped collection GroupedIngredients.GetOrAddGroup(item.Category).Remove(item); // recount items in categories IngredientCategories.CountItemsFrom(GroupedIngredients); }
public void AddIngredient(Ingredient item) { // add new record to database bool ok = DBHelper.Insert(item); if (!ok) { throw new InvalidOperationException(); } // add item to collection Ingredients.Add(item); // add item to grouped collection GroupedIngredients.GetOrAddGroup(item.Category).Add(item); // recount items in categories IngredientCategories.CountItemsFrom(GroupedIngredients); }
// public methods public void AddCategory(Category category, CategoryList list) { // add new record to database string tableName = GetDBTableName(list); bool ok = DBHelper.Insert(category, tableName); if (!ok) { throw new InvalidOperationException(); } // add to catagory list list.Add(category); // update item count of new category if (list == IngredientCategories) { IngredientCategories.CountItemsFrom(GroupedIngredients); } }