Пример #1
0
        private bool Validate(DishAddDTO dishAddDTO, ref string message)
        {
            bool    isValid      = true;
            string  name         = dishAddDTO.Name;
            string  categoryName = dishAddDTO.CategoryName;
            decimal price        = dishAddDTO.Price;

            if (String.IsNullOrEmpty(name))
            {
                isValid = false;
                message = "Dish's name cannot be empty";
            }
            if (name.Length > 40)
            {
                isValid = false;
                message = "Dish's name cannot be more then 40 symbols";
            }
            else if (String.IsNullOrEmpty(categoryName))
            {
                isValid = false;
                message = "Dish's category's name cannot be empty";
            }
            else if (price < 0)
            {
                isValid = false;
                message = "Dish's price cannot be less then 0";
            }
            else if (unitOfWork.Dishes.GetAll().Any(dish => dish.Name == name))
            {
                isValid = false;
                message = "Dish with such name already exists";
            }

            return(isValid);
        }
Пример #2
0
        private void RaiseDishAddedEvent(DishAddDTO dish)
        {
            var handler = DishAdded;

            if (handler != null)
            {
                GenericEventArgs <DishAddDTO> e = new GenericEventArgs <DishAddDTO>(dish);
                handler(this, e);
            }
        }
Пример #3
0
        public DishAddViewModel(IControllerFactory factory, ICategorySubject subject)
        {
            this.factory = factory;
            this.dish    = new DishAddDTO();

            this.SaveCommand = new DelegateCommand(Save, CanSave);

            this.Categories = new ObservableCollection <CategoryDisplayDTO>();

            subject.Subscribe(this);
            Update();
        }
Пример #4
0
        private void OnAdd(DishAddDTO dish, DishAddViewModel viewModel)
        {
            using (IDishController controller = factory.CreateDishController())
            {
                ControllerMessage controllerMessage = controller.Add(dish);
                if (controllerMessage.IsSuccess)
                {
                    viewModel.Name  = String.Empty;
                    viewModel.Price = 0;

                    Notify();
                }
                else
                {
                    MessageBox.Show(controllerMessage.Message);
                }
            }
        }
Пример #5
0
        public ControllerMessage Add(DishAddDTO dishAddDTO)
        {
            string message = String.Empty;
            bool   success = Validate(dishAddDTO, ref message);

            if (success)
            {
                try
                {
                    CategoryEntity category = unitOfWork.Categories.Get(dishAddDTO.CategoryName);
                    if (category != null)
                    {
                        DishEntity dishEntity = new DishEntity
                        {
                            Name     = dishAddDTO.Name,
                            Price    = dishAddDTO.Price,
                            Category = category
                        };

                        unitOfWork.Dishes.Add(dishEntity);
                        unitOfWork.Commit();

                        message = "Dish added";
                    }
                    else
                    {
                        success = false;
                        message = "Category not found";
                    }
                }
                catch (Exception ex)
                {
                    success = false;
                    message = ExceptionMessageBuilder.BuildMessage(ex);
                }
            }

            return(new ControllerMessage(success, message));
        }