private void OnAddFoodButtonClick(object sender, EventArgs e) { try { FoodDTO dto = FoodItemPresenter.GetInputs(); if (dto.Category == null) { CategoryDTO categoryDto = new CategoryDTO() { Name = FoodItemPresenter.GetNewCategoryName() }; newCategory = FoodTracker.AddCategory(categoryDto); dto.Category = newCategory; } FoodTracker.AddFood(dto); AddFoodView.DialogResult = DialogResult.OK; } catch (ValidationException vex) { MessageBox.Show(vex.Message); } }
private void OnAddCategoriesButtonClick(object sender, EventArgs e) { try { CategoryDTO newCategory = new CategoryDTO() { Name = AddCategoryView.CategoryName }; FoodTracker.AddCategory(newCategory); CategoryLookupPresenter.UpdateCategories(CategoryLookupPresenter.SelectedCategory); AddCategoryView.CategoryName = ""; } catch (ValidationException vex) { MessageBox.Show(vex.Message); } }
public void EditFoodCategory(CategoryDTO categoryDto, FoodCategory foodCategory) { FoodCategory editedCategory = parseCategoryDTO(categoryDto); Validator.ValidateObject(editedCategory, new ValidationContext(editedCategory), true); if (editedCategory.Name.ToLower() != foodCategory.Name.ToLower() && UnitOfWork.FoodCategoryRepository.GetAll().Where(o => o.Name.ToLower().Equals(editedCategory.Name.ToLower())).Any()) { throw new ArgumentException("A category with this name already exists."); } foodCategory.Update(editedCategory); UnitOfWork.FoodCategoryRepository.Edit(foodCategory); }
public FoodCategory AddCategory(CategoryDTO categoryDto) { FoodCategory newCategory = parseCategoryDTO(categoryDto); Validator.ValidateObject(newCategory, new ValidationContext(newCategory), true); if (UnitOfWork.FoodCategoryRepository.GetAll().Where(x => x.Name.ToLower().Equals(newCategory.Name.ToLower())).Any()) { throw new ValidationException("A category with this name already exists."); } UnitOfWork.FoodCategoryRepository.Add(newCategory); UnitOfWork.Save(); return newCategory; }
private FoodCategory parseCategoryDTO(CategoryDTO dto) { dto.Name = dto.Name.Trim(); return new FoodCategory() { Name = dto.Name }; }
private void OnEditFoodButtonClick(object sender, EventArgs e) { if (InEditMode) { //Save the edited changes to the database FoodDTO dto = FoodItemPresenter.GetInputs(); Food selectedFood = FoodLookupPresenter.SelectedFood; try { if (dto.Category == null) { CategoryDTO categoryDto = new CategoryDTO() { Name = FoodItemPresenter.GetNewCategoryName() }; FoodCategory newCategory = FoodTracker.AddCategory(categoryDto); dto.Category = newCategory; } FoodTracker.EditFood(dto, selectedFood); FoodItemPresenter.UpdateCategories(); if (!dto.Category.Equals(FoodLookupPresenter.SelectedCategory)) { FoodLookupPresenter.SelectedCategory = dto.Category; FoodLookupPresenter.SelectedFood = selectedFood; } InEditMode = false; } catch(ValidationException vex) { MessageBox.Show(vex.Message); } } else { InEditMode = true; } }